In this Python tutorial, we will see Python Data types with examples. To know the data types in Python, you must know the Python variables, and rules to create variables in Python.
➡️ Click here to learn all about the Python Variables.
Before going to see Python data types let’s brushup the Python Variables.
Headings of Contents
What are Python Variables?
Variables in Python are a kind of container that is used to store any kind of values for future use cases. As we know, Python is a dynamic-typed programming language which means we don’t need to specify the type of the variable before storing the value to a variable.
Here dynamically typed means, Python automatically detects the data type of the variable based on the value stored in a variable.
Let’s see a simple example.
name = "Programming Funda" # Data type of the name will be string. name = 111 # Data type of the name will be int.
Let’s see how we can know the data type of the variables using Python’s built-in function.
What are Python Data Types?
Data types in Python mean type of the value that a variable can store. In the above example, the data type of the first name will be Sring and the data type of the second name will be int.
Data Types in Python with Examples
To check the data type in Python, Python provides a function called type(). The type() function returns the data type of the passed variable.
Here I have mentioned some common data types in Python with the help of the examples.
Numeric Type
Under Numaric there are three data types. int, float, and complex.
Let’s by an example.
age = 56 # int data types print(type(age)) value = 101.40 # float data type print(type(value)) complex_value = 10+10j # complex data type print(type(complex_value))
Output
<class 'int'> <class 'float'> <class 'complex'>
String Type
The sequence of characters enclosed by single or double quotes belongs to the string data type.
name = "Programming Funda" # string data type print(type(name))
The Output will be
<class 'str'>
Boolean Type
Only two values belong to a boolean data type which is True and False.
value_1 = True value_2 = False # bool data type print(type(value_1)) print(type(value_2))
Output
<class 'bool'>
<class 'bool'>
Sequence Type
Python list and Python tuple belong to sequence data type.
lst = [1, 2, 3, 4] tpl = (1, 2, 3, 4) # sequence data type print(type(lst)) print(type(tpl))
Output
<class 'list'> <class 'tuple'>
Mapping Type
Python Dictionary belongs to the mapping type because it stores data in the form of keys and values.
dct = {"name": "Programming Funda", "age": "4 Years"} # dict data type print(type(dct))
Output
<class 'dict'>
Set Type
Python set belongs to the set data type. It is an unordered collection of unique items and it is a mutable data type also. Python set always creates, placing all the items inside curly braces {} separated by commas.
st = {"Programming Funda", 12} # set data type print(type(st))
Output
<class 'set'>
None Type
A variable with a None value indicates the null data type.
value = None # null data type print(type(value))
<class 'NoneType'>
Python Useful Articles:
Conclusion
So, in this tutorial, we have seen all about data types in Python with examples. Python data types are important in real-life applications because sometimes you can decide based on data types of Python variable values.
Understanding variables and data types is fundamental in Python programming, as it allows you to effectively manipulate and work with different kinds of data within your programs.
If you found this article helpful, please share and keep visiting for further Python tutorials.
Thanks for your time…