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!
En esta página del sitio puede ver el video en línea Python - For Loop with Examples - Python Programming Tutorial - APPFICIAL de Duración hora minuto segunda en buena calidad , que subió el usuario Appficial 28 septiembre 2021, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 724 veces y le gustó 5 a los espectadores. Disfruta viendo!