Python operator is used to performing different types of operations (arithmetic, logical, or types of computation) on variables or values. The variables or values are called operands.

For example

x=10

y=20

add=x+y

Here + is operator which carried arithmetic operation and x and y are called operands.

Python supports the following types of operators

- Arithmetic operators
- Logical (Boolean) operators
- Comparison(relational) operators
- Bitwise operators
- Assignment operators
- Special operators

**Arithmetic operators**

The arithmetic operator performs basic mathematical operations like addition, subtraction, multiplication, division, and others.

Operators | Description | Example |

Addition(+) | The addition operator add either side of operands | x+y |

Subtraction(-) | Subtract right operand from left operand | x-y |

Multiplication(*) | Multiply either side of the operands | x*y |

Divison(/) | Divide left operand from right operand | x/y |

Modulus(%) | divide left operand from the right and return the remainder | x%y |

Exponent(**) | Perform exponent(power) operation on left operand with power raised by right operand | x**y |

Example

x=20

y=9

add=x+y

print("Addition",add)

sub=(x-y)

print("Subtraction",sub)

mul=xyprint("Multiplication",mul)div=(x-y)print("Division",div)pow=(x*y)

print("Exponent",pow)

floor=(x//y)

print("FloorDiv",floor)Output

Addition 29

Subtraction 11

Multiplication 180

Division 11

Exponent 512000000000

FloorDiv 2

##### Logical Operator

The logical operator is used for performing logical AND, OR, and NOT operations.

Operator | Description | Example |

AND | Truth;- If both operands are TRUE | x and y |

OR | True:- If either of the operands is True | x or y |

NOT | True:- If the operand is False | not x |

Example

x=True

y=False

result_and=x and y

print("AND",result_and)

result_or= x or y

print("OR",result_or)

result_not=not x

print("NOT",result_not)Output

AND False

OR True

NOT False

##### Comparison(relational) operators

These types of operators are uses to comparing either side of the operand values. It returns output True or False.

Operator | Description | Example |

Equal (==) | if the value of both operands is equal then the condition becomes True else False | x==y |

Not Equal (!=) | if the value of both operands is not equal then the condition becomes True else False | x!=y |

Greater than (>) | If the left operand is greater than the right operand then the condition become True | x>y |

Less than (<) | If the left operand is less than the right operand then the condition become True | x<y |

Greater than equal to (>=) | If the left operand is greater than and equal to the right operand then the condition become True | x>=y |

Greater than equal to (<=) | If the left operand is less than and equal to the right operand then the condition become True | x<=y |

Example

equal=(x==y)

notequal=(x!=y)

greater=(x>y)

less=(x=y)

less_equal=(x<=y)

print("Equal to", equal)

print("Not equal to", notequal)

print("Greater than", greater)

print("Less than", less)

print("Less than equal to", greater_equal)

print("Less than equal to", less_equal)Output

Equal toFalse

Not equal toTrue

Greater thanTrue

Less thanFalse

Less than equal toTrue

Less than equal toFalse

##### Bitwise operator

Bitwise operator performs operations bit by bit, First, it converts integer value into binary format and then performs a logical operation on each bit of binary value.

Operator | Description | Example |

& | Bitwise AND | x&y |

| | Bitwise OR | x|y |

~ | Bitwise NOR | x~y |

^ | Bitwise XOR | x^y |

<< | Bitwise left shift | x<<y |

>> | Bitwise right shift | x>>y |

Example

x=20

y=15

bitand=x&y

print("bitwise AND", bitand)

bitor=x|y

print("bitwise OR", bitor)

bitnor=~x

print("bitwise NOT", bitnor)

bitxor=x^y

print("bitwise XOR", bitxor)

bitleft=x<>y

print("bitwise Right", bitright)

Output

bitwise AND 4

bitwise OR 31

bitwise NOT -21

bitwise XOR 27

bitwise Left 655360

bitwise Right 0

##### Assignment operator

Assignment operators are used in assigning a value to the variable but it can be in different ways.

Operator | Description | Equivalent |

= | Assing value to right side operand to left size operand | x=10 |

+= | It calculates addition first and then assigns value to the left operand | x=x+10 |

-= | It calculates subtraction first and then assigns value to the left operand | x=x-10 |

*= | It calculates multiplication first and then assigns value to the left operand | x=x*10 |

/= | It calculates division first and then assigns value to the left operand | x=x/10 |

%= | It takes modules first and thn assigns value to left operand | x=x%10 |

**= | It calculates exponent first and then assigns value to the left operand | x=x**10 |

//= | It calculates floor division first and then assigns value to the left operand | x=x//10 |

##### Special operators

There are also different types of operator called Membership operator and Identity operator.

Operator | Description | Example |

in(Membership operator) | Return True if element found in the specified sequence | 10 in x |

not in(Membership operator) | Return True if element not found in the specified sequence | 10 not in x |

is(Identity operator) | Return True if operands are identical | x is True |

is not(identity operator) | Return True if operand are not identical | x is not True |

## Leave a Reply

You must be logged in to post a comment.