![[Nijika_Ijichi_Think_Python.png]]
In programming, a core principle is DRY—Don't Repeat Yourself. If you find yourself writing the same line of code over and over, there's almost certainly a better, more efficient way. That better way is using loops.
Loops allow us to execute a block of code multiple times, saving us from tedious repetition and making our programs more powerful and dynamic.
A while loop is the simplest form of a loop. It will repeat a block of code as long as a certain condition remains True.
Think of it as telling the computer: "While this is true, keep doing this."
Python
# A simple counter using a while loop
i = 0
while i < 3:
print("meow")
i = i + 1 # Increment the counterAnatomy of the while Loop:
-
Initialization: We start with a "counter" variable,
i, set to0. -
Condition: The loop checks
while i < 3:. The first time,0 < 3isTrue, so the loop runs. -
Body: The indented code inside the loop (
print("meow")andi = i + 1) is executed. -
Update: We increment
i. It becomes1. The loop goes back to step 2 and checks the condition again (1 < 3isTrue... then2 < 3isTrue). -
Termination: Once
ibecomes3, the condition3 < 3isFalse, and the loop stops.
i = i + 1), the condition will never become false, and your program will be stuck forever!
Use Ctrl+C to break out of the loop in VS Code
A for loop is designed for definite iteration—that is, looping a specific number of times. It is perfect for iterating over a sequence of items, like a list.
Think of it as telling the computer: "For each item in this collection, do this."
Before we master for loops, we need a data structure to loop over. A list is an ordered collection of items, stored in a single variable. You create a list using square brackets [].
Python
Gacha_Games = ["Zenless Zone Zero", "Wuthering Waves", "Genshin Impact"]
# Accessing an item by its index (position)
print(Gacha_Games[0]) # Output: "Zenless Zone Zero"The for loop elegantly handles the initialization, condition check, and updating for you.
Python
Characters = ["Iuno", "Augusta", "Galbrena"]
# The variable 'student' will take on the value of each item in the list, one by one.
for Characters in Characters:
print(Characters)This is far cleaner and less error-prone than a while loop for this task.
What if you just want to do something a specific number of times, without a list? The range() function generates a sequence of numbers for you.
-
range(3)produces the sequence0, 1, 2. -
range(5)produces0, 1, 2, 3, 4.
Python
# A more "Pythonic" way to meow three times
for _ in range(3):
print("meow")When you need a loop to run a certain number of times but you don't care about the counter variable itself, it's a common convention to use an underscore (_) as the variable name.
What if a simple list isn't enough? A dictionary or dict stores data not in an ordered sequence, but as key-value pairs. It's like a real-world dictionary where you look up a word (the key) to find its definition (the value). You create dictionaries with curly braces {}.
Python
# Using a dictionary to store characters and their games
characters = {
"Iuno": "WutheringWaves",
"Zani": "WutheringWaves",
"Cartethiya": "WutheringWaves",
"Seed": "Zenless",
"Evelynn": "Zenless"
}
# Access the value using its key
print(characters["Iuno"]) #Output WutheringWavesWhen you loop over a dictionary, you are iterating over its keys.
Python
# This loop prints each character's name (the key)
for character in characters:
# We use the key to look up the corresponding value
print(character, characters[character], sep=", ")You can put a loop inside another loop. This is called a nested loop, and it's perfect for problems that involve grids, matrices, or combinations of items.
Imagine you want to print a square of bricks (#).
Python
def main():
print_square(3)
def print_square(size):
# The outer loop handles the rows (height)
for i in range(size):
# The inner loop handles the columns (width)
for j in range(size):
# The 'end=""' prevents print() from starting a new line
print("#", end="")
# After the inner loop finishes, print a newline to start the next row
print()
main()How it works:
-
The outer loop starts (
i = 0). -
The inner loop runs to completion, printing
###. -
print()creates a new line. -
The outer loop runs again (
i = 1), and the inner loop runs to completion again, printing another row of###. -
This repeats until the square is complete.
Sometimes you need to alter a loop's behavior mid-run.
-
break: Immediately terminates the loop it's in. -
continue: Skips the rest of the current iteration and jumps to the next one.
Python
# An example of getting positive input from a user
while True: # This creates an intentional infinite loop
n = int(input("What's n? "))
if n > 0:
break # If n is positive, exit the loop
# This code will only be reached after the loop is broken
for i in range(n):
print("meow")This "loop forever and break" pattern is a very common and useful way to handle user input validation.