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!