Python Numbers

Balasaheb Garule

8 months ago

Python Numbers | insideAIML
Table of Content
Introduction
  • Python Int
  • Python Float
  • Python Complex Number
Type Conversion
Arithmetic Operations
  • Addition. 
  • Subtraction. 
  • Multiplication. 
  • Division
  • Integer Division
  • Exponents
  • The Modulus Operator
Summary

Introduction

          Numeric data is stored into numbers data type in python. Number data structures are immutable i.e. you never change the value of data type or if you change the value then a new object is created. It stores positive, negative, decimal, floating-point numbers, and complex numbers.
In python, there are following numeric data types are available
  • Integers 
  • Floating-point numbers
  • Complex Numbers

Python Int

          The integer in the python data type represents whole numbers. A number is positive or negative, the length of the number is unlimited. We can declare a very large integer value using the python int type.
e.g.    
n = 10000000000000000001

print(type(n))
Output 
<class 'int'>

Python Float

          Python float is a decimal data type. It will be used for declaring floating-point values. Values may be positive or negative.
e.g. 
fp = 3.14

print(type(fp))
Output
<class 'float'>

Python Complex Number

          The complex number contains two parts: the real and imaginary part with j.
We can access the real and imaginary parts with  a.real and  a.imag, respectively.
e.g.
c = 3+4j

print(type(c))

print(c.real)

print(c.imag) 
Output
<class 'complex'>

3.0

4.0

Type Conversion

          We can convert one type of variable into other types of variable. The complex type of variable is not converted into any other type. It is also known as typecasting. For this we use int(), float(), str(), and complex() methods.type conversion between float to integer remove the decimal value from float value.
e.g.
	p = 10    # int

q = 3.14  # float

r = 2+4j   # complex

print(“convert from python int to float”)

x = float(p)

print(type(x))

print(x)

print(“convert from python float to int”)

y = int(q)

print(type(y))

print(y)

print(“convert from python int to complex”)

z = complex(p)

print(type(z))

print(z)

print(“convert from python int to string”)

s = str(p)

print(type(s))

print(s)
Output
convert from python int to float

<class 'float'>

10.0

convert from python float to int

<class 'int'>

3

convert from python int to complex

<class 'complex'>

(10+0j)

convert from python int to string

<class 'str'>

10

Arithmetic Operations

Addition

          Adding variable values using the “+” operator. Addition performs on same as well as a different type of variable. The addition of int and float is always a return a python float value.
#python program to add two numbers

# integer number addition

num1 = 10

num2 = 20

add = num1+ num2 

print(“Integer  Addition : ”,add)

# float number addition

num1 = 10

num2 = 2.5

add = num1 + num2   

print(“Float  Addition : ”,add)

#Complex number  addition

num1 = 5+3j

num2 = 2

add = num1 + num2   

print(“Complex  Addition : ”,add)

# Real and imaginary parts are added to get the result.
Output
Integer  Addition :  30

Float  Addition :  12.5

Complex  Addition :  (7+3j)

Subtraction

          Subtract variable value using “-” operator. Subtraction performs on the same as well as a different type of variable. Subtraction of int and float is always returned a python float value.
#python program to subtract two numbers

# integer number Subtraction

num1 = 10

num2 = 20

sub = num1- num2 

print(“Integer  Subtraction: ”,sub)

# float number Subtraction

num1 = 10

num2 = 2.5

sub = num1 - num2   

print(“Float  Subtraction: ”,add)

#Complex number  Subtraction

num1 = 5+3j

num2 = 2

sub = num1 - num2   

print(“Complex  Subtraction: ”,sub)

# Real and imaginary parts are Subtract to get the result.
Output
Integer  Subtraction :   -10

Float  Subtraction :   7.5

Complex  Subtraction :   (3+3j)

Multiplication

          Multiplication is done using the “*” operator. Multiplication performs on the same as well as different types of variables. Multiplication of int and float is always returned a python float value.
#python program to Multiplication two numbers

# integer number Multiplication

num1 = 10

num2 = 20

mul = num1- num2 

print(“Integer  Multiplication: ”,mul)

# float number Multiplication

num1 = 10

num2 = 2.5

mul = num1 - num2   

print(“Float  Multiplication: ”, mul)

#Complex number Multiplication

num1 = 5+3j

num2 = 2

mul = num1 - num2   

print(“Complex  Multiplication: ”, mul)

# Real and imaginary parts are multiply by num2 value to get the result.
Output
Integer  Multiplication :   200

Float  Multiplication :   25.0

Complex  Multiplication :   (10+6j)

Division

          Division operation is done using the “/” operator. The division performs on the same as well as different types of variables. Division operation always returns a float value.
#python program to Division two numbers

# integer number Division

num1 = 10

num2 = 20

div = num1/ num2 

print(“Integer  Division: ”,div)

# float number Division

num1 = 10

num2 = 2.5

div = num1 / num2   

print(“Float  Division: ”, div)

#Complex number Division

num1 = 5+3j

num2 = 2

div = num1 / num2   

print(“Complex  Division: ”, div)

# Real and imaginary parts are divide by num2 value to get the result.
Output
Integer  Division :   0.5

Float  Division :   4.0

Complex  Division :   (2.5+1.5j)

Integer Division

          To convert division operation value into integer use integer division.to perform this operation uses the “//” operator.it is also called a floor division operator.
e.g.
n = int(5.0/2)

print(n)
output
2
similar to
n = 5 // 2

print(n)
output
2

Exponents

          To calculate exponent use  the “**” operator.
e.g.
n = 3**2

print(n)
output
9
m = 3 ** -1

print(m)
Output
0.3333333333333333

The Modulus Operator

          The modulus operator (%) returns a reminder of two values.  
e.g.
r = 5 % 2

print(r)
output
1

Summary 

          In this blog, we learned about python numbers. Int(), float(), and complex() are the python number data types. We also learn about arithmetic operation, integer division, exponents, and modulus operators. 
If you have any doubt about insideAIML’s python numbers blog, drop a comment below and we will get back to you.
   
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live discussion forum.
Keep Learning. Keep Growing. 

Submit Review