Python Numbers – Three cheers for int, float and complex numbers
In this Python numbers article, we will look at the different types of numbers and understand how to work with them in Python. Then we will see how numbers can be converted from one form to another, i.e. Type conversions.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
Python Number Types
Python has basically three numeric data types named as int, float, and complex.
Since Python is a dynamically typed language we don’t have to define the data type, it automatically recognizes the data type during runtime when we assign the variable.
Time to level up your coding skills with variables in Python.
1. Python int
In Maths, an integer is a whole number that can be either positive or negative and the number does not have any fractional value.
Python int is the same and we can create number objects directly by assigning a numeric value to them.
Num1 = 45 Num2 = -10 print( Num1, Num2) type(Num1)
In Python, there is no limit to how long a number can be. It is only constrained by the amount of memory your system has.
Num = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 1 print(Num)
The division operator(‘/’) will convert the number into float. We use (‘//’) to do integer division.
Ease your programming skills using operators in Python.
print(4/2) type(4/2) print(4//2) type(4//2)
Python Integers Representation in different base
print(0b100, 0B100) #100 in binary print(0o100, 0O100) #100 in octal print(0x100, 0X100) #100 in hexadecimal
The type of Python integers is the same in all the different representations.
type(0b01) type(0o10) type(0x10)
2. Python float
In floating-point numbers, there is no fixed number of digits before or after the decimal. All the real numbers that have one decimal can be considered as a float value.
X = 1.0 Y = 25.125 Z = - 48.1928569 print(X, Y, Z) type(X)
Scientific Notation of Float Numbers
Float numbers can also be represented in scientific notation by using the character ‘e’ or ‘E’.
For example, 0.01 can be denoted as 1e-2.
X = 1e-2 Y = 4e5 Z = 2.5e105 print(X) print(Y) print(Z)
2.5e + 105
The limit to floating-point numbers is that it can only store values up to 1.79 * 10^308. Values maximum than this will be represented with ‘inf’.
Next in Python numbers article, we will discuss about complex numbers.
3. Python Complex number
Complex numbers are represented in the form of (a+ib), where a is called the real part and b is called the imaginary part. Python uses j instead of i.
Let’s see the representation of a complex number in code.
X = 4+2j Y = 2-5j Z = -5j print(X) print(Y) print(Z) type(5-4j)
It is compulsory to define the coefficient of the imaginary part, otherwise, the j will be considered as a variable.
X = 4 + j # NameError: name ‘j’ is not defined Y = 4 + 1j # Valid complex number
Python Type Conversion
Type conversion is the process of converting data types from one type to another. For example, an integer value is converted into a float, or a string is converted into a number.
Type conversion can occur in one of the two types –
1. Implicit Conversion in Python
In implicit conversion, the Python automatically converts the value from one type to another.
For example: when we add an integer and a float value, the resultant will be in float value.
In these types of situations, Python always converts the smaller data types into larger data types so that data is not lost.
X = 12 Y = 20.50 Z = X + Y print(Z) type(X) type(Y) type(Z)
However, when we add a string and an integer together, Python is not able to use implicit conversion and it will give us a TypeError.
We can achieve this task by explicitly converting the data type.
2. Explicit Conversion in Python
In explicit conversion, users convert the data type of an object to the required data type. To perform the explicit conversion, we have some predefined functions like int(), float(), str(), hex(), oct(), etc.
This process is also called Type Casting. One thing to note here is that some data can be lost during typecasting.
The int() function can convert another numeric or string data type into an integer type.
print( int(5.25) ) print( int(5.80) ) print( int(“456”) ) print( int(0b1010) )
As you can see, while converting to an integer, Python discards the numbers after the decimal point.
You can’t convert a complex number into an integer. Python will give you TypeError.
This function converts other numeric values into floating values.
print( float(5) ) print( float(0xb) )
Complex numbers can not be converted into float values either.
Numeric values can be converted into complex numbers with complex() function.
print( complex(3) ) print( complex(8.5))
(8.5 + 0j)
The function bin() will convert integer numbers into their binary representation.
print( bin(20) ) print( bin(4) )
oct() function will convert integer numbers into their octal representations.
print( oct(20) ) print( oct(4) )
The function hex() also converts the integer numbers into hexadecimal representations.
print( oct(10) ) print( oct(5000) )
Note: bin(), oct() and hex() function are only used for integer values, float, and complex numbers cannot be converted through them.
This article made a detailed talk about Python numbers and their different types: int, float and complex numbers. We also saw how they can be represented in binary, octal, and hexadecimal.
Later on, we understood the type conversion and how you can convert it from one type to another and also saw the limitations of each of them.
If you have any queries in TechVidvan’s Python numbers article, mention them in the comment section.