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!
Auf dieser Seite können Sie das Online-Video Python - For Loop with Examples - Python Programming Tutorial - APPFICIAL mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Appficial 28 September 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 724 Mal angesehen und es wurde von 5 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!