In this article, we are going to learn all about Data types in Python with the help of examples. Data types are one of the most important stuff of any programming language that tells to interpreter what type of data the programmer wants to use.
In this, we will explore Python data types with the help of suitable examples along with their different data types.
Headings of Contents
Data Types in Python
In Python programming, every value is a data type. Since everything is an object in Python programming. Data types in Python are classes and variables are the instances ( Objects ) of these classes.
Here list of different Python data types.
- Number
- String
- Float
- Int
- Complex
- Bool
- Set
- Dict
- List
- Tuple
In Python, the variable stores a value and the value belongs to a specific data type, I don’t know what type of value stores the variable so don’t worry Python provides a type() built-in function that is used to find the data types of variable.
Let us understand by an example.
abc = 'Python'
print(abc)
print(type(abc))
x = [1,2,3,4,5]
print(x)
print(type(x))
Let’s understand each data type one by one with the help of the examples.
Number Data Type
In Python integers, floating numbers, and complex numbers belong to the Python number data type. These data types are defined as int, float, and complex.
To know the data type of any Python variable, you can use the Python type() function.
Example
a = 12 # integer data type
print(type(a))
b = 12.0 # float data type
print(type(b))
c = 12+bj # complex data type
print(type(c))
String Data Type
In Python collection of characters are strings for example “Python“, and “Database” strings. String value belongs to the str data type.
Example
abc = 'Python'
print("The value of a:- ", abc)
print("The data type of abc:- ", type(abc))
Boolean Data Type
In Python True and False comes under the Boolean data type.
Example
a = True
print(type(a))
b = False
print(type(b))
Set Data Type
Python set is a collection of items. Python Set is s unindexed, mutable, or changeable data type, and also set does not allow duplicate items. In Python set define inside curly { } bracket.
Example
a = {1,2,3,4,5,6}
print("Set is:- ", a)
# data type of set
print(type(a))
When you assign duplicate items inside a set in Python, It eliminates duplicate items automatically.
Example
a = {1,2,3,4,5,6,6, 7, 7}
# print the value of a
print("Set is:- ", a)
# data type of set
print(type(a))
Dict Data Type
A dict or Dictionary is a collection of elements that are unordered, indexed, and changeable or mutable. In Python, a dictionary has keys and values and each value has its corresponding keys if you want to access the value then you will need to use the key.
Python dictionary is written inside curly bracket { } in the form of key: value pair.
Example
dict1 = {'id':12,'name':'Alex','salary':12000}
# print the value of dict1
print(dict['name'])
data type of dict1
print(type(dict1))
You can use Python for loop to access the key and value of the Python dictionary.
Example
dict1 = {'id':12,'name':'Alex','salary':12000}
for key, value in dict1.items():
print(f"{key}: {value}")
"""
Output
id: 12
name: Alex
salary: 12000
"""
List Data Type
The list is a collection of elements that a changeable, indexed, and mutable data types. In Python, the list allows duplicate values, a list written with a square bracket [ ].
Example
# defined list
myList = [1,2,3,6,6,7,7]
# print the value of list
print(myList)
# data type of the list
print(type(myList))
You can use Python for loop to access the value of the Python list.
Example
myList = [1,2,3,6,6,7,7]
for i in myList:
print(i)
Output
1
2
3
6
6
7
7
Tuple Data Type
In Python tuple is a collection of elements that are ordered and unchangeable or immutable and a tuple also allows duplicate items. If a tuple once created don’t modify it again. A tuple is written with a circular bracket ( ).
Example
# defined type
tuple1 = (1,2,3,3,4)
# print type value
print(tuple)
# data type of Python tuple
print(type(tuple1))
You can use Python for loop to access the value of the Python tuple as well.
Example
tuple1 = (1,2,3,3,4)
for i in tuple1:
print(i)
Output
1
2
3
3
4
Data Type conversion
In Python, we can convert one data type to another data type easily. The process of data type conversion is called type conversion.
let’s see how we can convert.
# convert to floating point
print(float(a))
# convert to complex
print(complex(a))
# convert to str
print(str(a))
# convert to octal
print(oct(a))
# convert to binary
print(bin(a))
# convert to hexadecial
print(hex(a))
Conclusion
So, in this article, we have seen all about Data types in Python with the help of examples. If we talk about data type in one line, Then it will be the identity of the data, the programmer wants to use.
Unlike other Programming language such as C, JavaScript, C++, and Java, Python does not have any keywords to declare variables.
Python provides lots of built-in functions to convert variables from one data type to another data type.
If you like this article, please share and support us, so that we come back for interesting Python tutorials.
Thanks for reading