Loops
#- The ability of doing something multiple times
cat.py
#Example of code
#print("meow")
print("meow")
print("meow")
Flowchart of the code
#
while
#- Is a way to repeat a block of code while a condition is true
While using substraction
#i = 3
while i != 0:
print("meow")
i = i - 1
Flowchart of the code
#
While using addition
#i = 0
while i < 3:
print("meow")
i = i + 1
Flowchart of the code
#
for
#- Is a way to repeat a block of code a number of times
For using a list
#for i in [0, 1, 2]:
print("meow")
For using range
#for i in range(3):
print("meow")
Pythonic way v1
#- The _ is a convention to indicate that the variable is not going to be used or is not important
for _ in range(3):
print("meow")
Pythonic way v2
#print("meow\n" * 3, end="")
Validating input
#- Is a way to ensure that the input is correct
Example of code
#while True:
n = int(input("What's n? "))
if n > 0:
break
for _ in range(n):
print("meow")
Exampo of code using a function
#def main():
number = get_number()
meow(number)
def get_number():
while True:
n = int(input("What's n? "))
if n > 0:
return n
def meow(n):
for _ in range(n):
print("meow")
main()
Iteration with Lists
#- Is a way to iterate over a list
Example of code
#students = "[Hermione, Harry, Ron]"
for student in students:
print(student)
len
#- Is a way to get the length of a list
Example of code
#students = ["Hermione", "Harry", "Ron"]
for i in range(len(students)):
print(i + 1, students[i])
Dictionaries
#- Is a way to store key-value pairs
Example of the code in a manual way
#students = {
"Hermione": "Gryffindor",
"Harry": "Gryffindor",
"Ron": "Gryffindor",
"Draco": "Slytherin",
}
print(students["Hermione"])
print(students["Harry"])
print(students["Ron"])
print(students["Draco"])
Example of the code using a loop
#students = {
"Hermione": "Gryffindor",
"Harry": "Gryffindor",
"Ron": "Gryffindor",
"Draco": "Slytherin",
}
for student in students:
print(student, students[student], sep=", ")
List of Dictionaries
#students = [
{"name": "Hermione", "house": "Gryffindor", "patronus": "Otter"},
{"name": "Harry", "house": "Gryffindor", "patronus": "Stag"},
{"name": "Ron", "house": "Gryffindor", "patronus": "Jack Russell terrier"},
{"name": "Draco", "house": "Slytherin", "patronus": None},
]
for student in students:
print(student["name"], student["house"], student["patronus"], sep=", ")
Nested Loops
#- Is a way to have a loop inside another loop
Example of code v1
#def main():
print_square(3)
def print_square(size):
# For each row in square
for _ in range(size):
# For each brick in row
for _ in range(size):
# Print brick
print("#", end="")
print()
main()
Example of code v2
#def main():
print_square(3)
def print_square(size):
for i in range(size):
for j in range(size):
print("#", end="")
print()
main()
Example of code v3
#def main():
print_square(3)
def print_square(size):
for _ in range(size):
print("#" * size)
main()
Example of code v4
#def main():
print_square(3)
def print_square(size):
for _ in range(size):
print_row(size)
def print_row(width):
print("#" * width)
main()
Expected output for all examples
####
###
###