Update and rename for_loops to for_loops_2

This commit is contained in:
Joe James
2020-02-25 08:12:56 -06:00
committed by GitHub
parent ecca009a78
commit ea0956aea9
2 changed files with 37 additions and 31 deletions

View File

@@ -1,31 +0,0 @@
# FOR LOOPS
# starts at 0, stops at 5-1
for i in range(5):
print(i)
print() # print a blank line
# iterate letters in a text string
for letter in "dog":
print(letter)
print()
# loop starts with 5 and continues to 10-1
for i in range(5, 10):
print (i**2) # print i squared
print()
# start=10, end=20-1, step=3
# (every third number from 10 to 19)
for i in range(10, 20, 3):
print(i)
print()
# loop iterates a list
birds = ['robin', 'sparrow', 'duck']
for i in birds:
print(len(i)) # print number of letters in each item

37
python/for_loops_2 Normal file
View File

@@ -0,0 +1,37 @@
# FOR LOOPS
# this for loop starts at 0, stops at 5-1
for i in range(5):
print(i)
print() # print a blank line
# iterate letters in a text string
# first time through the loop the variable 'letter' is d, then o, then g
# letter is just a variable name. we can change it to whatever we want
for letter in "dog":
print(letter)
print()
# loop starts with 5 and continues to 10-1
for i in range(5, 10):
print (i**2) # print i squared
print()
# parameters for the range function: (start, stop-1, step)
# in this loop, start=10, end=20-1, step=3
# (every third number from 10 to 19)
for i in range(10, 20, 3):
print(i)
print()
# for loop iterates a list
# here the list is a list of text strings, bird names
# birds is our variable name for the list
# i is our variable name for each bird in the list inside the for loop.
birds = ['robin', 'sparrow', 'duck']
for i in birds:
print(len(i)) # print number of letters in each item