Python: For Loops

Post Reply
User avatar
Jason
Site Admin
Posts: 749
Joined: Tue Jul 01, 2025 10:13 am

Code: Select all

vegetables = {"corn", "carrots", "jalapenos"}

for v in vegetables:

	print(v)
	
# List "vegetables" is declared and listed.

# The list is put into the "for variable" v and looped thru.

#  Loop "v" is printed.

# Note: any single letter can be used as the "for variable".

#  I like to choose ones which also start the main variable for readability. 

Code: Select all

for o in "okra":

	print(o)
	
# The letters in "okra" are put in the "for variable" o and looped thru

# Loop o is printed.
	
# Above prints out individual letters in the word "okra"
 

POSTREACT(ions) SUMMARY

Post Reply