for element in list:
code inside the loop
code outside the loop
Example: What will print in the code below?
for n in [1,2,3,4,5]:
print n
print("timnit")
for w in [1,2,3]:
print(w)
print("jelani")
1 2 3 4 5 timnit 1 2 3 jelani
for i in range(200,400):
if i % 20 == 0:
continue
if i % 30 == 0:
print(i)
210 270 330 390
fruits = ['strawberry','apple','banana','apple','mango']
# have students give one word at a time?
for fruit in fruits:
print(fruit)
Range lets us iterate through numbers without building a list of those numbers
Range can take:
one argument range(stop)
two arguments range(start, stop)
three arguments range(start, stop, skip)
# do 10 first, and then 11
for i in range(11):
print(i)
0 1 2 3 4 5 6 7 8 9
# do 20 first, then 21
for i in range(21):
print i
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# do 0,10, then 0,11, then 1,11
for i in range(1,11):
print i
1 2 3 4 5 6 7 8 9 10
# do 0,20,2 first, then 21, then 22
for i in range(0,21,2):
print(i)
0 2 4 6 8 10 12 14 16 18 20
# do 0,20,2 first, then 21, then 22
for i in range(3,31,3):
print(i)
3 6 9 12 15 18 21 24 27 30
n
using range. Write how you would test your code with at least 3 numbersdef print_all(n):
for i in range(n):
print(i)
start
to stop
counting by skip
using range. Write how you would test your code with at least 3 numbersdef print_some(start,stop,skip):
for i in range(start,stop,skip):
print(i)
How can we use range to print all the elements in a list? What number should we put into the range for stop
?
fruits1 = ['strawberry','apple','banana','apple','mango']
# first do 6, then do 5
# first do f, then fruits[f]
for f in range(5):
print(fruits1[f])
strawberry apple banana apple mango
len(fruits1)
5
What if I add a fruit?
fruits2 = ['strawberry','apple','banana','apple','mango', 'avocado']
# first do 6, then do 5
# first do f, then fruits[f]
for f in range(6):
print(fruits2[f])
strawberry apple banana apple mango avocado
len(fruits2)
6
What if I add two fruit?
fruits3 = ['strawberry','apple','banana','apple','mango', 'avocado', 'grape']
for f in range(7):
print(fruits3[f])
strawberry apple banana apple mango avocado grape
len(fruits3)
7
# start with print(i)
for i in range(len(fruits3)):
print(fruits3[i])
for i in range(len(fruits)-1):
print(fruits[i])
def print_all_except_last(input_list):
for i in range(len(input_list)-1):
print(input_list[i])
print_all_except_last(fruits1)
print_all_except_last(fruits2)
print_all_except_last(fruits3)
vegetables = ['hamburger','pizza','firfir','tibs','sandwich','pasta']
EX: pizza, tibs, pasta
for i in range(1,len(vegetables),2):
print(vegetables[i])
pizza tibs pasta
Break means "stop the loop, don't do anymore iterations -- even if we weren't done yet"
Continue means "skip the rest of this iteration of the loop and start the next iteration"
names = ['Hailey','Bre','Theresa','Stephen','Abraham','Nebiyu']
EX: 'Hailey','Bre','Theresa','Stephen','Abraham'
# why don't I need an 'else' here?
for i in range(len(names)):
if names[i] == 'Nebiyu': # == 5, < 6,
break;
print(names[i])
Hailey Bre Theresa Stephen Abraham
for i in range(len(names)):
if i > len(names) - 2: # == 5, < 6,
break;
print(names[i])
Hailey Bre Theresa Stephen Abraham
EX: 'Hailey','Bre','Theresa','Stephen'
# why don't I need an 'else' here?
for i in range(len(names)):
if names[i] == 'Abraham': # == 5, < 6,
break;
print(names[i])
Hailey Bre Theresa Stephen
# why don't I need an 'else' here?
for i in range(len(names)):
if i > len(names) - 3: # == 5, < 6,
break;
print(names[i])
Hailey Bre Theresa Stephen
EX: Nebiyu
# why don't I need an else here?
for i in range(len(names)):
if names[i] != 'Nebiyu':
continue;
print(names[i])
Nebiyu
# why don't I need an else here?
for i in range(len(names)):
if i < len(names) - 1:
continue;
print(names[i])
Nebiyu
EX: Abraham Nebiyu
for i in range(len(names)):
if i < len(names) - 2:
continue;
print(names[i])
Abraham Nebiyu
while condition:
code inside the loop
code outside the loop
If the condition is True, run the code in the while loop, and then start back at the beginning of the while loop.
If the condition is False, skip the code in the while loop.
current_step = 0 # initialize counter
while current_step < 10: # condition
print("clap "*current_step) # body
current_step += 1 # increment counter
cities=['addis abababa', 'boston', 'paris', 'cairo']
y=0
while y<len(cities):
print(cities[y])
y += 1
addis abababa boston paris cairo
addis ababa
using a while loop#What if I only want to print math?
i=0
while i<len(cities):
print(cities[i])
if i>0: #what is wrong?
break
i +=1 #What happens if I comment this out?
addis abababa boston
#What if I only want to print math?
i=0
while i<len(cities):
print(cities[i])
i +=1
if i>0:
break
addis abababa
cairo
using a while loopcities=['addis abababa', 'boston', 'paris', 'cairo']
#What if I only want to print english?
i=0
while i<len(cities):
if i==len(cities)-1:
print(cities[i])
i += 1
cairo
EX: 0, 2, 4, ... 20
i = 0
while i < 21:
if i % 2 == 0:
print(i)
i += 1
0 2 4 6 8 10 12 14 16 18 20
EX: 1,3,5, . . . 19
i = 0
while i < 21:
if i % 2 != 0:
print(i)
i += 1
1 3 5 7 9 11 13 15 17 19
Write a function to print all the even numbers until n. Write how you would test your code with at least three numbers.
def evens(n):
i = 0
while i < n:
if i % 2 == 0:
print(i)
i += 1
evens(3)
0 2
Write a function that takes a string and returns True
if the string is a palindrome and False
otherwise. You can solve this problem using a for loop, but please solve it here using a while loop
#Note: you can also use a for loop
def isPalindrome(s):
i = 0
j = len(s)-1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
isPalindrome('aaa')
True
isPalindrome('racecar')
True
isPalindrome('asdffdsa')
True
isPalindrome('asdfdsa')
True
isPalindrome('adsa')
False
isPalindrome('a')
True
n jhujfor i in [1,2,3,4,5]:
for j in [1,2,3,4,5]:
print(i,j)
(1, 1) (1, 2) (1, 3) (1, 4) (1, 5) (2, 1) (2, 2) (2, 3) (2, 4) (2, 5) (3, 1) (3, 2) (3, 3) (3, 4) (3, 5) (4, 1) (4, 2) (4, 3) (4, 4) (4, 5) (5, 1) (5, 2) (5, 3) (5, 4) (5, 5)
for names in ['hailey','nebiyou','stephen']:
for j in names:
print(j)
h a i l e y n e b i y o u s t e p h e n
for i in range(5):
store = ''
for j in range(i):
store += str(j)
print(store)
0 01 012 0123