Python Operators

Operators perform operations on variables and values.

1. Arithmetic Operators:

Operator Description Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division (float) x / y
// Floor Division( integer) x // y
% Modulus x % y
** Exponentiation x ** y
x = 10
y = 3
print(x + y) #13
print(x - y) #7
print(x * y) # 30
print(x / y) # 3.3333
print(x // y) #3 Integer division
print(x % y) #1 Modulus/Remainder
print(x ** y) #1000 Exponent

2. Comparison Operators (Relational Operators)

Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
x=5
y=10
print(x == y) #False
print(x != y) # True
print(x>y) #False
print(x print(x >= y) # False
print(x <= y) # True

3. Assignment Operators

Operator Description Example
= Assignment x = 5
+= Add and Assign x += 5 same as x = x + 5
-= Subtract and Assign x -= 5same as x = x - 5
*= Multiply and Assign x *= 5 same as x = x * 5
/= Divide AND Assign x /= 5 same as x = x / 5
%= Modulus AND Assign x %= 5 same as x = x % 5
//= Floor Division/Integer division AND Assign x //= 5 same as x = x // 5
**= Exponent AND Assign x **= 5 same as x = x ** 5
x=5
x += 3 #x= x + 3 ( x is now 8)
x -= 2 # x = x - 2 ( x is now 6)
x *= 4 # x= x * 4 (x is now 24)
x /= 3 # x= x/3 (x is now 8.0)
x %= 5 # x = x% 5 (x is now 3.0)
x //= 2 #x=x//2 (x is now 1.0)
x**=3 #x= x**3 ( x is now 1.0)

4. Logical Operators

Operator Description Example
and Logical AND x > 0 and x < 10 (True only if both condition are True)
or Logical OR x > 0 or x < 10 (True only if either conditions are True)
not Logical NOT not(x > 0 and x < 10) (Reverses the result, if result is True becomes False)
x = 5
y = 10
print(x > 2 and y < 20) # True (both conditions are true)
print(x > 6 or y==10) #True(atleast one condition is true)
print(not(x==5)) # False ( x is 5 so x == 5 is true but not operator makes the final output to False)

5. Bitwise Operators (Work with binary representations of integers.)

Operator Description Example
& Bitwise AND x & y
| Bitwise OR x | y
^ Bitwise XOR x ^ y
~ Bitwise NOT ~x
<< Left Shift x << n
>> Right Shift x >> n
x = 10 # Binary: 1010
y = 4 # Binary: 0100

print(x & y) #0 (1010 and 0100 only has 0's at shared place values.)
print(x | y) #14
print(x ^ y) # 14
print(~x) #-11
print(x << 2) # 40 (shifts 2 bits)
print(x >> 2) # 2

6. Membership Operators ( check sequence items)

Operator Description Example
in Returns True if a value found in the sequence otherwise returns False "apple" in list_of_fruits
not in Returns True if a value not found in the sequence otherwise returns False "apple" not in list_of_fruits
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
print("orange" not in fruits) # Output: True

7. Identity Operators (Compares memory locations of two objects)

Operator Description Example
is Returns True if both variables point same object otherwise returns False x is y
is not Returns True if both variables not point to the same object otherwise returns False x is not y
x = ["apple", "banana"]
y = ["apple", "banana"]
z = x
print(x is z) # True (x and z refer to the same list in memory.)
print(x is y) # False (Though x and y contain the same elements, but point different lists.)
print(x == y) # True (x and y contains same elements)
print(x is not y) # True (Since both point to the different memory locations)