Download presentation
Presentation is loading. Please wait.
Published byAnnabella Bell Modified over 6 years ago
1
Nate Brunelle Today: Repetition, A Trip To The Moon
CS1110 Nate Brunelle Today: Repetition, A Trip To The Moon
2
Questions?
3
Last Times exam review If, elif, else
4
Repetition (pseudocode)
def wash_hair(): Repeat 3 times: Lather() Rinse def Lather(): put shampoo in hand Repeat until hair is foamy: rub hand through hair
5
Repetition (Python), aka loops
def wash_hair(): for i in range(3): Lather() Rinse def Lather(): put shampoo in hand while not hair_is_foamy: rub hand through hair
6
“While” Loops while loop while boolean expression:
Keep repeating until the boolean expression is False “so long as this is True, keep doing the action” while boolean expression: action1 action2
7
Conditional decision Statement
if boolean expression: action elif boolean expression: another action else: yet another action 0 or more 0 or 1
8
Lists My_list[i] # gives the ith thing in the list (starts with 0)
A way of collecting together a bunch of things, and giving it all one name [the things separated by commas] my_list = [1, 2, 3, 4] my_list = [“one”, “two”, “three”, “four”] my_list = [1, “two”, 3, “four”] len(my_list) # gives the number of things in the list, in this case 4 My_list[i] # gives the ith thing in the list (starts with 0) my_list[1] -> “two”
9
“for” Loops For loop for [variable v] in [collection]: action
Do the action a certain number of times “do the action once per each thing in here” The variable takes the value of each thing in the collection in order for [variable v] in [collection]: action for i in range(5): print(‘hi’) Range(5) -> [0,1,2,3,4]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.