Create for_loops
This commit is contained in:
31
python/for_loops
Normal file
31
python/for_loops
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# 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
|
||||||
Reference in New Issue
Block a user