Operators Cheatsheet

Complete Python operators cheatsheet with clear examples for quick revision

Arithmetic Operators

1. Addition operators ( + ) -----> Used to perform addition

a=10
b=20
c=a+b # 10+20=30
print(c)

2. Subtraction Operators ( - ) -----> Used to perform subtraction

x=25
y=15
z=x-y # 25-15=10
print(z)

3. Multiplication Operators ( * ) -----> Used to perform multiplication

a=5
b=20
c=a*b # 5*20=100
print(c)

# Note: * works differently for sequences like string, list, etc. , which will be discussed later.

4. Division Operators ( / ) -----> Always gives output in float data type

5. Floor Division ( // ) -----> Always gives output in Integer data type

6. Exponent (Raise to the power operator) ( ** ) -----> Left side value must be Base whereas Right side value must be Power

7. Modulus Operator ( % ) -----> It returns the Remainder after the division of two operands. It always gives output in Integer data type

Assignment Operators

1. Assignment Operator ( = ) -----> It assigns the value of the right expression to the Left operand

2. Addition Assignment Operators ( += )

3. Subtraction Assignment Operators ( -= )

4. Multiplication Assignment operators ( *= )

5. Division Assignment operators ( /= )

6. Floor Division operators ( /= )

7. Exponentiation Assignment Operators ( **= )

Comparison Operators

Used to compare values and return True or False

1. Greater Than ( > ) ------> Returns True if Left value if greater than Right value

2. Greater than or Equals to ( >= ) -----> Returns True if the Left value is greater than or Equal to the Right value

3. Smaller than ( < ) -----> Returns True if Left value is Less than Right value

4. Smaller than Equals to ( <= ) -----> Returns True if the Left value is Less than or Equal to the Right value

5. Equals to ( == ) -----> Returns True if two values are equal

6. Not Equals to ( != ) -----> Returns True if Two values are not equals

Logical Operators

  1. and operator

  2. or operator

  3. not operator

1. and Operator -----> Returns True if both conditions are True

2. or Operator -----> Returns True if at least one condition is True

3. not Operator -----> It returns True if the condition is False, and returns False if the condition is True

Priority Table of Operators:

operator

( ) Parentheses

∗∗ Exponent

+,- Addition , Subtraction

==, !=, >, >=, <, <= Comparison Operator

Logical NOT

Logical AND

Logical OR

NOTE:

If there are operators with same priority,then they will be evaluated from left to right.

Last updated