38 lines
945 B
Plaintext
38 lines
945 B
Plaintext
# 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
|