Data type defines the type of the variable, whether it is an integer variable, string variable, tuple, dictionary, list etc. In this guide, you will learn about the data types and their usage in Python.
Python data types
Python data types are divided in two categories, mutable data types and immutable data types.
Immutable Data types in Python
1. Numeric
2. String
3. Tuple
Mutable Data types in Python
1. List
2. Dictionary
3. Set
1. Numeric Data Type in Python
Integer – In Python 3, there is no upper bound on the integer number which means we can have the value as large as our system memory allows.
# Integer number num = 100 print(num) print("Data Type of variable num is", type(num))
Output:
Long – Long data type is deprecated in Python 3 because there is no need for it, since the integer has no upper limit, there is no point in having a data type that allows larger upper limit than integers.
Float – Values with decimal points are the float values, there is no need to specify the data type in Python. It is automatically inferred based on the value we are assigning to a variable. For example here fnum is a float data type.
# float number fnum = 34.45 print(fnum) print("Data Type of variable fnum is", type(fnum))
Output:
Complex Number – Numbers with real and imaginary parts are known as complex numbers. Unlike other programming language such as Java, Python is able to identify these complex numbers with the values. In the following example when we print the type of the variable cnum, it prints as complex number.
# complex number cnum = 3 + 4j print(cnum) print("Data Type of variable cnum is", type(cnum))
Output:
Binary, Octal and Hexadecimal numbers
In Python we can print decimal equivalent of binary, octal and hexadecimal numbers using the prefixes.
0b(zero + ‘b’) and 0B(zero + ‘B’) – Binary Number
0o(zero + ‘o’) and 0O(zero + ‘O’) – Octal Number
0x(zero + ‘x’) and 0X(zero + ‘X’) – Hexadecimal Number
# integer equivalent of binary number 101 num = 0b101 print(num) # integer equivalent of Octal number 32 num2 = 0o32 print(num2) # integer equivalent of Hexadecimal number FF num3 = 0xFF print(num3)
Output:
2. Python Data Type – String
String is a sequence of characters in Python. The data type of String in Python is called “str”.
Strings in Python are either enclosed with single quotes or double quotes. In the following example we have demonstrated two strings one with the double quotes and other string s2 with the single quotes. To read more about strings, refer this article: Python Strings.
# Python program to print strings and type s = "This is a String" s2 = 'This is also a String' # displaying string s and its type print(s) print(type(s)) # displaying string s2 and its type print(s2) print(type(s2))
Output:
3. Python Data Type – Tuple
Tuple is immutable data type in Python which means it cannot be changed. It is an ordered collection of elements enclosed in round brackets and separated by commas. To read more about tuple, refer this tutorial: Python tuple.
# tuple of integers t1 = (1, 2, 3, 4, 5) # prints entire tuple print(t1) # tuple of strings t2 = ("hi", "hello", "bye") # loop through tuple elements for s in t2: print (s) # tuple of mixed type elements t3 = (2, "Lucy", 45, "Steve") ''' Print a specific element indexes start with zero ''' print(t3[2])
Output:
4. Python Data Type – List
List is similar to tuple, it is also an ordered collection of elements, however list is a mutable data type which means it can be changed unlike tuple which is an immutable data type.
A list is enclosed with square brackets and elements are separated by commas. To read more about Lists, refer this guide: Python Lists
# list of integers lis1 = [1, 2, 3, 4, 5] # prints entire list print(lis1) # list of strings lis2 = ["Apple", "Orange", "Banana"] # loop through list elements for x in lis2: print (x) # List of mixed type elements lis3 = [20, "Chaitanya", 15, "BeginnersBook"] ''' Print a specific element in list indexes start with zero ''' print("Element at index 3 is:",lis3[3])
Output:
5. Python Data Type – Dictionary
Dictionary is a collection of key and value pairs. A dictionary doesn’t allow duplicate keys but the values can be duplicate. It is an ordered, indexed and mutable collection of elements. To read more about it refer: Python dictionary.
The keys in a dictionary doesn’t necessarily to be a single data type, as you can see in the following example that we have 1 integer key and two string keys.
# Dictionary example dict = {1:"Chaitanya","lastname":"Singh", "age":31} # prints the value where key value is 1 print(dict[1]) # prints the value where key value is "lastname" print(dict["lastname"]) # prints the value where key value is "age" print(dict["age"])
Output:
6. Python Data Type – Set
A set is an unordered and unindexed collection of items. This means when we print the elements of a set they will appear in the random order and we cannot access the elements of set based on indexes because it is unindexed.
Elements of set are separated by commas and enclosed in curly braces. Lets take an example to understand the sets in Python.
# Set Example myset = {"hi", 2, "bye", "Hello World"} # loop through set for a in myset: print(a) # checking whether 2 exists in myset print(2 in myset) # adding new element myset.add(99) print(myset)
Output:
Sulaiman says
If i have to insert a variable’s value between sentence how do i do that. Cause by default it will insert it at end. I mean in c language we can put %d where we want value inputed. How do we do that in python.