A for loop executes a code block for each element in a container. A container is typically a list, tuple or a string.
Example for looping through a list of names:
for name in [‘Kanye’, ‘Jay’, ‘Beyonce’]:
print(f’Hello {name}!')
When you iterate over a dictionary using a for loop, it assigns the loop variable with the keys of the dictionary. The values are then accessed using the dictionary name followed by the key inside square brackets.
When you iterative over a string, you are looping through each character in the string
str = ’’
for character in ”I love underscores.":
str += character + '_’
print(str)
To loop backwards, use the reversed() function which reverses the order of element
Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
On this page of the site you can watch the video online Python - For Loop with Examples - Python Programming Tutorial - APPFICIAL with a duration of hours minute second in good quality, which was uploaded by the user Appficial 28 September 2021, share the link with friends and acquaintances, this video has already been watched 724 times on youtube and it was liked by 5 viewers. Enjoy your viewing!