Operators and Expressions
13 February 2025

Operators and Expressions
1.What are operators?
Operators are symbols that perform operations on variables and values. They are used to form expressions, which are evaluated to produce results.
2.Types of operators
1.Arithmetic Operators
These operators perform mathematical calculations:
•+(Addition):10+3=13
•-(Subtraction): 10-3=7
•*(Multiplication):10*3=30
•/(Floor Divison):10//3=3
•%(Modulus, remainder):10%3=1
•**(Exponentiation):10**3=1000
Example :
a, b = 10, 3
print(a + b) # 13
print(a ** b) # 1000
2.Comparison Operators
These operators compare values and return True and Flase.
•==(Equal to): 10==5→False
•!=(Not equal to): 10 != 5 → True
> (Greater than): 10 > 5 → True
< (Less than): 10 < 5 → False
>= (Greater than or equal to): 10 >= 10 → True
<= (Less than or equal to): 10 <= 5 → False
Example:
x, y = 10, 5
print(x > y) # True
print(x == y) # False
3. Logical Operators
These operators perform logical operations and return True or False.
and (Logical AND): Returns True if both conditions are True.
or (Logical OR): Returns True if at least one condition is True.
not (Logical NOT): Returns True if the condition is False, and vice versa.
Example:
a, b = True, False
print(a and b) # False
print(a or b) # True
print(not a) # False
4. Assignment Operators
These operators assign values to variables.
= (Assignment): x = 10
+= (Addition assignment): x += 5 → x = x + 5
-= (Subtraction assignment): x -= 5 → x = x - 5
*= (Multiplication assignment): x *= 5 → x = x * 5
/= (Division assignment): x /= 5 → x = x / 5
//= (Floor division assignment): x //= 5
%= (Modulus assignment): x %= 5
**= (Exponentiation assignment): x **= 5
Example:
x = 10
x += 3
print(x) # 13
5. Bitwise Operators
These operators perform operations at the binary level.
& (AND): 5 & 3 → 1
| (OR): 5 | 3 → 7
^ (XOR): 5 ^ 3 → 6
~ (NOT): ~5 → -6
<< (Left shift): 5 << 1 → 10
>> (Right shift): 5 >> 1 → 2
Example:
a, b = 5, 3
print(a & b) # 1
print(a | b) # 7
6. Membership Operators
These operators check for membership in a sequence (lists, tuples, strings, etc..)
not in: Returns True if a value is not found in the sequence.
nums = [1, 2, 3, 4]
print(3 in nums) # True
print(5 not in nums) # True
7. Identity Operators
These operators check if two variables refer to the same object in memory.
is: Returns True if two variables refer to the same object.
is not: Returns True if two variables refer to different objects.
Example:
An expression is a combination of values, variables, and operators that is evaluted to produce a result.
Example:
x = 5
y = 3
z = (x + y) * 2 # Expression
print(z) # 16
Conclusion
Operators are essential in programming as they allow various operations on data. Understanding these operators helps in writing efficient and effective code.