In this notebook, we learn to control the logic flow. We will use if
, while
, for
to create condition and iteration.
While-loop is usually used for iteration that we don’t know the total count.
For-loop is when we know the iteration count. For example, we already have the list of items. Or we already know how many times to repeat.
data = ['Apple', 'Banana', 'Orange']
for fruit in data:
print(fruit)
Apple Banana Orange
Let’s bring back our is_student_pass
function we defined. We can use a for loop to get a list of Pass/Fail result.
def is_student_pass(score):
if score >= 60:
return "Pass"
else:
return "Fail"
scores = [80, 59, 62, 70, 99, 40, 51]
for score in scores:
result = is_student_pass(score)
print(f"{score}: {result}")
80: Pass 59: Fail 62: Pass 70: Pass 99: Pass 40: Fail 51: Fail
In this exercise, we bring back the book lists and loop books to print each details.
books = [
{'title': 'Python Tricks', 'category': 'Programming', 'price': 240},
{'title': 'Python Crash Course', 'category': 'Programming', 'price': 200},
{'title': 'Getting Real', 'category': 'Startup', 'price': 200}
]
Try to loop the books and print the title and their price. We don’t need to print the category in this exercise.
for book in books:
# Your code here
title = None
price = None
print(None)
None None None
Expected result |
---|
Python Tricks: $240 |
Python Crash Course: $200 |
Getting Real: $200 |
def is_student_pass(score):
if score >= 60:
return "Pass"
else:
return "Fail"
scores = [80, 59, 62, 70, 99, 40, 51]
result_map = map(is_student_pass, scores)
result_list = list(result_map)
print(result_list)
['Pass', 'Fail', 'Pass', 'Pass', 'Pass', 'Fail', 'Fail']
result_list.count("Pass")
4
result_list.count("Fail")
3
Sometimes, we don’t have the list to iterate. But we know how many steps we want to loop. In this case, we can use range
.
# print("0-9")
for i in range(10):
print(i)
0 1 2 3 4 5 6 7 8 9
# print("1-10")
for i in range(1,11):
print(i)
1 2 3 4 5 6 7 8 9 10
# print("1,3,5,7,9")
for i in range(1,10,2):
print(i)
1 3 5 7 9
# print("2,4,6,8,10")
for i in range(2,11,2):
print(i)
2 4 6 8 10
By combining list and while loop, we can keep appending the value to the tasks
list until the input is "q". The break
command exits the while-loop.
tasks = []
while True:
value = input("Please input a to-do task, or 'q' to quit. ")
if value == 'q':
break
tasks.append(value)
print(tasks)
Please input a to-do task, or 'q' to quit. q []
You can tell from the indentation that line 4—9 under the while True:
loop. Line 7 is under the if value=='q':
loop.
Keep in mind that we need to be careful when using while True:
. the condition is always true so the loop will run forever until we meet the break
command. If the logic we designed has flaw, the loop may never end.
In this exercise, we would like to create a guest list by modifying while
loop example given above.
guests = []
# Your code here
while None:
value = input("Your text here ")
if value == 'q':
break
None # append the value to guests
print(guests)
[]
In this lesson, we learned the basic logic flow. Specifically: