Operators

Loading concept...

๐ŸŽญ 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! ๐Ÿโœจ

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.