Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python While Loop
Next article icon
Try it on GfG Practice
redirect icon

PythonFor Loops are used for iterating over a sequence like lists, tuples, strings, and ranges.

  • For loop allows you to apply the same operation to every item within loop.
  • Using For Loop avoid the need of manually managing the index.
  • For loop can iterate over any iterable object, such as dictionary, list or any custom iterators.

For Loop Example:

Python
s=["Geeks","for","Geeks"]# using for loop with stringforiins:print(i)

Output
GeeksforGeeks

Flowchart of Python For Loop

For Loops in Python
For Loop flowchart

Python For Loop Syntax

for var in iterable:

# statements

pass

Note:In Python, for loopsonlyimplement thecollection-based iteration.

Python For Loop with String

This code uses a for loop to iterate over astring and print each character on a new line. The loop assigns each character to the variable i and continues until all characters in the string have been processed.

Python
s="Geeks"foriins:print(i)

Output
Geeks

Using range() with For Loop

Therange() function is commonly used with for loops to generate a sequence of numbers. It can take one, two, or three arguments:

  • range(stop): Generates numbers from 0 to stop-1.
  • range(start, stop): Generates numbers from start to stop-1.
  • range(start, stop, step):Generates numbers from start to stop-1, incrementing by step.
Python
foriinrange(0,10,2):print(i)

Output
02468

Control Statements with For Loop

Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Continue with For Loop

Pythoncontinue Statement returns the control to the beginning of the loop.

Python
# Prints all letters except 'e' and 's'foriin'geeksforgeeks':ifi=='e'ori=='s':continueprint(i)

Output
gkforgk

Break with For Loop

Pythonbreak statement brings control out of the loop.

Python
foriin'geeksforgeeks':# break the loop as soon it sees 'e'# or 's'ifi=='e'ori=='s':breakprint(i)

Output
e

Pass Statement with For Loop

Thepass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.

Python
# An empty loopforiin'geeksforgeeks':passprint(i)

Output
s

Else Statement with For Loops

Python also allows us to use theelse condition for loops. The else block just after for/while is executed only when the loop is NOT terminated by a break statement.

Python
foriinrange(1,4):print(i)else:# Executed because no break in forprint("No Break\n")

Output
123No Break

Using Enumerate with for loop

In Python,enumerate() function is used with the for loop to iterate over an iterable while also keeping track of index of each item.

Python
li=["eat","sleep","repeat"]fori,jinenumerate(li):print(i,j)

Output
0 eat1 sleep2 repeat

Nested For Loops in Python

This code uses nested for loops to iterate over two ranges of numbers (1 to 3 inclusive) and prints the value of i and j for each combination of these two loops. \

The inner loop is executed for each value of i in outer loop. The output of this code will print the numbers from 1 to 3 three times, as each value of i is combined with each value of j.

Python
foriinrange(1,4):forjinrange(1,4):print(i,j)

Output
1 11 21 32 12 22 33 13 23 3

Python For LoopExercise Questions

Below are two Exercise Questions on Python for-loops. We have covered continue statement and range() function in these exercise questions.

Q1.Code to implement Continue statement in for-loop

Python
a=["shirt","sock","pants","sock","towel"]b=[]foriina:ifi=="sock":continueelse:print(f"Washing{i}")b.append("socks")print(f"Washing{b}")

Output
Washing shirtWashing pantsWashing towelWashing ['socks']

Q2.Code to implement range function in for-loop

Python
foriinrange(1,8):d=3+(i-1)*0.5print(f"Day{i}: Run{d:.1f} miles")

Output
Day 1: Run 3.0 milesDay 2: Run 3.5 milesDay 3: Run 4.0 milesDay 4: Run 4.5 milesDay 5: Run 5.0 milesDay 6: Run 5.5 milesDay 7: Run 6.0 miles

Test Your Knowledge -Python For Loop Quiz


For loops in Python
Visit Courseexplore course icon
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp