๐ญ Python Operators: Your Magic Toolbox
The Big Picture: Imagine you have a magic calculator that can do way more than just add numbers. It can compare things, check if something is inside a box, and even talk to your computer in its secret language. These magic buttons are called operators!
๐งฎ Arithmetic Operators: The Math Wizards
Think of arithmetic operators like the buttons on a calculator. Each button does a different math trick!
The Seven Math Spells
| Operator | Name | What It Does | Example |
|---|---|---|---|
+ |
Addition | Adds things together | 5 + 3 โ 8 |
- |
Subtraction | Takes away | 10 - 4 โ 6 |
* |
Multiplication | Makes copies | 4 * 3 โ 12 |
/ |
Division | Shares equally | 15 / 3 โ 5.0 |
// |
Floor Division | Shares equally, no pieces | 17 // 5 โ 3 |
% |
Modulus | Whatโs left over? | 17 % 5 โ 2 |
** |
Exponentiation | Super multiplication | 2 ** 3 โ 8 |
๐ช Cookie Story
Imagine you have 17 cookies and 5 friends:
cookies = 17
friends = 5
each_gets = cookies // 5 # 3 cookies each
leftover = cookies % 5 # 2 cookies remain
Floor division (//) tells you: โEach friend gets 3 cookies.โ
Modulus (%) tells you: โ2 cookies are left over for you!โ
โ๏ธ Comparison Operators: The Judges
Comparison operators are like judges at a talent show. They look at two things and say True (thumbs up ๐) or False (thumbs down ๐).
The Six Judges
| Operator | Question It Asks | Example | Answer |
|---|---|---|---|
== |
Are they equal? | 5 == 5 |
True |
!= |
Are they different? | 5 != 3 |
True |
> |
Is left bigger? | 7 > 4 |
True |
< |
Is left smaller? | 3 < 8 |
True |
>= |
Is left bigger or same? | 5 >= 5 |
True |
<= |
Is left smaller or same? | 4 <= 6 |
True |
๐ Age Check Story
your_age = 10
ride_age = 8
can_ride = your_age >= ride_age # True!
print("You can ride!") if can_ride else print("Too young")
Youโre 10, the ride needs 8+. The >= judge says True! ๐ข
๐ง Logical Operators: The Decision Makers
Logical operators help you make big decisions by combining smaller yes/no questions.
The Three Thinkers
| Operator | What It Does | Real Life Example |
|---|---|---|
and |
BOTH must be True | โIs it sunny AND warm?โ |
or |
AT LEAST ONE must be True | โDo you want pizza OR burgers?โ |
not |
Flips the answer | โNOT rainingโ means itโs dry |
๐ฎ Video Game Story
has_homework = False
is_weekend = True
can_play = not has_homework and is_weekend
# True! No homework AND it's weekend!
graph TD A[Can I play games?] --> B{Homework done?} B -->|Yes| C{Is it weekend?} B -->|No| D[โ Do homework first!] C -->|Yes| E[โ Play games!] C -->|No| F[โฐ Only 1 hour]
๐ Assignment Operators: The Note Takers
Assignment operators are like writing notes. They save values into boxes called variables.
The Quick Writers
| Operator | What It Does | Same As |
|---|---|---|
= |
Put value in box | x = 5 |
+= |
Add to whatโs inside | x = x + 3 |
-= |
Subtract from inside | x = x - 2 |
*= |
Multiply whatโs inside | x = x * 4 |
/= |
Divide whatโs inside | x = x / 2 |
//= |
Floor divide inside | x = x // 3 |
%= |
Get remainder | x = x % 5 |
**= |
Power up inside | x = x ** 2 |
๐ฆ Piggy Bank Story
piggy_bank = 10 # Start with $10
piggy_bank += 5 # Add $5 โ Now $15
piggy_bank -= 3 # Spend $3 โ Now $12
piggy_bank *= 2 # Double it! โ Now $24
The += is a shortcut. Instead of writing piggy_bank = piggy_bank + 5, just write piggy_bank += 5!
๐ Identity Operators: The Twin Detectors
Identity operators check if two things are the exact same thing in the computerโs memoryโnot just equal, but literally the same!
The Two Detectives
| Operator | Question | Example |
|---|---|---|
is |
Same object in memory? | a is b |
is not |
Different objects? | a is not b |
๐ Balloon Story
balloon1 = [1, 2, 3]
balloon2 = [1, 2, 3]
balloon3 = balloon1
print(balloon1 == balloon2) # True (same colors)
print(balloon1 is balloon2) # False (different balloons!)
print(balloon1 is balloon3) # True (same balloon!)
Two balloons can look the same (==) but still be different balloons (is). When you say balloon3 = balloon1, youโre pointing to the SAME balloon!
๐ฆ Membership Operators: The Box Checkers
Membership operators check if something is inside a collection, like looking for your toy in a toy box.
The Two Searchers
| Operator | Question | Example |
|---|---|---|
in |
Is it inside? | "a" in "cat" โ True |
not in |
Is it missing? | 5 not in [1,2,3] โ True |
๐ Backpack Story
backpack = ["book", "pencil", "lunch", "water"]
print("pencil" in backpack) # True โ
print("phone" in backpack) # False โ
print("toys" not in backpack) # True (no toys!)
Works with strings too:
print("py" in "python") # True!
print("java" in "python") # False!
๐ข Bitwise Operators: The Binary Wizards
Bitwise operators work with numbers in their secret computer language called binary (just 0s and 1s!).
The Binary Spells
| Operator | Name | What It Does |
|---|---|---|
& |
AND | Both bits must be 1 |
| |
OR | At least one bit is 1 |
^ |
XOR | Exactly one bit is 1 |
~ |
NOT | Flip all bits |
<< |
Left Shift | Move bits left (multiply by 2) |
>> |
Right Shift | Move bits right (divide by 2) |
๐ก Light Switch Story
Think of bits as light switches (0 = off, 1 = on):
a = 5 # Binary: 101
b = 3 # Binary: 011
print(a & b) # 1 (001) - AND
print(a | b) # 7 (111) - OR
print(a ^ b) # 6 (110) - XOR
# Shifting is like super-fast math!
print(4 << 1) # 8 (multiply by 2)
print(8 >> 1) # 4 (divide by 2)
graph TD A["5 = 101"] --> C["& AND"] B["3 = 011"] --> C C --> D["1 = 001"] A --> E["| OR"] B --> E E --> F["7 = 111"]
๐ Operator Precedence: Who Goes First?
When you have many operators, Python follows a special orderโjust like math class! Think of it as a VIP line where some operators are more important.
The VIP Order (Top = First)
1. ( ) โ Parentheses are KING ๐
2. ** โ Exponentiation
3. ~, +, - โ Unary (single number operations)
4. *, /, //, %โ Multiplication family
5. +, - โ Addition family
6. <<, >> โ Bit shifting
7. & โ Bitwise AND
8. ^ โ Bitwise XOR
9. | โ Bitwise OR
10. ==, !=, <, >, <=, >=, is, in โ Comparisons
11. not โ Logical NOT
12. and โ Logical AND
13. or โ Logical OR
๐ฏ Order Story
result = 2 + 3 * 4 # 14, not 20!
# Python does: 3 * 4 = 12, then 2 + 12 = 14
result = (2 + 3) * 4 # 20!
# Parentheses first: 2 + 3 = 5, then 5 * 4 = 20
Pro Tip: When confused, use parentheses ( ). They always go first and make your code easier to read!
# Hard to read:
x = 2 ** 3 + 4 * 5 - 6 / 2
# Easy to read:
x = (2 ** 3) + (4 * 5) - (6 / 2)
# = 8 + 20 - 3.0 = 25.0
๐ You Did It!
Youโve learned all seven types of Python operators:
| Type | Superpower | Key Operators |
|---|---|---|
| Arithmetic | Math magic | + - * / // % ** |
| Comparison | Judge things | == != > < >= <= |
| Logical | Make decisions | and or not |
| Assignment | Save values | = += -= *= |
| Identity | Find twins | is is not |
| Membership | Search boxes | in not in |
| Bitwise | Binary wizardry | & | ^ ~ << >> |
๐ Remember: Operators are your tools. The more you practice, the more powerful your Python programs become!
Now go forth and operate! ๐โจ