In this article, we will know all about Python data type and there use with suitable examples. The contents of the article is shown below:
Introduction
Variables can hold values of different types called data types. Based on the data type of a variable, the interpreter reserves memory for it and also determines the type of data that can be stored in the reserved memory. The five standard data types supported by Python include numbers, strings, list, tuples, and dictionary. We can also create our own data type in Python (like classes).
You will get to know easily the data type of a variable by simply using the type() function that we have already discussed in the previous post click here
Python has the following data types which are in-built in it by default :
Numeric type
In Python numeric data type represents that the data type which can store or can be assigned numeric values. Example – int,float,complex.
# Python program to
# demonstrate numeric value
a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
In the above code, you can see the three types of numeric data types. The first one is of int that means integer type which doesn’t contain any fractional part within it. Second is a float type which contain both integer and fractional part and finally the third one is a complex one which has both real and imaginary part in it. To know more you can refer to this site.
Sequence Type
In Python sequence is a data type where we can store a list of data of similar type or different type. Example: list, range, tuple, string
for i in range(1,5):
print('Table of '+str(i))
for j in range(1,5):
x=i*j;
print(str(i)+'x'+str(j)+'='+str(x))
The above one is an example of using range. You can get the link of Python String and tuple where we discuss this in details.
Boolean
Boolean is another data type in Python. A variable of Boolean can have one of the two values—True or False. This type of variables can also be created by comparing values using the == operator. It is denoted by the class bool.
x = "Hello"
y = 15
print(bool(x))
print(bool(y))
Dictionary
A dictionary object is an unordered collection of data in a key: value pair form. A collection of such pairs is enclosed in curly brackets.
dict = {
"brand": "Realme",
"model": "X50 Pro",
"year": 2020
}
print(dict)
A full-length article on the dictionary is here
Python Casting
Casting a data type in Python simply indicates specifying the variable. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. It is done by using constructors such as :
- int(): It constructs an integer number from an integer or a float(rounding off the fractional part) or a string( providing the string represents a whole number ) type variable.
- float(): It constructs a float number from an integer or a float or a string( providing the string represents a whole number ) type variable.
- str(): constructs a string from a wide variety of data types, including strings, integer and float.
This discussion has been shown below in code.
a = int(5) # a will be 5
b = int(5.2) # b will be 5
c = int("5") # c will be 5
print(a)
print(b)
print(c)
The above casting is for int()
a = float(2) # x will be 1.0
b = float(2.8) # y will be 2.8
c = float("2") # z will be 2.0
d = float("2.2") # w will be 2.2
print(a)
print(b)
print(c)
print(d)
This is an example of casting for float()
x = str("Python") # x will be 'Python'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
The above is for str()
Mutable and Immutable Objects
There’s a concept of mutable and immutable object in Python. It is that if an object can be altered or modified after it’s creation it is known as mutable Ex: list and if an object can’t be altered after it’s creation it is immutable Ex: tuple and string.