Skip to content

Chunk b basics


๐Ÿ“š Understanding the Core Concepts

๐Ÿงฉ What is an Object? (review)

What is an object?

  • 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 like animate_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!
# Like a carousel spinning 3 turns
for i in range(3):
    print("Hi!")  # Print "์•ˆ๋…•!" on every turn

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."

range(3)  # 0, 1, 2 (3 total)
range(5)  # 0, 1, 2, 3, 4 (5 total)

List positions also start from 0.

my_list = ['a', 'b', 'c']
my_list[0]  # 'a' (the first item)
my_list[1]  # 'b' (the second item)

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!

for i in range(3):
    print(f"This is the {i}turn!")
  • 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! ๐Ÿ