Chunk b basics
๐ Understanding the Core Concepts
๐งฉ What is an Object? (review)

- having both properties and behavior a LEGO block properties and behaviors.
- Variables hold properties, functions hold the instructions for behavior, and an object combines the two.
- Ursina's Entity is
model,color,scaleโ properties likeanimate_position()and an object with methods like
Before learning the code, let's first understand some important concepts!
Loops are a really useful tool! They can spin like a carousel and repeat the same thing! ๐
What is a Loop (For Loop)? ๐
Loopsis used when you want to run the same code multiple times!
Just like a carouselLike this, it spins a set number of times, repeating the same thing! ๐
๐ Understanding it with the carousel analogy
A loop is like a carousel!
- A carousel spins a set number of times (e.g., 8 turns)
- The same music plays on every turn
- A loop also spins a set number of times, running the same code!
Why do we use loops?
Without loops, you'd have to copy and paste the same code over and over. ๐
# Without a loop (inefficient) approach
print("Hi!")
print("Hi!")
print("Hi!")
# Using a loop (efficient) approach
for i in range(3):
print("Hi!")
Using a loop makes the code much shorter and cleaner! โจ
What is the range() function? ๐ญ
range() A function Number factoryit is! ๐ญ
If you order "make me 5!" at the number factory, it produces the numbers 0, 1, 2, 3, 4!
๐ญ The number factory analogy
range() is like a factory that stamps out numbers!
range(5)= "Make me 5 numbers!" โ produces 0, 1, 2, 3, 4! ๐ญrange(8)= "Make me 8 numbers!" โ produces 0, 1, 2, 3, 4, 5, 6, 7! ๐ญ
Just like a factory stamping out numbers, range() the function produces numbers too!
| Code | the numbers the number factory makes | Number of repetitions |
|---|---|---|
range(3) |
0, 1, 2 | 3 times ๐ |
range(5) |
0, 1, 2, 3, 4 | 5 times ๐ |
range(8) |
0, 1, 2, 3, 4, 5, 6, 7 | 8 times ๐ |
Why does it start from 0?
In programming, we usually start counting from 0! This is called "0-based indexing."
List positions also start from 0.
Variable iis what? ๐งบ
the variable used in a loop iis a basket that holds the current numberit is! ๐งบ
Every time the carousel spins, a new number goes into the basket!
๐งบ The basket analogy
Variable iis just like a basket that holds a number!
- First turn: 0 goes into the basket ๐งบ โ
i = 0 - Second turn: 1 goes into the basket ๐งบ โ
i = 1 - Third turn: 2 goes into the basket ๐งบ โ
i = 2
The number in the basket changes on every turn!
Think of it as a counter too
iis a counteryou can also think of it as!
- Turn 1: "This is turn 0!"
- Turn 2: "This is turn 1!"
- Turn 3: "This is turn 2!"
Why Indentation Matters ๐
In Python, Indentationis very important!
Indentation the loop's roomit is! ๐
Only the code inside the room runs repeatedly!
๐ The loop's room analogy
A loop is just like a house!
for i in range(3):
print(i) # ๐ Code inside the room (repeated)
print(i * 2) # ๐ Code inside the room (repeated)
print("Done!") # ๐ช Code outside the room (runs only once)
- Inside the room (indented): runs repeatedly! ๐
- Outside the room (not indented): runs only once!
๐จ If you don't indent, you'll get an error!
# Correct way โ
for i in range(3):
print(i) # ๐ Inside the room, so it repeats
print(i * 2) # ๐ Inside the room, so it repeats
# Wrong way โ
for i in range(3):
print(i) # ๐ช Outside the room, so it errors out!
You usually use 4 spaces or 1 tab to make the room! ๐