Working with Python Numbers

Working with Python Numbers

5 mins read913 Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Nov 20, 2022 21:50 IST

In the last blog from the Python Tutorial blog series, you got introduced to the different data types in Python. In this blog on Python Numbers, we will focus on numbers datatype in Python and learn it in depth. You will understand what various operations you can perform on Python numbers.

2022_01_Python-Data-Types-4.jpg

The numbers data type is immutable. Meaning, if you try to change the value of a number type it will get stored in a new memory location. 

Are Python numbers immutable?

Python Code

 
# Checking the memory location of num using <id>
num = 10
print("Memory location of",num,"is", id(num))
# Changing the value stored in same variable
num = 20
print("Memory location of",num,"is", id(num))
Copy code

Python Output

The memory location of 10 is 94309023595296
The memory location of 20 is 94309023595616

From the above output, you can see that, when the value of the variable got updated the memory location (id) of the same variable got changed. In other words, the same variable is now pointing to a new memory location. Thus proving that yes, number data type in Python is immutable.

Types of Python Number

There are mainly three types of Python Number:

  • Integer
  • Floating-point Numbers/ Decimal point number
  • Complex Numbers

Let’s understand them better and let the code speak for itself:

Python code:

 
a = 10 # int
b = 3.14 # float
c = 10j # complex
Copy code

How to find the data type of a variable in Python?
Python Code:

 
a = 10 # integer in python
print("Data Type of ",a,"is ", type(a))
b = 3.14 # float
print("Data Type of ",b,"is ", type(b))
c = 10j # complex
print("Data Type of ",c,"is ", type(c))
Copy code

Python Output

Data Type of  10 is  <class 'int'>
Data Type of  3.14 is  <class 'float'>
Data Type of  10j is  <class 'complex'>

Changing the data type of Python Numbers

Type Conversion and Typecasting is explicitly changing the data type of a variable. We can change the type of python number using the following two methods:

  1. Type Conversion
  2. Type Casting

What is Type Conversion in Python?

Python defines type Conversion as the scenarios where the data type automatically gets converted to another data type during the compilation of the code. This conversion is done by the compiler if both data types are compatible with each other. 

For example:

Using Arithmetic Operations: We can use operations like addition, subtraction to change the type of number implicitly. In simple words, automatically converts one data type to another. This is also known as implicit type conversion.

Python code:

 
num1 = 10
num2 = 5.0
print (num1 + num2)
Copy code

Python Output:

15.0

Here as you can see, on adding num1(integer) and num2(float), the output is automatically converted to a float type.  

What is Typecasting?

The process in which a data type is converted to another by the coder while writing a program is known as typecasting or explicit type conversion. The programmer manually converts one data type into another while writing his/her code.

For example:

Using built-in functions: Using python built-in functions like int(), float() and complex() you can convert into different types explicitly.

Python code

 
num_1 = 10 #int
num_2 = 5.0 #float
num_3 = 10j #complex
print("Initial type of", num_1,"is", type(num_1))
print("Initial type of", num_2,"is", type(num_2))
print("Initial type of", num_3,"is", type(num_3))
#convert from int to float:
updated_num_1 = float(num_1)
#convert from float to int:
updated_num_2 = int(num_2)
#convert from complex to int:
updated_num_3 = complex(num_3)
print("Updated type of num_1 is", type(updated_num_1))
print("Updated type of num_2 is", type(updated_num_2))
print("Updated type of num_3 is", type(updated_num_3))
print("Updated Numbers: ",updated_num_1, updated_num_2, updated_num_3)
Copy code

Python Output:

Initial type of 10 is <class 'int'>
Initial type of 5.0 is <class 'float'>
Initial type of 10j is <class 'complex'>
Updated type of num_1 is <class 'float'>
Updated type of num_2 is <class 'int'>
Updated type of num_3 is <class 'complex'>
Updated Numbers:  10.0 5 10j

NOTE:
  • When you convert float to int, its decimal part is truncated 
  • You cannot convert a complex data type number into an int or float data type

Python Arithmetic Operators using Numbers

You can use various Arithmetic operators with numeric values to perform common mathematical operations. Let’s understand it with the below code.

Python code

 
num1 = 10
num2 = 5
# Addition
add = num1 + num2
print("Addition:",num1,"+",num2,"=",add)
# Subtraction
sub = num1 - num2
print("Subtraction:",num1,"-",num2,"=",sub)
# Division
div = num1 / num2
print("Division:",num1,"/",num2,"=",div)
# Multiplication
mul = num1 * num2
print("Multiplication:",num1,"*",num2,"=",mul)
# Remainder
rem = num1 % num2
print("Remainder:",num1,"%",num2,"=",rem)
# Exponential
exp = num1 ** num2
print(num1,"to the power of ",num2,"=",exp)
Copy code

Python output

Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Division: 10 / 5 = 2.0
Multiplication: 10 * 5 = 50
Remainder: 10 % 5 = 0
10 to the power of  5 = 100000

Decimal Numbers in Python

 
(3.2 - 3.0 == 0.2)
Copy code

Python Output:

False

You might be thinking about why Python gave an incorrect result in this case? Click here to learn more on Decimal Numbers in Python 

Using the Python inbuilt decimal library you can resolve the above issue.

Python code:

 
import decimal
num1 = decimal.Decimal('3.2')
num2 = decimal.Decimal('3.0')
print(num1-num2)
Copy code

Python Output

0.2

Fractions in Python

  • Fractions in python are used to create fractions from integers, floats, decimals and some other numeric values and strings
  • A fraction is created by specifying a pair of integers as numerator and denominator
  • You can use fraction functions by using fractions. Fraction class
  • The default value of the numerator is 0 and the denominator is 1
  • ZeroDivisionError is raised when the denominator is 0

Let’s learn python fractions with the below code:

Python Code:

 
from fractions import Fraction as frac
#Simply print the number as a fraction
# frac<numerator, denominator>
print(frac(1, 2))
print(frac(0, 5))
print(frac(10, 15))
Copy code

Output:

1/2
0
2/3

Python Code:
 
#Converting the float numbers to its fractional form
print(frac('-10.14'))
print(frac('3.14159265359'))
Copy code

Output:

-507/50
314159265359/100000000000

Click here to learn more about Fractions in Python

Random Numbers – Random Function in Python

Syntax:

random.randint(a,b)

This returns a random number N within the range of a,b (a <= N <= b), where the a, b are included within the range.

Python Code: 

Build a dice game – A program which generates a random number between 0 and 6 each time you run the code.

 
# importing the random module
import random
print(random.randint(0,6))
Copy code
Python output:
5

Note: Every time you execute the above code the output would be different in a range of 0 to 6. That’s just how we roll the dice. 

Python Math Functions

Python allows several math’s operations using the math module. You can access them just by importing the math library. Let’s see the following example:

 
#importing pre-defined math module
import math
print(math.pi)
print(math.cos(math.pi))
print(math.exp(10))
print(math.log10(1000))
print(math.sinh(1))
print(math.factorial(6))
Copy code
About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio