π¦ Variables
What is a variable?
A named box that stores information. Give it a label, put a value inside!
# Create (STORE)
name = "Alex" # Text
age = 8 # Number
ready = True # Boolean
# Change
score = 0
score = score + 10
# Print (SHOW)
print("Score:", score)
VAR FLOW
VAR β STORE β CHANGE β PRINT
3 Data Types
π Text: "Hello" (in quotes)
π’ Number: 42 (no quotes)
β
Boolean: True / False
β οΈ Common Mistakes
player name = "x"player_name = "x"
score = "100"score = 100
print scoreprint(score)
ready = trueready = True
π Loops
FOR loop β set number of times
# Repeat 5 times (0,1,2,3,4)
for i in range(5):
print("Hello!")
# Count 1 to 10
for i in range(1, 11):
print(i)
# Use the variable
for i in range(1, 6):
print("Star", i)
WHILE loop β while condition is true
# Count down from 5
count = 5
while count > 0:
print(count)
count = count - 1
FOR vs WHILE β when to use?
π΅ FOR: Know exactly how many times
π’ WHILE: Don't know how many times
β οΈ Infinite Loop β DANGER!
If the condition never becomes False, the loop runs forever! Always make sure something changes inside the loop.
range() quick guide
range(5) β 0,1,2,3,4
range(1,6) β 1,2,3,4,5
range(0,10,2) β 0,2,4,6,8
ποΈ Conditionals
if score >= 80:
print("You passed! π")
else:
print("Try again! πͺ")
IF-ELIF-ELSE (multiple choices)
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
Comparison Operators
> greater than Β < less than
>= greater or equal Β <= less or equal
== equal to Β != not equal to
Boolean Operators
ANDBoth must be True
ORAt least one must be True
NOTFlips TrueβFalse
| A | B | A AND B | A OR B | NOT A |
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |
ποΈ Functions
# Define (create)
def greet():
print("Hello! π")
# Call (use)
greet()
greet() # Use again!
def greet(name):
print("Hello,", name)
greet("Alex")
greet("Nova")
def add(a, b):
result = a + b
return result
answer = add(5, 3)
print(answer) # 8
Function Path
CALL β RUN β RETURN
Define first, then call!
Parts of a Function
π·οΈ Name: What you call it (greet)
π¦ Parameter: Input (name)
π Body: What it does (indented)
β©οΈ Return: What it sends back
π Sequences & Algorithms
Sequence
Steps in a specific order. Order matters β changing it changes the result!
Algorithm
A precise, step-by-step plan. Exact enough for a robot to follow without asking questions!
Flowchart Symbols
β¬ Oval: Start / End
β Rectangle: Action / Step
β Diamond: Decision (Yes/No)
β Arrow: Flow direction
Bot Commands
MOVE β go forward 1 step
TURN_LEFT β rotate 90Β° left
TURN_RIGHT β rotate 90Β° right
COLLECT β pick up item
Sequence vs Algorithm
π Sequence: "Make a sandwich"
π€ Algorithm: "Get 2 slices of bread. Spread 5g butter on each slice. Add 2 slices of cheese..."