Open In App

Python Numbers

Last Updated : 22 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

Different types of Number data types are :

  • int
  • float
  • complex

Python Int  type

Python int is the whole number, including negative numbers but not fractions. In Python, there is no limit to how long an integer value can be.

Example 1: Creating int and checking type

Python3




num = -8
 
# print the data type
print(type(num))


Output:

<class 'int'>

Example 2: Performing arithmetic Operations on int type

Python3




a = 5
b = 6
 
# Addition
c = a + b
print("Addition:",c)
 
d = 9
e = 6
 
# Subtraction
f = d - e
print("Subtraction:",f)
 
g = 8
h = 2
 
# Division
i = g // h
print("Division:",i)
 
j = 3
k = 5
 
# Multiplication
l = j * k
print("Multiplication:",l)
 
m = 25
n = 5
 
# Modulus
o = m % n
 
print("Modulus:",o)
 
p = 6
q = 2
 
# Exponent
r = p ** q
print("Exponent:",r)


Output:

Addition: 11
Subtraction: 3
Division: 4
Multiplication: 15
Modulus: 0
Exponent: 36

Python Float type 

This is a real number with a floating-point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation. . Some examples of numbers that are represented as floats are 0.5 and -7.823457.

They can be created directly by entering a number with a decimal point, or by using operations such as division on integers. Extra zeros present at the number’s end are ignored automatically.

Example 1: Creating float and checking type

Python3




num = 3/4
 
# print the data type
print(type(num))


Output:

<class 'float'>

As we have seen, dividing any two integers produces a float. A float is also produced by running an operation on two floats, or a float and an integer.

Python3




num = 6 * 7.0
 
print(type(num))


Output:

<class 'float'>

Example 2: Performing arithmetic Operations on the float type

Python3




a = 5.5
b = 3.2
 
# Addition
c = a + b
print("Addition:", c)
 
# Subtraction
c = a-b
print("Subtraction:", c)
 
# Division
c = a/b
print("Division:", c)
 
# Multiplication
c = a*b
print("Multiplication:", c)


Output

Addition: 8.7
Subtraction: 2.3
Division: 1.71875
Multiplication: 17.6

Note: The accuracy of a floating-point number is only up to 15 decimal places, the 16th place can be inaccurate.

Python Complex type 

A complex number is a number that consists of real and imaginary parts. For example, 2 + 3j is a complex number where 2 is the real component, and 3 multiplied by j is an imaginary part.

Example 1: Creating Complex and checking type

Python3




num = 6 + 9j
 
print(type(num))


Output:

<class 'complex'>

Example 2: Performing arithmetic operations on complex type

Python3




a = 1 + 5j
b = 2 + 3j
 
# Addition
c = a + b
print("Addition:",c)
 
d = 1 + 5j
e = 2 - 3j
 
# Subtraction
f = d - e
print("Subtraction:",f)
 
 
g = 1 + 5j
h = 2 + 3j
 
# Division
i = g / h
print("Division:",i)
 
 
j = 1 + 5j
k = 2 + 3j
 
# Multiplication
l = j * k
print("Multiplication:",l)


Output:

Addition: (3+8j)
Subtraction: (-1+8j)
Division: (1.307692307692308+0.5384615384615384j)
Multiplication: (-13+13j)

Type Conversion in Python

We can convert one number into the other form by two methods:

Using Arithmetic Operations: 

We can use operations like addition, and subtraction to change the type of number implicitly(automatically), if one of the operands is float. This method is not working for complex numbers.

Example: Type conversion using arithmetic operations

Python3




a = 1.6
b = 5
 
c = a + b
 
print(c)


Output:

6.6

Using built-in functions

We can also use built-in functions like int(), float() and complex() to convert into different types explicitly.

Example: Type conversion using built-in functions

Python3




a = 2
print(float(a))
 
b = 5.6
print(int(b))
 
c = '3'
print(type(int(c)))
 
d = '5.6'
print(type(float(c)))
 
e = 5
print(complex(e))
 
f = 6.5
print(complex(f))


Output:

2.0
5
<class 'int'>
<class 'float'>
(5+0j)
(6.5+0j)

When we convert float to int, the decimal part is truncated. 

Note: 

  1. We can’t convert a complex data type number into int data type and float data type numbers.
  2. We can’t apply complex built-in functions on strings.

Decimal Numbers in Python

Arithmetic operations on the floating number can give some unexpected results. 

Example 1:

Let’s consider a case where we want to add 1.1 to 2.2. You all must be wondering why the result of this operation should be 3.3 but let’s see the output given by Python.

Python3




a = 1.1
b = 2.2
c = a+b
 
print(c)


Output:

3.3000000000000003

Example 2:

You can the result is unexpected. Let’s consider another case where we will subtract 1.2 and 1.0. Again we will expect the result as 0.2, but let’s see the output given by Python.

Python3




a = 1.2
b = 1.0
c = a-b
 
print(c)


Output:

0.19999999999999996

You all must be thinking that something is wrong with Python, but it is not. This has little to do with Python, and much more to do with how the underlying platform handles floating-point numbers. It’s a normal case encountered when handling floating-point numbers internally in a system. It’s a problem caused when the internal representation of floating-point numbers, which uses a fixed number of binary digits to represent a decimal number. It is difficult to represent some decimal numbers in binary, so in many cases, it leads to small roundoff errors. 

In this case, taking 1.2 as an example, the representation of 0.2 in binary is 0.00110011001100110011001100…… and so on. It is difficult to store this infinite decimal number internally. Normally a float object’s value is stored in binary floating-point with a fixed precision (typically 53 bits). So we represent 1.2 internally as,

1.0011001100110011001100110011001100110011001100110011  

Which is exactly equal to :

1.1999999999999999555910790149937383830547332763671875

For such cases, Python’s decimal module comes to the rescue. As stated earlier the floating-point number precision is only up to 15 places but in the decimal number, the precision is user-defined. It performs the operations on the floating-point numbers in the same manner as we learned in school. Let’s see the above two examples and try to solve them using the decimal number.

Example:

Python3




import decimal
 
a = decimal.Decimal('1.1')
b = decimal.Decimal('2.2')
 
c = a+b
print(c)


Output

3.3

We can use a decimal module for the cases – 

  • When we want to define the required accuracy on our own
  • For financial applications that need precise decimal representations

Note: For more information about decimal numbers in Python and the functions provided by this module, refer to Decimal Functions in Python

Random Numbers in Python

Python provides a random module to generate pseudo-random numbers. This module can create random numbers, select a random element from a sequence in Python, etc.

Example 1: Creating random value

Python3




import random
 
print(random.random())


Output

0.9867200671824407

Example 2: Selecting a random element from a string or list

Python3




import random
 
s = 'geeksforgeeks'
L = [1, 2 ,3, 5, 6, 7, 7, 8, 0]
print(random.choice(s))
print(random.choice(L))


Output

f
0

Note: For more information about random numbers, refer to our Random Number tutorial

Python Math module

The math module of Python helps to carry different mathematical operations trigonometry, statistics, probability, logarithms, etc. 

Python3




# importing "math" for mathematical operations
import math
 
a = 3.5
 
# returning the ceil of 3.5
print ("The ceil of 3.5 is : ", end="")
print (math.ceil(a))
 
# returning the floor of 3.5
print ("The floor of 3.5 is : ", end="")
print (math.floor(a))
 
# find the power
print ("The value of 3.5**2 is : ",end="")
print (pow(a,2))
 
# returning the log2 of 16
print ("The value of log2 of 3.5 is : ", end="")
print (math.log2(a))
 
# print the square root of 3.5
print ("The value of sqrt of 3.5 is : ", end="")
print(math.sqrt(a))
 
# returning the value of sine of 3.5
print ("The value of sine of 3.5 is : ", end="")
print (math.sin(a))


Output

The ceil of 3.5 is : 4
The floor of 3.5 is : 3
The value of 3.5**2 is : 12.25
The value of log2 of 3.5 is : 1.8073549220576042
The value of sqrt of 3.5 is : 1.8708286933869707
The value of sine of 3.5 is : -0.35078322768961984

Note: For more information about Python’s math module, refer to our math module tutorial.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads