EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials Python 3 Tutorial Python 3 For Loop

Python 3 For Loop

Updated March 30, 2023

Definition of Python 3 For Loop

Python 3 For loop statement is a little different than what we would be used to from C or Pascal. Rather than iterating over an arithmetic progression of numbers or allowing the user to set both the iteration step and the halting condition (as in C), Python’s for statement iterates over the items of any sequence (a list or a string) in the order they occur in the sequence.

Python 3 For Loop

ADVERTISEMENT
Popular Course in this category
PYTHON DJANGO Certification Course Bundle - 4 Courses in 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What is Python 3 For Loop?

  • A For loop is usually used with an iterable object, such as a list or a range. The For statement in Python iterates across the members of a sequence, executing the block each time.
  • Compare the For statement to the “while” loop, which is used when a condition needs to be verified each iteration or when a piece of code needs to be repeated indefinitely.

How to Use Python 3 For Loop?

When we have a block of code that we want to repeat a certain number of times, we use a For loop.

  • When it comes to iterating through a sequence, a For loop is employed. This behaves more like an iterator function found in other object-oriented programming languages than the keyword in other programming languages.
  • The For loop allows us to run a sequence of statements once for each item in a list, tuple, set, and so on.
  • A For loop can basically be used with any object that has an iterable method.
  • Even strings, despite the lack of an iterable technique but we won’t go into that right now. Having an iterable method simply means that the data may be displayed in a list format, with numerous values arranged in a logical sequence.

Below is the syntax of Python 3 For loop as follows.

Syntax:

for val (variable (in sequence))
		Loop body

Val is the variable that, on each iteration, takes the value of the item in the sequence.

The loop is repeated until the last item in the sequence is reached. Indentation is used to distinguish the body of the For loop from the remainder of the code.

The below steps shows how to use python 3 For loop are as follows.

In this step, we use python 3 to print the list of strings. In the below example, we are printing the list of names.

Code:

name = ["ABC", "PQR", "XYZ"]
for x in name:
    print (x)

Output:

List of Strings

In this step, we use python 3 to print the single string. In the below example, we are printing the single names.

Code:

for x in "ABCDEF":
    print (x)

Output:

Single String

In this step returning an iterator that progresses integers from 0 to n-1. It is typecast to list to get a list object of the sequence. The for statement can now be used to iterate this list.

Code:

for num in list (range (5)):
    print (num)

Output:

List Objects

The below step shows a list of values that was iterated through the range of values as follows.

Code:

for p in range(0,10): 
    print(p)

Output:

List of values

Python 3 For loop Data Types

Below is the python 3 For loop data types as follows.

1) Python 3 For loop using range data types

The range function is used in loops to control the number of times the loop is run. We can supply up to three integer arguments to the range when working with it.

The below example shows the use of python 3 For loop data types as follows.

Code:

for p in range(0,10):
    print(p)

Output:

Python 3 For Loop - Range data types

2) For loop using sequential data types

  • Iteration parameters For loops can be used with lists and other data sequence kinds. We can define a list and iterate through that instead of iterating through a range.
  • We’ll create a list and iterate through it. We’re publishing each item in the list in this example. We could have used any legal variable name instead of shark and had the same result.

The below example shows For loop using sequential data types as follows.

Code:

name = ['ABC', 'PQR', 'XYZ', 'CBZ', 'PQR']
for name in name:
    print(name)

Output:

Sequential Data Types

3) Nested For loops data types

  • A nested loop is a loop that is contained within another loop, comparable to nested if statements in structure.
  • The outer loop is encountered first, and the program executes its first iteration. The inner, nested loop is triggered by the first iteration, and it runs to completion.

Code:

num = [11, 12, 13]
alpha = ['abc', 'pqr', 'xyz']
for number in num:
    print(number)
    for letter in alpha:
        print(letter)

Output:

Python 3 Nested For Loop

Python 3 For loop Statement

  • A Python For loop can traverse over a list of items and perform a series of actions on each one. On each iteration of a For loop, a temporary value is assigned to a variable.
  • Remember to properly indent each action while writing a For loop, or else we will get an IndentationError.

Below is an example of For loop statement as follows.

Code:

num = 4
for p in range(0, num):
    print (p)

Output:

Python 3 For Loop Statement

It is also possible to iterate using the sequence’s index of elements. The fundamental concept is to calculate the list’s length first, then loop through the sequence within that range.

The below example shows the range functions which start with 0 as follows.

Code:

for y in range(10, 16):
    print (y)

Output:

Python 3 For Loop - Range functions

Conclusion

The range function is used in loops to control the number of times the loop is run. We can supply up to three integer arguments to the range when working with it. Python 3 For loop statement is a little different than what we would be used to from C.

Recommended Articles

This is a guide to Python 3 For Loop. Here we discuss the definition, What is python 3 For loop, How to use python 3 For loop? Examples with code implementation. You may also have a look at the following articles to learn more –

  1. For Loop in PLSQL
  2. Lua for loop
  3. Python Memory Error
  4. SQL For loop
ADVERTISEMENT
GOLANG Course Bundle - 6 Courses in 1
23+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
iOS DEVELOPER Course Bundle - 61 Courses in 1
147+ Hours of HD Videos
61 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests
56+ Hours of HD Videos
18 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
RED HAT LINUX Course Bundle - 5 Courses in 1
28+ Hours of HD Videos
5 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW