+91 81900 98289

Python For Loops

This Python For Loops lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

A for loop walks a sequence. Each pass binds the next item to the name you chose.

range(stop) yields numbers from 0 up to, but not including, stop. A second argument sets the start.

Example 1: Start here

Do not add or remove items from the list you are looping over.

for skill in ["Python", "SQL", "Git"]: # repeat the indented lines once per value print(skill)

Example 2: Loop a list

The name skill takes one item on each pass.

for skill in ["Python", "SQL", "Git"]: # repeat the indented lines once per value print(skill)

Example 3: Loop a range

range(1, 4) yields 1, 2, and 3. The stop value is not included.

for n in range(1, 4): # repeat once per number; range stops before the last print(n)

Example 4: Index and value

enumerate gives you the position and the item together.

skills = ["Python", "SQL"] # a list; the values stay in this order for index, skill in enumerate(skills, start=1): # repeat the indented lines once per value print(index, skill) # a comma prints each value, with a space between
Do not add or remove items from the list you are looping over.
Call Now 81900 98289