#9 - Python For loops

#9 - Python For loops

By Ifeanyi Omeata


Topics:


1. For loops
2. For loops - in range
3. For loops - in range pool
4. For loops - in range pool (with steps)
5. For loops - in list
6. For loops - in list index
7. For loops - break
8. For loops - continue
9. For loops - Nested Loops
10. For loops - Birthday Invitation
11. For loops - Enumerate
12. For loops - Enumerate (start from number)


1. For loops


>>Return to Menu

for letter in 'Norwegian blue':
    print(letter)

print("For Loop done!")

image.png image.png


2. For loops - in range


>>Return to Menu

for furgle in range(8):
    print(furgle)

print("For Loop done!")

image.png image.png


3. For loops - in range pool


>>Return to Menu

for furgle in range(2,8):
    print(furgle)

print("For Loop done!")

image.png image.png


4. For loops - in range pool (with steps)


>>Return to Menu

for furgle in range(1,15,3):
    print(furgle)

print("For Loop done!")

image.png image.png


5. For loops - in list


>>Return to Menu

friends = ['John','Terry','Eric','Michael','George']
for friend in friends:
    print(friend)

print("For Loop done!")

image.png image.png


6. For loops - in list index


>>Return to Menu

friends = ['John','Terry','Eric','Michael','George']
for index in range(len(friends)):
   print(friends[index])

print("For Loop done!")

image.png image.png


7. For loops - break


>>Return to Menu

friends = ['John','Terry','Eric','Michael','George']
for friend in friends:
    if friend == 'Eric':
        print('Found' + friend + '!')
        break
    print(friend)

print("For Loop done!")

image.png image.png


8. For loops - continue


>>Return to Menu

friends = ['John','Terry','Eric','Michael','George']
for friend in friends:
    if friend == 'Eric':
        print('Found ' + friend + '!')
        continue
    print(friend)

print("For Loop done!")

image.png image.png


9. For loops - Nested Loops


>>Return to Menu

friends = ['John','Terry','Eric']
for friend in friends:
    for number in [1,2,3]:
        print(friend, number)

print("For Loop done!")

image.png image.png


10. For loops - Birthday Invitation


>>Return to Menu

names = ['john ClEEse','Eric IDLE','michael']
names1 = ['graHam chapman', 'TERRY', 'terry jones']
msg = 'You are invited to the party on saturday.'
#names.extend(names1)
names += names1
for index in range(2):
    names.append(input('Enter a new name: '))

for name in names:
    #msg1 = f'{name.title()}! {msg}'
    msg1 = name.title() + '! ' + msg
    print(msg1)

image.png image.png


11. For loops - Enumerate


>>Return to Menu

print('python101 - Enumerate')
friends = ['Brian', 'Judith', 'Reg', 'Loretta', 'Colin']

for num, friend in enumerate(friends):
    print(num, friend)

image.png image.png


12. For loops - Enumerate (start from number)


>>Return to Menu

print('python101 - Enumerate')
friends = ['Brian', 'Judith', 'Reg', 'Loretta', 'Colin']

for num, friend in enumerate(friends, 1):
    print(num, friend)
print('python101 - Enumerate')
friends = ['Brian', 'Judith', 'Reg', 'Loretta', 'Colin']

for num, friend in enumerate(friends, start = 1):
    print(num, friend)

image.png image.png

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022