Skip to main content

Command Palette

Search for a command to run...

#9 - Python For loops

By Ifeanyi Omeata

Updated
3 min read
#9 - Python For loops
I

Hi, I am a Software Developer of 3-4 years specialising in React, Javascript, Node, NextJS, Express, Python, Django, Fast API, SQL and a few other technology stack, with a good background in Networking and Cloud Infrastructure. I am working to become a DevOps Solutions Engineer and happily married to my long time girlfriend.


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:
www.ifeanyiomeata.com
contact@ifeanyiomeata.com

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

LEARN PYTHON/DJANGO

Part 28 of 37

Learn Python and Django the easy way!..... PYTHON | DJANGO | FASTAPI

Up next

#8 - Python While loops

By Ifeanyi Omeata