Open In App

Python Numbers

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, “Numbers” is a category that encompasses different types of numeric data. Python supports various types of numbers, including integers, floating-point numbers, and complex numbers. Here’s a brief overview of each:

Python Integer

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

Python
num = -8

# print the data type 
print(type(num))

Output:

<class 'int'>

Example 2: Performing arithmetic Operations on int type

Python
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

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

Python
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.

Python
num = 6 * 7.0

print(type(num))

Output:

<class 'float'>

Example 2: Performing arithmetic Operations on the float type

Python
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

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

Python
num = 6 + 9j

print(type(num))

Output:

<class 'complex'>

Example 2: Performing arithmetic operations on complex type

Python
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

Python
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

Python
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.

Python
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.

Python
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.

Related Article – floor() and ceil() function Python

Let’s see the above two examples and try to solve them using the decimal number.

Example:

Python
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



Previous Article
Next Article

Similar Reads

Convert Strings to Numbers and Numbers to Strings in Python
In Python, strings or numbers can be converted to a number of strings using various inbuilt functions like str(), int(), float(), etc. Let's see how to use each of them. Example 1: Converting a Python String to an int: C/C++ Code # code # gfg contains string 10 gfg = &quot;10&quot; # using the int(), string is auto converted to int print(int(gfg)+2
2 min read
Python3 Program to Rotate all odd numbers right and all even numbers left in an Array of 1 to N
Given a permutation arrays A[] consisting of N numbers in range [1, N], the task is to left rotate all the even numbers and right rotate all the odd numbers of the permutation and print the updated permutation. Note: N is always even.Examples:  Input: A = {1, 2, 3, 4, 5, 6, 7, 8} Output: {7, 4, 1, 6, 3, 8, 5, 2} Explanation: Even element = {2, 4, 6
2 min read
Python program to check if the list contains three consecutive common numbers in Python
Our task is to print the element which occurs 3 consecutive times in a Python list. Example : Input : [4, 5, 5, 5, 3, 8] Output : 5 Input : [1, 1, 1, 64, 23, 64, 22, 22, 22] Output : 1, 22 Approach : Create a list.Create a loop for range size – 2.Check if the element is equal to the next element.Again check if the next element is equal to the next
3 min read
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate random numbers through the random module. Functions in the random module rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0. These particular type of functions is used in a lot of games, lotteries, or any appl
5 min read
Python Numbers | choice() function
choice() is an inbuilt function in Python programming language that returns a random item from a list, tuple, or string. Syntax: random.choice(sequence) Parameters: sequence is a mandatory parameter that can be a list, tuple, or string. Returns: The choice() returns a random item. Note:We have to import random to use choice() method. Below is the P
1 min read
Python | Numbers in a list within a given range
Given a list, print the number of numbers in the given range. Examples: Input : [10, 20, 30, 40, 50, 40, 40, 60, 70] range: 40-80 Output : 6 Input : [10, 20, 30, 40, 50, 40, 40, 60, 70] range: 10-40 Output : 4 Multiple Line Approach:Traverse in the list and check for every number. If the number lies in the specified range, then increase the counter
6 min read
Python map function | Count total set bits in all numbers from 1 to n
Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n = 3 Output: 4 Binary representations are 1, 2 and 3 1, 10 and 11 respectively. Total set bits are 1 + 1 + 2 = 4. Input: n = 6 Output: 9 Input: n = 7 Output: 12 Input: n = 8 Output: 13 We have existing solution for t
2 min read
Python Dictionary | Check if binary representations of two numbers are anagram
Given two numbers you are required to check whether they are anagrams of each other or not in binary representation. Examples: Input : a = 8, b = 4 Output : YesBinary representations of bothnumbers have same 0s and 1s.Input : a = 4, b = 5Output : NoCheck if binary representations of two numbersWe have existing solution for this problem please refer
3 min read
Python | Generate random numbers within a given range and store in a list
Given lower and upper limits, Generate random numbers list in Python within a given range, starting from 'start' to 'end', and store them in the list. Here, we will generate any random number in Python using different methods. Examples: Input: num = 10, start = 20, end = 40 Output: [23, 20, 30, 33, 30, 36, 37, 27, 28, 38] Explanation: The output co
5 min read
Replacing strings with numbers in Python for Data Analysis
Sometimes we need to convert string values in a pandas dataframe to a unique integer so that the algorithms can perform better. So we assign unique numeric value to a string value in Pandas DataFrame. Note: Before executing create an example.csv file containing some names and gender Say we have a table containing names and gender column. In gender
3 min read
Practice Tags :
three90RightbarBannerImg