In this article, we will learn about basic data types in Python programming language. Data types are very important in any programming language. Choosing the right set of data types for your program has a lot to do with the efficiency, accuracy & performance of that program. So it becomes critical to understand data types well so that you are able to utilize them effectively.
Python programming language offers various data types that help in the development of various applications with different requirements. We will learn about basic data types available in the Python programming language as part of this article and learn more about advanced data types in our upcoming articles.
This is the fifth article in the Series: Python Tutorial – Learn Python Programming for Beginners
So far as part of this series we have learned about the Python programming language concepts, Python installation & usage with Visual Studio Code & Variable in Python.
Table of Contents
Basic Data Types in Python

Every variable that you create in Python programs can store values of different types and this data type for a variable gets set either explicitly while assigning value to a variable using cast variable or it gets set implicitly based on the type of value being assigned to a variable.
Everything in Python programming is an object so data type is a class and variable is an instance of this class. Data types help to understand what kind of operations can be performed on a value.
The type() function can be used to know about the data type of value or a variable. Similarly, there is also an isInstance() function which can be used to check if a variable or value is of a particular data type or not.
As part of basic data types in Python, we will learn about Numeric & Boolean data types. So let’s dive into the details of those types.
Numeric Types
Numeric data types are used to store data values of type numbers it can be either integer, float or complex numbers. There are no commands to create a Numeric variable it gets created when you assign a value to them.
Int
Int or Integer are whole numbers without a fraction and they can be either positive or negative numbers e.g. 50, 100, -250, 999999, etc. There is no limit to how long an int value can be i.e. it can be as long as your program requires it to be.
variable1 = 1 variable2 = 50 variable3 = 992826615522772 variable4 = -992826615522772
If you will check the type of the above variable using the type() function then for all the variables you will get the type as int i.e. <class, ‘int’>. Below we have used the type() function to print & check the data types of the variables.

You can explicitly set the data type of a variable to int when assigning a value to that variable using the int() function as shown below
variable1 = int(50.0)
Float
Float numbers are decimal values specified with a decimal point. This decimal point makes them different from int. Float can also be either a positive or negative number. Float can be accurate up to 15 decimal places. e.g. 1.2, 72626.653, -456.25, etc.
Float can also hold scientific numbers with an e followed by a positive or negative integer that indicates the power of 10 e.g. 8e2, 8.3e-2, etc.
variable1 = 1.98 variable2 = 454.567 variable3 = -284902.8277 variable4 = 8e2
If you will verify the type of the above variable, using the type() function, then for all variables you will get the type as float i.e. <class, ‘float’>. Below we have used the type() function to print & check the data types of the variables.

You can explicitly set the data type of a variable to float when assigning a value to that variable using the float() function as shown below
variable1 = float(50)
Complex
Complex numbers are represented in two parts i.e. a real part and imaginary component like 3 + 5j (<realpart> + <imaginarypart>j)
variable1 = 2+9j variable2 = 15+98j
If you will check the type of the above variable using the type() function then for both the variables you will get the type as complex i.e. <class, ‘complex’>. Below we have used the type() function to print & check the data types of the variables.

Type Conversion
Like any other programming language in Python also it is possible to convert numeric data values from one type to another. There are functions available for conversions i.e. int(), float() & complex()
You cannot convert complex numbers into int or float as that is not possible in Python. If you try to convert complex type into float then it will throw an error i.e. TypeError – can’t convert complex to float.
variable1 = 50 variable2 = 10.5 variable3 = 2 + 3j print(type(variable1)) print(type(variable2)) print(type(variable3)) variable1 = float(variable1) variable2 = int(variable2) print(type(variable1)) print(type(variable2)) print(variable1) print(variable2)
As you can see above we have declared variable1 & variable2 as int & float respectively based on values assigned to those variable1 & variable2. Later on, in code, we have converted variable1 from int to float and on the same lines, we have also converted variable2 from float to int. Below is the result of the above code

We can see from the above results that variables were converted from int to float and vice versa and in process of doing so the value of float 10.5 got changed to 10 to fit it into an int. So be careful while converting data types you may lose actual values which can cause an issue.
So far we have learned about numeric data types in Python now let’s learn about one more basic data type in Python i.e. Boolean and look at how to use Boolean data types in Python programming language.
Boolean Type
Boolean is represented by either True or False. There are only 2 possible values for Boolean i.e. Boolean variables in Python can have one of the 2 values i.e. True or False.
In Python, if you compare two values then that also results in Boolean i.e. either both expressions match (True) or they are different (False).
Note that Boolean values in Python are True & False with a capital “T” & “F” as they are not valid with small “t” & “f” i.e. true & false are invalid and will throw an error. The class name for Boolean in Python is bool
variable1 = True variable2 = False
If you will check the type of the above variables using the type() function then for both the variables you will get the type as bool i.e. <class, ‘bool’>

Summary
We learned about basic data types in Python like int, float, complex & Boolean. We also saw how to perform type conversion for numeric data types in Python.
In our next article, we will learn about additional data types in Python i.e. Sequence Types in the Python programming language.
Please provide your suggestions & questions in the comments section below
References – Data Types in Python
Hope you found this article useful. Please support the Author
