SlideShare a Scribd company logo
1 of 14
Loops in Python
Programming Language
Submitted By:
Maghfoor Ahmad
MCA TYC 2nd Sem
Roll No: 27512100052
Introduction
Sometimes, there is a need to tell a computer
to repeat an action without writing separate
code for each repetition. In programming, this
concept is called loops.
Loops
A loop can be used to tell a program to execute
statements repeatedly. Or we can say that a loop
repeatedly executes the same set of instructions
until a termination condition is met.
Loops changes the control flow of program
that’s why they are also called Control Structure.
Why is it important?
Suppose that you need to display a
string (e.g., Good Morning! ) a
hundred times.
It would be tedious to write this
statement a hundred times:
print(“Good Morning!“)
print(“Good Morning!“)
print(“Good Morning!“)
.
.
.
.
print(“Good Morning!“)
Fortunately, We have loops. Using a loop control statement, you simply tell the
computer to display a string a hundred times without having to code the print
statement a hundred times, as follows:
for i in range (0,100):
print(“Good Morning!")
This will write the Good Morning! hundred times as follows:
Good Morning!
Good Morning!
Good Morning!
Good Morning!
Good Morning!
.
.
.
Good Morning!
Good Morning!
Output:
Python programming language provides
following types of loops to handle looping
requirements. They are:
● While Loop
● For Loop
● Nested Loop
Let’s understand each of the loops
mentioned above.
Type of Loops
While Loop
A while loop statement in Python programming language repeatedly executes a
block of statement as long as a given condition is true.
while expression:
statement(s)
Here, statement(s) may be a single statement
or a block of statements. The expression may
be any condition.
The loop iterates while the condition is true.
When the condition becomes false, program
control passes to the line immediately
following the loop.
The syntax of a while loop in Python
programming language is:
Source: Tutorials Point
Example of while Loop
i=1
#The while loop will iterate until condition becomes false.
While(i<=10):
print(i)
i=i+1
Program to print 1 to 10 using while loop
1
2
3
4
5
6
7
8
9
10
Output:
for Loop
A for loop is used for iterating over a sequence (that is either a range, a list, a
tuple, a dictionary, a set, or a string)
for iterating_var in sequence:
statement(s)
Here, the iterating_var can be either an iterable
or a sequence item. If it is an index, we use
the range() function instead of the sequence.
Otherwise, we can mention the sequence.
For each item in sequence, the statements block
will be executed until there are no item. After no
items left in sequence the control will be
transferred to code following the loop.
The syntax of a for loop in Python is:
Source: Tutorials Point
Example of for Loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
LIST: Program to print each fruit in a fruit list:
apple
banana
cherry
Output:
for i in range(0,3):
print("Hello")
Range: Program to print “Hello” 3 times:
Hello
Hello
Hello
Output:
Nested Loops
A nested loop is a loop inside the body of a loop. It can be for loop inside a for loop
or while inside a while loop. It can also be for inside a while loop and vice versa.
#Outer Loop
for iterating_var1 in sequence1:
#Inner Loop
for iterating_var2 in sequence2:
statement(s)
For each iteration, the outer loop will executes once and the inner loop will
executes till completion.
The nested loops can be understood by following examples:
● Printing multiplication table of numbers (1 to 3)
● Printing a matrix of 3 column and 2 rows
The syntax of a nested loop in Python programming language is:
#Outer Loop
while expression:
#Inner Loop
while expression:
statement(s)
statement(s)
Example of Nested while Loop
i=1
while i<=3: #Outer Loop
j=1
while j<=5: #Inner Loop
print i,"*",j,'=',i*j
j+=1 #Inner Loop Ends
i+=1
print "n" #Outer Loop Ends
Printing multiplication table up to 5 of numbers (1 to 3)
1 * 1 = 1 2 * 1 = 2 3 * 1 = 3
1 * 2 = 2 2 * 2 = 4 3 * 2 = 6
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15
Output:
Example of Nested for Loop
for row in range(2):
for col in range(3):
print("1", end= " ")
print("n")
Printing a matrix of 3 column and 2 rows
1 1 1
1 1 1
Output:
while Loop for Loop
1.
While loop is used when the
number of iteration is not known.
For loop is used when the
number of iteration is known.
2.
It shows an error if no condition is
given.
It iterates infinite time, if no
condition is given.
3.
Initialization, condition checking,
and iteration statements are
written at the top.
Only initialization and condition
checking is written at the top
Key Differences
Thankyou!
1. Reddy, Priyanka. “Introduction to Loops in Coding”. Codingal, 2021.
https://www.codingal.com/blog/coding-for-kids/loops-in-programming/
2. Ansari, Rumman. “C Programming Loop Introduction”. Atnyla, 2019.
https://www.atnyla.com/tutorial/loop-introduction-in-c/1/194
3. Gupta, Raghav, Nishant, Priyanka Gupta, and Naveen Sharma. Computational
Problem Solving With Programming In Python. Kalyani Publishers, 2019.
4. “Pyhton - Loops”. Tutorials Point.
https://www.tutorialspoint.com/python/python_loops.htm
5. “Pyhton while Loop Statements”. Tutorials Point.
https://www.tutorialspoint.com/python/python_loops.htm
6. “Python For Loops”. W3 Schools.
https://www.w3schools.com/python/python_for_loops.asp
7. Vishal. “Nested Loops in Python”. Pynative, 2021.
https://pynative.com/python-nested-loops/#h-what-is-a-nested-loop-in-python
Bibliography

More Related Content

What's hot (20)

Python for loop
Python for loopPython for loop
Python for loop
 
Python list
Python listPython list
Python list
 
Types of Statements in Python Programming Language
Types of Statements in Python Programming LanguageTypes of Statements in Python Programming Language
Types of Statements in Python Programming Language
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Python programming : Control statements
Python programming : Control statementsPython programming : Control statements
Python programming : Control statements
 
Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3Zero to Hero - Introduction to Python3
Zero to Hero - Introduction to Python3
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Data types in python
Data types in pythonData types in python
Data types in python
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 

Similar to Loops in Python.pptx

Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMPjbp4444
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
Python programing
Python programingPython programing
Python programinghamzagame
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRKrishna Raj
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitionsaltwirqi
 

Similar to Loops in Python.pptx (20)

Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
While loop
While loopWhile loop
While loop
 
Control structures pyhton
Control structures  pyhtonControl structures  pyhton
Control structures pyhton
 
Python Loop
Python LoopPython Loop
Python Loop
 
Iteration
IterationIteration
Iteration
 
Loops c++
Loops c++Loops c++
Loops c++
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Matlab Script - Loop Control
Matlab Script - Loop ControlMatlab Script - Loop Control
Matlab Script - Loop Control
 
Python programing
Python programingPython programing
Python programing
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 

Recently uploaded

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 

Loops in Python.pptx

  • 1. Loops in Python Programming Language Submitted By: Maghfoor Ahmad MCA TYC 2nd Sem Roll No: 27512100052
  • 2. Introduction Sometimes, there is a need to tell a computer to repeat an action without writing separate code for each repetition. In programming, this concept is called loops. Loops A loop can be used to tell a program to execute statements repeatedly. Or we can say that a loop repeatedly executes the same set of instructions until a termination condition is met. Loops changes the control flow of program that’s why they are also called Control Structure.
  • 3. Why is it important? Suppose that you need to display a string (e.g., Good Morning! ) a hundred times. It would be tedious to write this statement a hundred times: print(“Good Morning!“) print(“Good Morning!“) print(“Good Morning!“) . . . . print(“Good Morning!“)
  • 4. Fortunately, We have loops. Using a loop control statement, you simply tell the computer to display a string a hundred times without having to code the print statement a hundred times, as follows: for i in range (0,100): print(“Good Morning!") This will write the Good Morning! hundred times as follows: Good Morning! Good Morning! Good Morning! Good Morning! Good Morning! . . . Good Morning! Good Morning! Output:
  • 5. Python programming language provides following types of loops to handle looping requirements. They are: ● While Loop ● For Loop ● Nested Loop Let’s understand each of the loops mentioned above. Type of Loops
  • 6. While Loop A while loop statement in Python programming language repeatedly executes a block of statement as long as a given condition is true. while expression: statement(s) Here, statement(s) may be a single statement or a block of statements. The expression may be any condition. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop. The syntax of a while loop in Python programming language is: Source: Tutorials Point
  • 7. Example of while Loop i=1 #The while loop will iterate until condition becomes false. While(i<=10): print(i) i=i+1 Program to print 1 to 10 using while loop 1 2 3 4 5 6 7 8 9 10 Output:
  • 8. for Loop A for loop is used for iterating over a sequence (that is either a range, a list, a tuple, a dictionary, a set, or a string) for iterating_var in sequence: statement(s) Here, the iterating_var can be either an iterable or a sequence item. If it is an index, we use the range() function instead of the sequence. Otherwise, we can mention the sequence. For each item in sequence, the statements block will be executed until there are no item. After no items left in sequence the control will be transferred to code following the loop. The syntax of a for loop in Python is: Source: Tutorials Point
  • 9. Example of for Loop fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) LIST: Program to print each fruit in a fruit list: apple banana cherry Output: for i in range(0,3): print("Hello") Range: Program to print “Hello” 3 times: Hello Hello Hello Output:
  • 10. Nested Loops A nested loop is a loop inside the body of a loop. It can be for loop inside a for loop or while inside a while loop. It can also be for inside a while loop and vice versa. #Outer Loop for iterating_var1 in sequence1: #Inner Loop for iterating_var2 in sequence2: statement(s) For each iteration, the outer loop will executes once and the inner loop will executes till completion. The nested loops can be understood by following examples: ● Printing multiplication table of numbers (1 to 3) ● Printing a matrix of 3 column and 2 rows The syntax of a nested loop in Python programming language is: #Outer Loop while expression: #Inner Loop while expression: statement(s) statement(s)
  • 11. Example of Nested while Loop i=1 while i<=3: #Outer Loop j=1 while j<=5: #Inner Loop print i,"*",j,'=',i*j j+=1 #Inner Loop Ends i+=1 print "n" #Outer Loop Ends Printing multiplication table up to 5 of numbers (1 to 3) 1 * 1 = 1 2 * 1 = 2 3 * 1 = 3 1 * 2 = 2 2 * 2 = 4 3 * 2 = 6 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 Output:
  • 12. Example of Nested for Loop for row in range(2): for col in range(3): print("1", end= " ") print("n") Printing a matrix of 3 column and 2 rows 1 1 1 1 1 1 Output: while Loop for Loop 1. While loop is used when the number of iteration is not known. For loop is used when the number of iteration is known. 2. It shows an error if no condition is given. It iterates infinite time, if no condition is given. 3. Initialization, condition checking, and iteration statements are written at the top. Only initialization and condition checking is written at the top Key Differences
  • 14. 1. Reddy, Priyanka. “Introduction to Loops in Coding”. Codingal, 2021. https://www.codingal.com/blog/coding-for-kids/loops-in-programming/ 2. Ansari, Rumman. “C Programming Loop Introduction”. Atnyla, 2019. https://www.atnyla.com/tutorial/loop-introduction-in-c/1/194 3. Gupta, Raghav, Nishant, Priyanka Gupta, and Naveen Sharma. Computational Problem Solving With Programming In Python. Kalyani Publishers, 2019. 4. “Pyhton - Loops”. Tutorials Point. https://www.tutorialspoint.com/python/python_loops.htm 5. “Pyhton while Loop Statements”. Tutorials Point. https://www.tutorialspoint.com/python/python_loops.htm 6. “Python For Loops”. W3 Schools. https://www.w3schools.com/python/python_for_loops.asp 7. Vishal. “Nested Loops in Python”. Pynative, 2021. https://pynative.com/python-nested-loops/#h-what-is-a-nested-loop-in-python Bibliography