Forum

Learn Python Operat…
 
Notifications
Clear all

Learn Python Operators

1 Posts
1 Users
0 Reactions
11 Views
 josh
(@josh)
Member Admin
Joined: 2 months ago
Posts: 510
Topic starter  

Python operators are like the action heroes of your code—they perform operations on variables and values. Let’s break them down into categories so you can see how they work 🧠⚙️


➕ Arithmetic Operators

Used for basic math:

x = 10
y = 3

print(x + y)  # Addition → 13
print(x - y)  # Subtraction → 7
print(x * y)  # Multiplication → 30
print(x / y)  # Division → 3.333...
print(x % y)  # Modulus → 1
print(x ** y) # Exponentiation → 1000
print(x // y) # Floor Division → 3

📝 Assignment Operators

Used to assign values and update them:

x = 5
x += 3  # Same as x = x + 3 → x becomes 8
x *= 2  # x becomes 16

Other examples: -=, /=, //=, %=, **=


🔍 Comparison Operators

Used to compare values and return True or False:

x = 5
y = 10

print(x == y)  # False
print(x != y)  # True
print(x < y)   # True
print(x >= y)  # False

🔗 Logical Operators

Used to combine conditions:

x = 5
print(x > 2 and x < 10)  # True
print(x < 2 or x > 10)   # False
print(not(x > 2))        # False

🧬 Identity Operators

Check if two variables refer to the same object:

a = [1, 2]
b = a
c = [1, 2]

print(a is b)     # True
print(a is c)     # False
print(a is not c) # True

📦 Membership Operators

Check if a value is in a sequence:

fruits = ["apple", "banana"]
print("apple" in fruits)     # True
print("cherry" not in fruits) # True

🧮 Bitwise Operators

Operate on binary numbers:

x = 5  # 0101
y = 3  # 0011

print(x & y)  # AND → 1
print(x | y)  # OR → 7
print(x ^ y)  # XOR → 6
print(~x)     # NOT → -6
print(x << 1) # Left shift → 10
print(x >> 1) # Right shift → 2

 


   
Quote
Share: