Python Numbers Types with Examples

What is Python Numbers?

The Python Numbers data types store numeric values.

Since they are immutable, changing the value of a number data type results in a freshly allocated object.

Python Number objects are produced when a value is assigned to them.

Python supports three types of numbers: complex numbers, integers, and floating point numbers (decimals).

Every number in Python is an object of the first class. It implies that each numeric data type is implemented via a class definition.

For instance, Python programming defines entire numbers as integers.

The number’s data type can be confirmed as follows.

num1 = 1234
num2 = 12.34
print(type(num1))
print(type(num2))

Using the type() method, we may determine the data type of a number or the class to which a number belongs.

Output:

<class 'int'>
<class 'float'>

The first number is an integer, thus the returned data type is class 'int'.

The second number is a decimal, therefore the returned data type is class 'float'.

Now, we will examine each native Python number data type individually.

Number Data Types in Python

There are three number data types in Python:

  1. Integer
  2. Float
  3. Complex

1. Integer Data Type in Python

Python’s Integer Data Types represent whole numbers like 0 and negative integers like -1.

Defining an integer variable is as simple as assigning it a number.

var1 = 751
var2 = -8712
print (var1)
print ("The var1 data type is {}".format(type(var1)))
print (var2)
print ("The var2 data type is {}".format(type(var2)))

Output:

751
The var1 data type is <class 'int'>
-8712
The var2 data type is <class 'int'>

Integer data types in Python do not have a maximum value, unlike other computer languages.

You can define a number of any size you choose, but the size is limited by the capabilities of your computer.

For example, here we have built a number that contains 90 digits.

# 90 digit integer
var1 = 963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714
print ("Here is the 99 digit integer with {} type".format(type(var1)))
print (var1)

Output:

Here is the 99 digit integer with <class 'int'> type
963021548778451203698745632103698521478521548915820542014778621036985102587695184747474714

Additionally, Python offers integer data types for various number systems, such as binary, octal, and hexadecimal.

We can represent binary numbers in Python by prefixing the number with 0b, as in 0b1110, hexadecimal numbers by prefixing the number with 0x, as in 0x1117, and octal numbers by prefixing the number with 0o, as in 0o1117.

var1 = 1117
var2 = 0x1117
var3 = 0o1117
var4 = 0b1110

print("Data Type of 1117 is {}".format(type(var1)))
print("Data Type of 0x1117 is {}".format(type(var2)))
print("Data Type of 0o1117 is {}".format(type(var3)))
print("Data Type of 0b1110 is {}".format(type(var4)))

Output:

Data Type of 1117 is <class 'int'>
Data Type of 0x1117 is <class 'int'>
Data Type of 0o1117 is <class 'int'>
Data Type of 0b1110 is <class 'int'>

You may note that the data type of every number, regardless of the number system, is int.

Each binary, octal, and hexadecimal number is an instance of the int class.

2. Float Data Type in Python

Float Data Types in Python is also known as floating-point number.

Python employs double-precision floating-point numbers to represent decimal or real numbers.

A number with floating-point precision may be positive, negative, or zero.

Floating-point numbers contain one or more digits after the decimal point, much like decimal numbers.

var1 = 123.14
print (var1)
print ("The var1 data type is {}".format(type(var1)))

Output:

123.14
The var1 data type is <class 'float'>

Floats also representing real numbers, are represented with a decimal point separating the integer and fractional components.

Floats may alternatively be expressed in scientific notation, where E or e represents the power of 10 (2.5e2 = 2.5 x 102 = 250).

var1 = 35e3
var2 = 12E4
var3 = -87.7e100

print(type(var1))
print(type(var2))
print(type(var3))

Output:

<class 'float'>
<class 'float'>
<class 'float'>

3. Complex Numbers in Python

The complex numbers data type is also supported by Python.

In Python, a complex number is represented as a + i b, where a and b are numeric values representing the real and imaginary parts of the complex number, respectively.

Any English letter may be substituted for the letter i.

var1 = 6+5j
var2 = 9j
var3 = -7j

print(type(var1))
print(type(var2))
print(type(var3))

Output:

<class 'complex'>
<class 'complex'>
<class 'complex'>

As seen in the above example, the returned data type is “complex“.

Using the complex() function is another method for defining complex numbers.

The complex() function receives the real and imaginary components of the needed complex number and returns a complex number.

The actual component will be passed as the first argument, while the imaginary part will be passed as the second parameter.

Additionally, the real part is required while the imaginary part is optional.

var1= complex(3,2)
print(var1,type(var1))

Output:

(3+2j) <class 'complex'>

The "real" and "imag" characteristics of a complex number provide access to the real and imaginary components, respectively.

var1= complex(3,2)
realnum = var1.real
imagnum = var1.imag
print(var1)
print("Real number is ", realnum)
print("Imaginary number is ", imagnum)

Output:

(3+2j)
Real number is  3.0
Imaginary number is  2.0

The conjugate() method can also be used to locate the conjugate of a complex number.

The conjugate() method returns the conjugate complex of a complex integer in the following format:

var1= complex(3,2)
conjugateNum= var1.conjugate()
print(var1)
print("Conjugate is", conjugateNum)

Output:

(3+2j)
Conjugate is (3-2j)

Fraction Numbers in Python

Using the fraction numbers module, fractions have been explicitly implemented in Python.

Import the fractions module using the import statement below to use the fraction data type in Python.

import  fractions

The Fraction() function of the fractions module can be used to define a fraction object using integers in Python.

Two integers are required as input.

The first input is regarded as the numerator, while the second input is regarded as the denominator.

After execution, the Fraction() function returns a numerator/denominator fraction object.

import fractions

var1 = fractions.Fraction(1,2)
print("var1 answer is",var1)
print(type(var1))

Output:

var1 answer is 1/2
<class 'fractions.Fraction'>

Using the Fraction() method, you may also convert a floating-point number to a fraction.

import fractions

var1 = fractions.Fraction(0.5)
print("var1 answer is",var1)
print(type(var1))

Output:

var1 answer is 1/2
<class 'fractions.Fraction'>

Mathematical Functions

The following list below is the Mathematical Built-in Functions that perform Mathematical calculations in Python.

No.Function & Returns ( description )
1abs(x)
The absolute value of x: the (positive) distance between x and zero.
2ceil(x)
The ceiling of x: the smallest integer not less than x
3cmp(x, y)
-1 if x: < y, 0 if x == y, or 1 if x > y
4exp(x)
The exponential of x:ex
5fabs(x)
The absolute value of x.
6floor(x)
The floor of x: the largest integer not greater than x
7log(x)
The natural logarithm of x, for x > 0
8log10(x)
The base-10 logarithm of x for x > 0.
9max(x1, x2,...)
The largest of its arguments: the value closest to positive infinity
10min(x1, x2,...)
The smallest of its arguments: the value closest to negative infinity
11modf(x)
The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.
12pow(x, y)
The value of x**y.
13round(x [,n])
x
 rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
14sqrt(x)
The square root of x for x > 0

If you require additional information, please refer to the Python Mathematical Functions Documentation.

Trigonometric Functions

The following list below is the Trigonometric Functions that perform Trigonometric calculations in Python.

No.Function and Description
1acos(x)
Return the arc cosine of x, in radians.
2asin(x)
Return the arc sine of x, in radians.
3atan(x)
Return the arc tangent of x, in radians.
4atan2(y, x)
Return atan(y / x), in radians.
5cos(x)
Return the cosine of x radians.
6hypot(x, y)
Return the Euclidean norm, sqrt(x * x + y * y).
7sin(x)
Return the sine of x radians.
8tan(x)
Return the tangent of x radians.
9degrees(x)
Converts angle x from radians to degrees.
10radians(x)
Converts angle x from degrees to radians.

Check the Python Documentation for additional details about Trigonometric Functions.

Mathematical Constants

The list below is the two Mathematical constants in Python.

NoConstant and Description
1pi
The mathematical constant pi.
2e
The mathematical constant e.

Random Numbers Functions in Python

The list below is the most commonly used Random Numbers Functions in Python.

This is used in simulations, games, security, testing, and privacy applications.

NoFunction and Description
1choice(seq)
A random item from a list, tuple, or string.
2randrange ([start,] stop [,step])
A randomly selected element from range(start, stop, step)
3random()
A random float r, such that 0 is less than or equal to r and r is less than 1
4seed([x])
Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
5shuffle(lst)
Randomizes the items in a list without moving them. Returns None.
6uniform(x, y)
A random float r, such that x is less than or equal to r and r is less than y

Python Documentation has additional information on Random Numbers.

Summary

In summary, you learned about Type conversion of Python Numbers, Mathematical Functions such as Trigonometric Functions, and Random Numbers functions.

I hope that this tutorial helped you understand how to use Numbers in Python.


Leave a Comment