def meaw(n):
for _ in range(n):
print("Meaw")
40 Control Flow Exercise
40.1 For Loop
40.1.1 Meaw
meaw()
: using for loop
let’s print “meaw” 3 times
3) meaw(
Meaw
Meaw
Meaw
meaw()
: pythonista way
def meaw(n):
print("Meaw\n" * n, end="")
4) meaw(
Meaw
Meaw
Meaw
Meaw
40.1.2 Hogwarts
40.1.2.1 List of Students
= ["Hermione", "Harry", "Ron"]
students students
['Hermione', 'Harry', 'Ron']
for student in students:
print(student)
Hermione
Harry
Ron
40.1.2.2 Dictionary of Students
= [
students "name": "Hermione", "house": "Gryffindor", "patronus": "Otter"},
{"name": "Harry", "house": "Gryffindor", "patronous": "Stag"},
{"name": "Draco", "house": "Slytherin", "patronous": None}
{ ]
Iterate though array of student (row by row)
for student in students:
print(student["name"], student["house"], sep=", ")
Hermione, Gryffindor
Harry, Gryffindor
Draco, Slytherin
Define a function to print information from each student
def print_student_info(i):
print("Student No.", i + 1)
for k, v in students[i].items() :
print("- ", k, ": ", v)
0) print_student_info(
Student No. 1
- name : Hermione
- house : Gryffindor
- patronus : Otter
Now, print information from all students
for i in range(len(students)):
print_student_info(i)
Student No. 1
- name : Hermione
- house : Gryffindor
- patronus : Otter
Student No. 2
- name : Harry
- house : Gryffindor
- patronous : Stag
Student No. 3
- name : Draco
- house : Slytherin
- patronous : None