0
1.4kviews
What are different math operators used in Python?

Describe all different types of math operators used in Python. Also give an example for each.

1 Answer
1
16views

Different operators used in Python are:

Arithmetic

1. Addition: Used to add to values.

for eg:  x+y or 3+5 = 8

2. Subtraction: Used to substract values.

for eg:  x-y or 5-3 = 2

3. Multiplication: Used to perform multiplication.

for eg:  x * y or 2 * 4 = 8

4. Division: Used to perform division.

for eg :  x / y or 13 / 4 = 4.333

5. Floor Division : Used to perform division & it rounds the result down to the nearest whole number.

for eg: x // y or 15 // 2 = 7

6. Modulus: Used to perform division & modulus returns the remainder after performing division.

for eg:  x % y or 5 % 2 = 1

7. Exponential: return 1st raised to 2nd no.

for eg:  x ** n or 2 ** 5 = 32

Assignment Operator

1. = Operator is used to assign value to the variable

for eg:  x = 5 (i.e. value of x is 5)

2. += operator is used to add the value then assign it to the variable

for eg:  x +=3 (i.e. x = x + 3, x = 5+3, x=8)

3. -= operator is used to substract the value then assign it to the variable

for eg:  x -=3 (i.e. x = x - 3   , x = 5 - 3 ,  x=2 )

4. *= operator is used to multiply the value then assign it to the variable

for eg:  x *=3 (i.e. x = x * 3 , x = 5 * 3 , x= 15)

5. /= operator is used to divide the value then assign it to the variable

for eg:  x /=3 (i.e. x = x / 3  , x = 5 / 3 , x = 1.666666666666667)

6. %= operator is used to divide the value then assign the remainder to the variable

for eg:  x %=3 (i.e. x = x % 3  , x = 5 % 3 , x = 2)

7. //= operator is used to divide the value then return the integer part of the quotient

for e.g:  x // = 3(i.e. x= x//3 , x = 5 // 3 , x = 1)

Comparison Operators

Consider value of x as 5 and y as 3

1. Equal: this operator is used to compare the values and return true if both the values are same or false if diffrent.

for e.g: print(x == y) 
Output: False as value of x and  y is different.

2. Not equal this operator is used to compare the values and return true if different or false if same.

for e.g:  print(x!=y)
Output:  True  as the value of x and y are different

3. Greater than this operator compares the value if the sign represents correctly then return true else false.

for e.g:  print( x > y) 
Output:  True as the value of x is greater then y

4. Less than this operator is used to compare the value if the sign represents correctly then return true else false.

for e.g:  print( x < y) 
Output:  False as the value of x is greater then y

5. Greater than or equal to is used to compare the value if the sign represents correctly then return true else false.

For e.g:  print( x > = y) 
Output:  True as the value of x is greater then y

6.Less than or equal to this operator is used to compare the value if the sign represents correctly then return true else false.

for e.g:  print( x < =  y) 
Output:  False as the value of x is greater then y

Logical Operator

consider x = 5

1. and Operator returns true of the both the conditions are true

for e.g:  print( x > 3 and x <6 )
Output:  True as value of x is in between 3 and 6

2. or Operator return true even if one of the statement is true

for e.g:  print( x > 6 or x < 10) 
Output:  True as value of x is less then 10

3. not Operator return false if the result is true

for e.g:  print(not(x >10 )) 
Output:  True as value of x is less then 10 the result would be false but operator would return opposite value of the result.

Identity Operators

Consider the x="Hello" y ="Hello"

1. is operator is used to check if both values are same then return true else return false.

for e.g:  print(x is y ) 
Output:  True as both the variable has the value as Hello

2. is not operator is used to check if both the values are different then return true else return false

for e.g:  print( x is not y) 
Output:  False as both the values are same the output would be false

Membership Operators

Consider the value of x = "Hello how are you ?"

1. in operator used to check if the particular string is a part of the values stored in the variable

for e.g:  print( "you" in x) 
Output:  True as you is present in the sentence which is stored in the variable x.

2. not in operator used to check if the particular string is not a part of the values stored in the variable

for e.g:  print( "Hi" in x) 
Output:  True as Hi is not a part of  the sentence which is stored in the variable x.

Bitwise Operator

Bitwise operator is used ti compare bits of a binary no

1. & (and) Operator is Sets each bit to 1 if both bits are 1

2. | (or) Operator Sets each bit to 1 if both bits are 1

3. ~ (NOT) Operator Inverts all the bits

4. ^ (XOR) Operator Sets each bit to 1 if only one of two bits is 1

5. >> (Signed right shift) Operator Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off.

6. << (Zero fill left shift) Operator Shift left by pushing zeros in from the right and let the leftmost bits fall off.

Please log in to add an answer.