๐งโโ๏ธ Python Operators: The Python Operator School of Magic ๐ช
Once upon a code-time, in a world not far from our keyboards, there was a school for young programmers called Pythonopolis. Every student there learned how to use magic symbols called โOperatorsโ. These werenโt wands or potions โ no! These were symbols that gave instructions to computers.
Each type of operator had a special job to do, and today, youโre about to meet them one by one!
๐ท๏ธ 1. The Assignment Spell: =
Letโs start with young Ada, a clever coder in Pythonopolis.
One day, she picked up a magic chalk and wrote on her scroll:
age = 8
The scroll glowed! Ada had used the assignment operator =
, which is like saying: โI give the name age
to the value 8
.โ
Itโs like saying: โHey, computer! From now on, if I say age
, I mean 8
.โ
She could also copy that magic and give it to another scroll:
another_age = age
Now both scrolls knew the same secret number!
โโโ๏ธโ 2. The Arithmetic Spells
Next, Ada visited the Math Room, where all the arithmetic operators danced around.
+
was the cheerful one who always added things together.-
was moody and liked to take away things.*
loved to multiply things./
liked to divide things equally.
Ada wrote:
cookies = 5 + 3
Boom! cookies
now had the value 8
. Then she tried:
juice = 10 - 4 # juice is 6
cakes = 2 * 3 # cakes is 6
shares = 8 / 2 # shares is 4.0
But there were even more powerful spells:
%
gave her the leftovers:7 % 3
gave1
(3 goes into 7 twice, with 1 left).**
was a power spell:2 ** 3
meant2 ร 2 ร 2 = 8
.//
was for only the whole part:7 // 2
gave3
, not3.5
.
She could also say:
score = 10
score += 2 # now score is 12!
The chalk sparkled with delight!
๐ 3. The Comparison Scrolls
One morning, Ada tried comparing scrolls.
She used the comparison operators to ask True or False questions.
5 == 5 # True
5 != 3 # True
5 > 3 # True
2 < 1 # False
3 >= 3 # True
2 <= 1 # False
It was like asking, โAre these numbers the same? Bigger? Smaller?โ
If the answer was true, the scroll glowed green. If false, it glowed red.
๐ฎ 4. The Boolean Magic Words
Ada discovered three powerful words in the logic room:
not
โ the flipper:not True
becameFalse
.and
โ the checker:True and False
wasFalse
. Only both True meant True!or
โ the helper:True or False
wasTrue
. As long as one thing was true, the spell worked.
Try this:
sunny = True
warm = False
go_outside = sunny and warm # False
have_fun = sunny or warm # True
โ๏ธ 5. Bitwise and Identity Operators
Now, these were for advanced magic students.
&
,|
,^
,~
,<<
,>>
โ These were bitwise tools, used when talking to computers at the bit level (tiny 0s and 1s).is
โ asks if two names point to the same magic object.in
โ checks if something is inside a group.
Example:
name = "Ada"
print("d" in name) # True, because "d" is in "Ada"
๐ Review Time!
Here are some questions to test your operator magic!
1. What will this line of code print?
result = 7 % 3
print(result)
A. 2 B. 1 C. 3 D. 0
2. Which operator would you use to add two numbers?
A. -
B. +
C. *
D. //
3. What will this return?
not (True and False)
A. True B. False C. Error D. None
4. Which operator checks if two values are equal?
A. =
B. !=
C. ==
D. >=
5. What will this code return?
"e" in "hello"
A. True B. False C. Error D. โYesโ