In this lesson, we will learn about Operators and Expressions in Python.
โจ Types of Operators in Python
1. Arithmetic Operators
These are used for basic math:
Operator | Description | Example (a = 10 , b = 3 ) | Result |
---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division | a / b | 3.33 |
// | Floor Division | a // b | 3 |
% | Modulus (remainder) | a % b | 1 |
** | Exponentiation | a ** b | 1000 |
๐ Try this in Python:
print(5 + 3)
print(7 % 2)
print(2 ** 3)
2. Comparison (Relational) Operators
Compare values and return True
or False
.
Operator | Description | Example (a = 10 , b = 3 ) | Result |
---|---|---|---|
== | Equal to | a == b | False |
!= | Not equal to | a != b | True |
> | Greater than | a > b | True |
< | Less than | a < b | False |
>= | Greater than or equal | a >= b | True |
<= | Less than or equal | a <= b | False |
3. Logical Operators
Used to combine conditional statements.
Operator | Description | Example | Result |
---|---|---|---|
and | True if both are true | True and False | False |
or | True if at least one is true | True or False | True |
not | Reverses result | not True | False |
4. Assignment Operators
Assign values to variables.
Operator | Example | Same As |
---|---|---|
= | x = 5 | Assign 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
//= | x //= 2 | x = x // 2 |
%= | x %= 2 | x = x % 2 |
**= | x **= 3 | x = x ** 3 |
5.Bitwise Operators
Bitwise operators work on bits (0s and 1s) of integers at the binary level.
They are used to perform operations like AND, OR, XOR, NOT, etc., bit-by-bit.
Operator | Description | Example |
---|---|---|
& | AND | 5 & 3 = 1 |
` | ` | OR |
^ | XOR | 5 ^ 3 = 6 |
~ | NOT | ~5 = -6 |
<< | Left Shift | 5 << 1 = 10 |
>> | Right Shift | 5 >> 1 = 2 |
๐งช 6. Identity and Membership Operators
Identity :
x = [1, 2]
y = x
print(x is y) # True
Membership
fruits = ["apple", "banana"]
print("apple" in fruits) # True
Expressions in Python
x = 10
y = 5
result = (x + y) * 2
print(result) # Output: 30
Operator Precedence
Determines the order in which operations are performed.
From highest to lowest:
()
โ Parentheses**
โ Exponentiation+
,-
(Unary)*
,/
,//
,%
+
,-
(Binary)<
,<=
,>
,>=
,==
,!=
and
or
=
,+=
,-=
, etc.
Example :
x = 10
y = 4
z = 3
result = x + y * z - y / 2
print(result)
# Output: 10 + 12 - 2.0 = 20.0
Examples
๐ข 1. Arithmetic Operators
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.333...
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000
๐ค 2. Comparison Operators
x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x < y) # Output: True
print(x >= 5) # Output: True
print(y <= 10) # Output: True
๐ง 3. Logical Operators
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
๐งฑ 4. Bitwise Operators
a = 5 # 0101
b = 3 # 0011
print(a & b) # Output: 1 (0001)
print(a | b) # Output: 7 (0111)
print(a ^ b) # Output: 6 (0110)
print(~a) # Output: -6 (inverts bits)
print(a << 1) # Output: 10 (shifts left: 1010)
print(a >> 1) # Output: 2 (shifts right: 0010)
โ๏ธ 5. Assignment Operators
x = 5
x += 3 # Same as x = x + 3
print(x) # Output: 8
x *= 2 # x = x * 2
print(x) # Output: 16
๐ 6. Identity Operators
a = [1, 2]
b = a
c = [1, 2]
print(a is b) # Output: True
print(a is c) # Output: False
print(a == c) # Output: True (values are same)
๐ 7. Membership Operators
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
print("grape" not in fruits) # Output: True
๐ฏ 8. Expressions Example
result = (5 + 3) * 2 - 4 / 2
print(result) # Output: 14.0
Welcome to the Python Programming Quiz!
Test your knowledge by selecting the correct answers. You will immediately see if you’re right or wrong.
Leave a Reply