Sayantan's Blog On Python Programming

PYTHON ARITHMETIC OPERATORS

PYTHON ARITHMETIC OPERATORS

ADDITION OPERATORS (+):

Addition operators are used for adding two or more numbers, either integer or floating point numbers.

INTEGER ADDITION:

# Arithmetic operator example

a = 9
b = 18
c = a + b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 9

Value of b -> 18

Value of c -> 27

FLOATING ADDITION:

# Arithmetic operator example

a = 6.5
b = 18.9
c = a + b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6.5

Value of b -> 18.9

Value of c -> 25.4

SUBTRACTION OPERATORS (-):

Subtraction operators are used to extract the difference between two or more numbers, either integer or floating point numbers.

INTEGER SUBTRACTION:

# Arithmetic operator example

a = 6
b = 18
c = 2
d =  b - a - c

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)
print("\nValue of d ->", d)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6

Value of b -> 18

Value of c -> 2

Value of d -> 10

FLOATING SUBTRACTION:

# Arithmetic operator example

a = 6.5
b = 18.9
c = b - a

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6.5

Value of b -> 18.9

Value of c -> 12.399999999999999

Floating has a large precision part. That’s why instead of 12.4, its showing as 12.399999999999999

MULTIPLICATION OPERATORS (*):

Multiplication operators are used to multiply two or more numbers, either integer or floating point numbers.

INTEGER MULTIPLICATION:

# Arithmetic operator example

a = 6
b = 18
c = a * b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6

Value of b -> 18

Value of c -> 108

FLOATING MULTIPLICATION:

# Arithmetic operator example

a = 6.2
b = 18.1
c = a * b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6.2

Value of b -> 18.1

Value of c -> 112.22000000000001

Floating has a large precision part. That’s why instead of 112.22, its showing as 112.22000000000001

DIVISION OPERATORS (/):

Division operators are used to divide two or more numbers, either integer or floating point numbers.

INTEGER DIVISION:

# Arithmetic operator example

a = 6
b = 18
c = b / a

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6

Value of b -> 18

Value of c -> 3.0

Although both a and b are integers but the result is a floating point number. So the division of any two or more integer numbers will results a floating number.

FLOATING DIVISION:

# Arithmetic operator example

a = 6.3
b = 18.6
c = b / a

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nValue of c ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6.3

Value of b -> 18.6

Value of c -> 2.9523809523809526

EXPONENTIATION OPERATORS (**):

Exponentiation operators returns the exponential result of an integer or floating point number. e.g. Square, cube etc.

INTEGER EXPONENTIATION:

# Arithmetic operator example

a = 6
b = 2
c = a**b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nSquare of a ->", c)

b = 3
c = a**b

print("\nValue of b ->", b)
print("\nCube of a ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6

Value of b -> 2

Square of a -> 36

Value of b -> 3

Cube of a -> 216

FLOATING EXPONENTIATION:

# Arithmetic operator example

a = 6.2
b = 2
c = a**b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nSquare of a ->", c)

b = 3
c = a**b

print("\nValue of b ->", b)
print("\nCube of a ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 6.2

Value of b -> 2

Square of a -> 38.440000000000005

Value of b -> 3

Cube of a -> 238.32800000000003

FLOOR DIVISION OPERATORS (//):

It returns the quotient part, rounded to the next smallest whole number as a result of two integer or floating division.

INTEGER FLOOR DIVISION:

# Arithmetic operator example

a = 9
b = 2
c = a//b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nFloor Division of a and b ->", c)

b = 5
c = a//b

print("\nValue of b ->", b)
print("\nFloor Division of a and b ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 9

Value of b -> 2

Floor Division of a and b -> 4

Value of b -> 5

Floor Division of a and b -> 1

FLOATING FLOOR DIVISION:

# Arithmetic operator example

a = 9.5
b = 2.2
c = a//b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nFloor Division of a and b ->", c)

b = 5.1
c = a//b

print("\nValue of b ->", b)
print("\nFloor Division of a and b ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 9.5

Value of b -> 2.2

Floor Division of a and b -> 4.0

Value of b -> 5.1

Floor Division of a and b -> 1.0

MODULUS OPERATORS (%):

Modulus operator returns the reminder part as a result of two integer or floating division.

INTEGER MODULUS OPERATION:

# Arithmetic operator example

a = 9
b = 2
c = a%b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nModulus operation of a and b ->", c)

b = 5
c = a%b

print("\nValue of b ->", b)
print("\nModulus operation of a and b ->", c)

PYTHON ARITHMETIC OPERATORS : Output

Value of a -> 9

Value of b -> 2

Modulus operation of a and b -> 1

Value of b -> 5

Modulus operation of a and b -> 4

FLOATING MODULUS OPERATION:

# Arithmetic operator example

a = 9.2
b = 2.3
c = a%b

print("\nValue of a ->", a)
print("\nValue of b ->", b)
print("\nModulus operation of a and b ->", c)

b = 5
c = a%b

print("\nValue of b ->", b)
print("\nModulus operation of a and b ->", c)

PYTHON ARITHMETIC OPERATORS : Output

PYTHON ARITHMETIC OPERATORS : Output

RELATED TOPICS:

2 thoughts on “PYTHON ARITHMETIC OPERATORS”

  1. Pingback: PYTHON LOGICAL OPERATORS - Sayantan's Blog On Python Programming

  2. Pingback: PYTHON IDENTIFY OPERATORS - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *