python math operators

Math operators:

  • In python, we have various arithmetic operators.
  • These are called math operators because they perform different types of mathematical operations.
  • Here  we have seven types of arithmetic operators:
  1. +     addition
  2. -      subtraction.
  3. /       division.
  4. %     modulus.
  5. *       multiplication.
  6. //       floor division.
  7. **      exponential.
1.Addition(+): It is used to add two or more operands.
It is represented by the "+". symbol.
syntax:(a+b).

2.subtraction(-): It subtracts the value of the  one operand to other operands 
It is represented by the symbol "-".
syntax:(a-b).

3.Division(/): It divides one operand with another operand and gives the remainder. The result is always in float type only.
It is represented by the symbol "/'.
syntax:(a/b).

4.modulus(%): It is the remainder of the operands.
It is represented by the symbol "%".
syntax:(a%b).

5.Multiplication(*): It multiply the two or more operands.
It is represented by the symbol "*".
syntax:(a*b).

6.floor division(//): It divides the operands and get the result in the whole number only.
It is represented by the symbol "//'.
syntax:(a//b).

7.exponential(**): It is used to calculate the power of the values.
It is represented by the symbol "**'.
syntax:(a**b).


Example: write a python program for integers using math operators.

  1. x=3
  2. y=4
  3. print(x+y)
  4. print(x-y)
  5. print(x/y)
  6. print(x%y)
  7. print(x*y)
  8. print(x//y)
  9. print(x**y)
output:     7
               -1
               0.75
                3
               12
               0
               81




python division operator

  • The division operator in python is used to perform the division operations.
  • It divides the left-side values with the right-side value and gives the remainder.
  • In other languages, we have only one division operator but in python, we have 2 division operators.
  1. division.
  2. floor division
 1.Division: In division when we divide values we get the result in floating type.
It is denoted by the symbol "/".
 syntax:(x/y).

Example: write a python code for integers using a division operator.

  1. x=20
  2. y=4
  3. print(x/y)
output:5.0



2.floor division(//):  In the floor division when we divide the values we get the whole number only.
It is denoted by the symbol "//".
syntax:(x//y).

Example: write a python program for integer values using floor division.

  1. x=20
  2. y=4
  3. print(x//y)
output: 5















Python modulo operator


In Python, the modulo operator is represented by the percent sign (%). It is used to find the remainder of a division operation between two numbers. 
Here's an example of  modulo operator In Python

x = 7
y = 3
remainder = x % y
print(remainder)

This will print 1 as the remainder of 7 divided by 3 is 1.
modulo operator to check if a number is even or odd:


x = 7
if x % 2 == 0:
    print(x, "is even")
else:
    print(x, "is odd")
This will print "7 is odd" because the remainder of 7 divided by 2 is 1.

The modulo operator can also be used to wrap around a range of values. For example, you can use it to ensure that an index is always within the bounds of an array, even if the index is negative or greater than the size of the array:


index = -1
size = 5
correct_index = index % size
print(correct_index)

This will print 4 as the correct index, because -1 % 5 = -1, but we want the index to be between 0 and 4.

It's worth noting that, the modulo operation is defined as the remainder of the division of one number by another, i.e x % y = x - y * floor(x/y)
where floor is the floor division operator, that returns the quotient of the division and also discards any remainder.

  • Modulo operator:  The modulo is defined as "the remainder of the two arguments ".
  • It is denoted by the symbol   "%".
  • When we divide any number with 0 i.e;    x%0 the result is zero division error.
  • We can use int, float, and double values also.
  • Those int, float, double may be either positive or negative.
syntax:(x%y)
x is the first argument and y is the second argument.

#1.write a python program using the modulo operator.

  1. x=10.0
  2. y=2.0
  3. print(x%y)
output: 0.0


#2.write a python program for a value divides with 0 using the modulo operator.
  1. x=10.0
  2. y=0
  3. print(x%y)
output: zero division error


Python bitwise operators

  • Bitwise operator:  In python, the bitwise operator is used to performing the bitwise calculations on integers.
  • It can apply to boolean type also.
  • It will be converted into binary numbers and bitwise operations in integers first on that binary number.
  • As it performs the bit to bit operations the name is called a bitwise operator.
  • There are six types of bitwise operators in python they are:
  1. &   And.
  2. |      or.
  3. ^      xor.
  4. ~     bitwise not
  5. >>   right shift.
  6. <<   left shift.
1.And(&): If both bits are then it returns true, else false.
It is denoted by the "&" symbol.
syntax: (x&y).

Example: write a python program for integer values using And operator.

  1. x=4
  2. y=5
  3. print(x&y)
output: 4


2. or(|):  If any one of the bits is true then the result is true, else false.
           It is denoted by the symbol "|".
syntax:(x|y).

Example: write a python program for integer values using or. 

  1. x=10
  2. y=5
  3. print(x|y)
output: 15


3. xor(^): when both bits are different returns true, else false.
It is denoted by the "^" symbol.
syntax:(x^y)

Example: write a python code for integers using xor operator.

  1. x=1
  2. 2
  3. print(x^y)
output: 3


4. Bitwise not (~): It is the complement of the bit if the bit is 1 if the result is 0, if the bit is 0 then it result is 1.
It is denoted by the "~" symbol.
syntax:(~x)

Example: write a python code for bool type using bitwise not operator.

  1. x=true
  2. print(~true)
output: -2.



5.Right shift(>>): It shifts one bit right and fills the left side vacant cells with significance values.
It is denoted by ">>".
syntax:(x>>).

Example: Write a python program using the right shift operator.

  1. x=11
  2. y=2
  3. print(x>>y).
output: 2



6.Left shift(<<): It shifts one bit left and the right side vacant cells fill with 0.
It is denoted by the "<<" symbol.
syntax:(x<<)

Example: write a python code for int values using the left shift operator.

  1. x=11
  2. y=2
  3. print(x<<y)
output:44


python comparison operators

  • In python, the comparison operator is used to compare one value with the other value.
  • A comparison operator can be known as a relational operator as it shows the relation between the two values.
  • It returns the result in bool type i.e True or False.
  • It is applicable for string, boolean, numbers(int type).
  • In the comparison, if one comparison is true then the result will be true, and if one comparison is false all the results will be false.
  • Eg; 10<20>30, the result is false.
  • We have six different types of comparison operators/ relational operators
  1. ==  Equal to.
  2. != not equal to.
  3. >  Greater than.
  4. <   Less than.
  5. >= Greater than or Equal to.
  6. <= Less than or equal to.
  1. Equal to(==): If the values on both sides of operands are equal then the result is true, else false.
        Equal operator(==)  always check the content comparison. 
 syntax: (a==b).

Example: write a python code for comparison/relational operator using equal to the operator for string type.

  1. x='AA'
  2. y='aa'
  3. print(x==y)
output: false


2.     Not Equal to (!=): If both values in operands are not equal it results in true, else false.
syntax: (a!=b).

Example: write a python program using the not equal operator for an int data type.

  1. x=10
  2. y=20
  3. if(x!=y):
  4. print("x not equal to y")
output: x not equal to y.



3. Less than(<): If the value left operand is less than the right operand we use less than.
syntax: (a<b).

Example: write a python program using less than operator for string type.

  1.  x="a"
  2. y="A"
  3. print(x<y)
output: false.



4. Greater than(>):  If the value of the left operand is greater than the right operand then the result is true. 
syntax: (a>b).

Example: write a python program for string data type using greater than the operator.

  1. x="a"
  2. y="A"
  3. print(x>y)
output: true

5.Less than or equal to(<=): If the value in the left operand is less than or equal to right to operand it returns a true value.
syntax:(x<=y).

Example: write a python code for int data type using less than or equal to operator.

  1. x=100
  2. y=200
  3. if(x<=y):
  4. print("x less than or equal to y")
output: x less than or equal to y.


6. Greater than or equal to(>=): If the values in the left operand are greater than or equal to the right operand then the result is true else false.
syntax: (a>=b).

Example: write a python program for int data type using >= operator.

  1. 1000
  2. 200
  3. if(x>=y):
  4. print("x is greater than or equal to y")
output: x is greater than or equal to y.

Logical operators in python

  • In python, we have the logical operator.
  • "Logical operator" used to perform the different logical operations on the value of variables.
  • The logical operators are applicable for boolean and non-boolean types.
  • Boolean types are:
  • True, False
  • non-boolean types are :
  1. 0
  2. 1
  3. empty string. 
  • Here True means 1 and Flase means 0.      
  • There are three different types of logical operators, they are :
  1. And
  2. or
  3. Not
  •   And operator: And operator is used when both arguments are True then the result is True.
  • syntax:  x and y

Example: write a python code for And operator.

1.a=10
2.b=20
3.print(a>30 And b<30)
4.False



  •  or operator: or operator is used when at least any one of the arguments is true then the result will be True.
  • syntax: x or y

Example: Write a python code using the or operator.

  1. a=2
  2. b=5
  3. print(a<3 or b>)



  • not operator: not operator is the opposite to the result of an argument, if it is true then the result is False and vice-versa.
  • syntax: x not y

Example: write a python code using the not operator.

  1. x=5
  2. y=10
  3. print(not x==y)
  4. True

Select Menu