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.

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.

Code:

Num1 = 45
Num2 = -10
print( Num1, Num2)
type(Num1)

Output:

45 -10

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.

Code:

Num = 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 1
print(Num)

Output:

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Note:

The division operator(‘/’) will convert the number into float. We use (‘//’) to do integer division.

Ease your programming skills using operators in Python.

Code:

print(4/2)
type(4/2)

print(4//2)
type(4//2)

Output:

2.0
<class ‘float’>
2
<class ‘int’>

Python Integers Representation in different base

The default integer representation is in Decimal (base 10), but we can also store them as Binary, Octal or Hexadecimal. They require a prefix before the number.python numbers representation of integers

Code:

print(0b100, 0B100) #100 in binary
print(0o100, 0O100) #100 in octal
print(0x100, 0X100) #100 in hexadecimal

Output:

4 4
64 64
246 256

The type of Python integers is the same in all the different representations.

Code:

type(0b01)
type(0o10)
type(0x10)

Output:

<class ‘int’>
<class ‘int’>
<class ‘int’>

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.

Code:

X = 1.0
Y = 25.125
Z = - 48.1928569

print(X, Y, Z)
type(X)

Output:

1.0 25.125 -48.1928569
<class ‘float’>

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.

Code:

X = 1e-2
Y = 4e5
Z = 2.5e105

print(X)
print(Y)
print(Z)

Output:

0.01
400000.0
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’.

Code:

print(1.79e308)
print(1.8e308)

Output:

1.79e + 308
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.

Code:

X = 4+2j
Y = 2-5j
Z = -5j

print(X)
print(Y)
print(Z)

type(5-4j)

Output:

(4+2j)
(2-5j)
(-0-5j)
<class ‘complex’>

Note:

It is compulsory to define the coefficient of the imaginary part, otherwise, the j will be considered as a variable.

Code:

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 –

  • Implicit Conversion
  • Explicit Conversiontype conversion in Python numbers

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.

Code:

X = 12
Y = 20.50
Z = X + Y
print(Z)

type(X)
type(Y)
type(Z)

Output:

32.5
<class ‘int’>
<class ‘float’>
<class ‘float’>

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.

2.1. int()

The int() function can convert another numeric or string data type into an integer type.

Code:

print( int(5.25) )
print( int(5.80) )
print( int(“456”) )
print( int(0b1010) )

Output:

5
5
456
10

As you can see, while converting to an integer, Python discards the numbers after the decimal point.

Note:

You can’t convert a complex number into an integer. Python will give you TypeError.

2.2. float()

This function converts other numeric values into floating values.

Code:

print( float(5) )
print( float(0xb) )

Output:

5.0
11.0

Note:

Complex numbers can not be converted into float values either.

2.3. complex()

Numeric values can be converted into complex numbers with complex() function.

Code:

print( complex(3) )
print( complex(8.5))

Output:

(5 + 0j)
(8.5 + 0j)

2.4. bin()

The function bin() will convert integer numbers into their binary representation.

Code:

print( bin(20) )
print( bin(4) )

Output:

0b10100
0b100

2.5. oct()

oct() function will convert integer numbers into their octal representations.

Code:

print( oct(20) )
print( oct(4) )

Output:

0b10100
0b100

2.6. hex()

The function hex() also converts the integer numbers into hexadecimal representations.

Code:

print( oct(10) )
print( oct(5000) )

Output:

0xa
0x1388

Note: bin(), oct() and hex() function are only used for integer values, float, and complex numbers cannot be converted through them.

Summary

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.

Happy Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.