In this post, you’ll learn about the types of numbers used in Python, how to convert one number data type to another number data type (python number type conversion). and the mathematical operations supported with numbers in Python.
In any programming language, there is an important role of numbers. We perform mathematical operations only using numbers. Such as Addition, Substruction, Divide, Multiplication, Etc, this is a basic thing. Apart from this, we use numbers in many ways in the programming language.
Now we will know how many types of numbers are there in Python.
Number Data Type in Python
Python supports integers, floating-point numbers, and complex numbers.
In which, the integer is used using the int
keyword. The float number is defined using the float
keyword and the complex number is defined by using the complex
keyword.
Note:- In Python, when you are declaring a variable. Then you do not need to define its data type. Why Python when you assign a value to a variable. Then it decides its data type itself according to the value.
The type() function is used to find the type of a variable in Python. Types Mean Variables, int, float, strings, etc.
python int number
In Python, the int keyword is used for integer numbers. An integer number is one that is a whole number, positive and negative number. Which does not have a decimal point.
Example: int number in python
a = 1 print(a) print(type(a))
Output
1 <class 'int'>
Python float number
In Python, the float keyword is used for float numbers. A float number is one that is not a whole number but it can positive and negative number with a decimal point.
Example: float number in python
a = 1.1 print(a) print(type(a))
output
1.1 <class 'float'>
Python complex number
Complex numbers are written with a “j” as the imaginary part.
Example: complex number in python
a = 4+2j print(a) print(type(a))
Output
(4+2j) <class 'complex'>
Type conversion in python
You can use int(), float(), and complex() method for convert one number data type to another number data type.
Example: python convert int to float
x = 5 # int #convert from int to float: a = float(x) print(a) print(type(a))
Explanation of the above program
In this example, we did a variable declaration named x. And assign an int value 5 to it.
After this, we used the float () function to convert the integer number to the floating number in python and assign it to a new variable.
In the last, we printed from it and you can see the output. The Integer number was converted to a floating number in python.
Output
5.0 <class 'float'>
Example: python convert float to int
x = 5.5 # float #convert from float to int: a = int(x) print(a) print(type(a))
Explanation of the above program
In this example, we did a variable declaration named x. And assign an float value 5.5 to it.
After this, we used the int() function to convert the float number to the integer number in python and assign it to a new variable.
In the last, we printed from it and you can see the output. The float number was converted to a integer number in python.
Output
5 <class 'int'>
Example: convert int to complex number python
x = 5 # int #convert from int to complex: a = complex(x) print(a) print(type(a))
Explanation of the above program
In this example, we did a variable declaration named x. And assign an integer value 5 to it.
After this, we used the complex() function to convert the integer number to the complex number in python and assign it to a new variable.
In the last, we printed from it and you can see the output. The Integer number was converted to a complex number in python.
Output
(5+0j) <class 'complex'>
Note: You cannot convert complex numbers into another number type.