In previous post we have seen about variables and operators in Python. If you notice, you may notice that the variables are different here, even though we have been dealing with numeric values so far.
But in order to work normally, we have to work with different types of data, that is, the variables are also different. Just as we buy a liter of water instead of a kilogram, programming languages use specific types of data for specific tasks. And these different data types are called data types.
Every programming language has certain provided or built-in data types. Also in Python, but since Python data types are very rich and for ease of understanding, it can be divided into some basic parts –
- Boolean
- bool
- Binary
- bytes
- Bytearray
- Memoryview
- Numeric
- int
- float
- complex
- String
- str
- Sequence
- list
- tuple
- Mapping
- dict
- set
- set
- frozenset
Now we will learn these data types in detail.
Boolean
Boolean data type is very limited. A data type that is limited to True and False only. That is, only these two values can exist here.
bool
Boolean data type is denoted by bool in Python. Boolean data is used to evaluate the value of a conditional statement, the message of a statement or evaluate a variable, etc.
# This program is an example of boolean operator in python
a=10
b=5
# As a Conditional Statement
print(a>b)
print(a<b)
print(bool("python")) # As a validator
# As a message
if a>b:
print('a is greater than b')
else:
print('a is less than b')
Result –
True
False
True
a is greater than b
Note:
- Since Python is case sensitive, boolean values are only True and False; true and not false. Hope you understand the difference.
- If you look at line number 10, you will see that the result is True. Why True? Because Boolean operator indicates True if a variable has a value and False if it is not i.e. empty.
Numeric data type
We already know more or less about numerical data. That is, numbers will be discussed here.
int
The int data type holds any integer, positive or negative number. Python defines types dynamically so there is no need to worry about large numbers; Just be careful that it doesn’t become a fraction.
float
The float data type can also take decimal numbers. This is the only difference with int. A float number can be positive or negative.
complex
Complex numbers are used in many complex mathematical problems. And complex data is used for this complex number. Complex numbers are denoted by j.
Now let’s see all these together with an example –
# This program is an example of numerical data type in python
a=10
b=55555555555555555555555555
c=15.155
d=3+4j
print("The value of a is {} and type {}".format(a,type(a)))
print("The value of c is {} and type {}".format(c,type(c)))
print("The value of c is {} and type {}".format(d,type(d)))
Result:
The value of a is 10 and type <class 'int'>
The value of c is 15.155 and type <class 'float'>
The value of c is (3+4j) and type <class 'complex'>
Note: .format is used here to display the result. Its usage is where the value should be left blank with {}, then given as a variable inside the format. Instead of {}, the variables will sit one by one.
String
A string is a sequence of characters or letters. ‘String’ is a widely used data type in Python. From writing simple programs to building complex software, the use of strings is essential everywhere. Here we will look at how to use strings as well as some built-in member functions.
str
Strings are represented by str in Python. Let’s look at an example –
name="Karim"
print("Your name is {} and the data type is {}".format(name,type(name)))
Result:
Your name is Karim and the data type is <class 'str'>
That is, here Karim is a string data type.
As mentioned earlier, strings are sequential insertions of characters. So, the characters are arranged according to a specific index, and these indexes can be specified separately. For example –
name="Karim"
print("Your name is {} and the 3rd character is {}".format(name,name[2]))
Result:
Your name is Karim and the 3rd character is r
Note here that index starts from 0, never from 1. Here name[2] means the third element.
Since the scope of the discussion of strings is very large, I will end here, more details can be discussed later.
Sequence
Python’s sequence data type maintains a specific order and stores data of the same or similar types. Note that String is a sequence data type; But considering its prevalence, it is discussed separately.
list
A list is a collection of data, in other programming languages (C, C++ etc.) it is called an array. There is no separate method or function to define the list. The data must be placed inside the brackets one by one with commas. List values can be changed.
# This program is an example of list data type in python
a=[10,20,40] #Declaring a list
print("The value of a is {} and type {}".format(a,type(a)))
Result:
The value of a is [10, 20, 40] and type <class 'list'>
List elements like strings are accessed by index. To update the value of the list under the specified index.
a=[10,20,40] #Declaring a list
print("The Third value of this list is {}".format(a[1])) # Accessing a specific element in a list
a[1]=30 #update list for specific index
print("The value of updeted list is {}".format(a))
Result:
The third value of this list is 20
The value of updated list is [10, 30, 40]
Notice in line 5 that the value is changed using a specific index.
Tuple
Tuples and lists are almost the same data type; The only difference is that values in the list can be updated or deleted and not deleted. That is tuple immutable (immutable) data type.
Tuple elements must be enclosed in parentheses () with commas.
# This program is an example of Tuple data type in python
a=(10,20,40) #Declairng a tpple
print("Third value of this tuple is {}".format(a[1])) # Accessing a specific element in a tuple
a[1]=30 #update list for specific index
Result:
Third value of this list is 20
Traceback (most recent call last):
File "main.py", line 15, in <module>
a[1]=30 #update list for specific index
TypeError: 'tuple' object does not support item assignment
In the above result we see an error because we tried to update the value of the tuple in the 4th line which is not acceptable in Python.
Mapping
Sometimes hashes or mappings are needed to increase program efficiency. Most conveniently, Python has a built-in mapping data type known as a dictionary.
dict
dict is Python’s unordered data type. Generally speaking, just like in a Bengali or English dictionary the words are inserted under a letter, the Python dictionary also has a key, value pair. One has to access a particular value with its key. The structure is – key:value, as we will see below with an example.
# This program is an example of dict data type in python
a={1:'Python',2:'Anaconda',4:'Cobra'} #Declaring a dict
print("Third value of this dict is {}".format(a)) # Accessing dict
print("The first value of this dict is {}".format(a[1])) # Accessing a specific element in a dict
Result:
Third value of this dict is {1: 'Python', 2: 'Anaconda', 4: 'Cobra'}
The first value of this dict is Python
If you look at the 6th line, you will see that the value is accessed with the key of the existing dictionary.
set
The set data type was created in Python to represent the concept of mathematical sets. Duplicate values can never be added to a set. Sets are much faster than lists to find a specific element. Union, intersection operations can be done in the set.
# This program is an example of set data type in python
a={"Python","Anaconda","Cobra"} #Declaring a set
print("Third value of this set is {}".format(a)) # Accessing set
print("The value Python exists in this set a :{}".format("Python" in a)) # Accessing a specific element in a dict
a.add("Vipers") #adding element in set
a.remove("Anaconda") #remove and element from a set
print("Third value of this set is {}".format(a)) # Accessing set
Result:
Third value of this set is {'Anaconda', 'Cobra', 'Python'}
The value Python exists in this set a :True
Third value of this set is {'Cobra', 'Vipers', 'Python'}
A new element Vipers is added in the 8th line.
An element Anaconda is deleted on line 9.
From the result in line 11 we can see that Vipers have been added to our set and Anaconda has been removed.
Thanks everyone.