What is Python Nested for Loop?
Nested For Loop
Nested For Loops Loops can be nested in Python, as they can with other programming languages. The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion.
A for loop implements the repeated execution of code based on a loop counter or loop variable. This means that for loops are used most often when the number of iterations is known before entering the loop, unlike while loops which are conditionally based.
You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for loop.
Best Python Training Institute in Gurgaon...
0.0.1 Syntax:
for [first variable] in [outer loop]: # Outer loop [statement of outer loop] # Optional
for [second variable] in [nested loop]: # Nested loop [statement of netsted loop][1]:
Example:
ap2v_branch_list = ['Gurugram', 'Noida'] course_offerd = ['Python', 'DevOps', 'RHCE', 'AWS']
for institute in ap2v_branch_list: print('Branch Name:', institute) for course in course_offerd:
print('We provide:', course)
Branch Name: Gurgaon
We Provide: Python
We Provide: DevOps
We Provide: AWS
We Provide: RHCE
Branch Name: Noida
We Provide: Python
We Provide: DevOps
We Provide: AWS
We Provide: RHCE
The output illustrates that the program completes the first iteration of the outer loop by printing institute name, which then triggers completion of the inner loop, printing courses name consecutively of the inner loop has completed, the program returns to the top of the outer loop, prints again second item from list, then again prints the inner loop in its entirety all courses again.
Get More Information About Python Training and Certification Course Visit Here.
Thank You For Reading
Written By Sachin Saini
Comments
Post a Comment