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 and with the help of suitable exmaples along with their different data types.
Headings of Contents
Data types in Python
In Python programming, every value is an data type. Since everything is the object in Python programming. Data types in Python is 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 is storing a value and the value belongs to a specific data type, I don’t know what type of value store the variable then don’t worry python provides a type() built-in function which is used to find the data types of variable.
Let understand by an example.
abc = 'Python'
print(abc)
print(type(abc))
x = [1,2,3,4,5]
print(x)
print(type(x))
Let understand each data type one by one with the help of the examples.
Number Data Type
In python integer, floating number and complex number belong to Python number data type. These data types defined as int, float, and complex.
To know the data type of the any Python variable, you can use 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 character is string for example “Python“, “Database” is a string. 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 set in Python, It eliminate 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
Dict or Dictionary is a collection of elements that is unordered, indexed, and changeable or mutable.
In python, a dictionary has keys and values and each value has its corresponding keys and if
you want to access value then you will need to use the key.
Python dictionary 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 element which is a changeable, indexed, and mutable data type. 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 keyword to declare variables.
Python provide lots of built-in function to convert variable 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