SlideShare a Scribd company logo
8
Most read
9
Most read
13
Most read
For Loops and Nesting
Looping Structures
For Loops
 For loops are a pre-test loop
 In order to utilize a for loop you need 3 things:
1. Needs to initialize a counter
2. Must test the counter variable
3. It must update the counter variable
 Code:
for initialization in range(start, stop,
increment):
statement1
statement2
Example
for i in range(0, 5, 1):
print(“Hello”)
Step 1: Perform the initialization
expression
Step 2: Evaluate the test
expressions
Step 3: Execute the body of
the loop
Step 4: Perform the
update
Assign 0 to i
i < 5
Update iPrint “Hello”
True
False
Reasons to use a for loop
 Know the number of times the loop should iterate
 Using a counter
 Need a false condition to terminate the loop
Demonstrating when to use a for
loop
num = 1;
while ( num <= 10):
print( num, “tt”, num * num)
num+=1;
for i in range(1, 10, 1):
print( num, “tt”, num * num)
Initialization expression
Initialization
expression
Test
Update
UpdateTest
Other Ways to Update
 Most programmers and books refer to the update
expression as the incrementor
 Not only can you increment the expression but
decrement as well
 Additionally you can increase and decrease the
variable by values other than 1
 i++ i+=1 i = i + 1
 i-- i -=1 i = i - 1
 i+=2
 i -=5
Determining Which Loop to
Choose
 Each of the 3 loops is ideal to use in different
situations:
 The while loop. The while loop is a pre-test
conditional loop. It is ideal in situations where you
do not want the loop to iterate if the condition is
false from the beginning. For example, checking
health in a game until someone dies
 The for loop. The for loop is a pretest loop that has
built in expressions for initializing, testing and
updating a counter. This makes it convenient to
control the number of times the loop executes. For
Nested Loops
 A nested loop is a loop inside the body of another
loop
 Inner (inside), outer (outside) loops:
for row in range(1, 3, 1) //outer
for col in range(1, 3, 1)//inner
print(row * col)
 Inner loop goes through all repetitions for each
repetition of outer loop
 Inner loop repetitions complete sooner than outer loop
 Total number of repetitions for inner loop is product of
number of repetitions of the two loops.
 Can nest different styles of loops together
Nesting Loops
 Nested loops are necessary when a task is performs
a repetitive operation and that task itself must be
repeated.
 for initialize in range(start, stop, update):
while(condition):
for initialize in range(start, stop, update):
while(condition):
 Just like when we worked with nested conditionals
 if (condition):
if (condition):
 For example, a digital clock is a good example of
needing to nest for loops.
Digital Clock
 How can we use loops to simulate a digital clock?
 What would each loop be responsible for keeping
track of?
 There would be 3 loops:
 One would track the hour
 One would track the minutes
 One would track the seconds
 NOT AN ANALOG CLOCK, why??????
 What would it look like?
Digital Clock
 First let’s begin by creating a loop to display the
seconds up to a minute.
 Can anyone describe that loop???
for sec in range(0, 60, 1):
print(format(sec, ‘>2’)
 Question: What is the initialization, test and update
for the previous for loop?
 Question: What would it look like if we printed to
the left with setw(2)?
Digital Clock
 Now let’s add minutes by nesting our previous loop:
for min in range(0, 60, 1):
for sec in range(0, 60, 1):
print(format(min, ‘>2’), ”:”, format(sec,
‘>2’)
Digital Clock
 Write out the digital clock code to print the hour
as well
Digital Clock
for hour in range(0, 12, 1):
for min in range(0, 60, 1):
for sec in range(0, 60, 1):
print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec,
‘>2’)
 Question: What if we wanted to add
AM/PM
Digital Clock
Assign 0 to
hour
hour <
12
Assign 0 to
sec
Assign 0 to
min
True
False
min <
60
False
True
sec <
60
Fals
True Print clock
Checkpoint:
1. Name the three expressions that appear inside
the parentheses(header) of the for loop.
2. You want to write a for loop that displays “I love
to program!” 50 times.
A. What initialization expression will you use?
B. What test expression will you use?
C. What update expression will you use?
D. Write the loop
3. What will the following code segments produce:
A. for count in range(0, 6, 1):
print(count + count)
A. for j in range(20, 14, -2):
print(j)
4. When should I use each type of loop?

More Related Content

PDF
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
PPTX
Introduction to the basics of Python programming (part 1)
PPTX
Python-Inheritance.pptx
PPT
Python ppt
PDF
Variables & Data Types In Python | Edureka
PPTX
Loops in Python
PPTX
Python Operators
Python Loops Tutorial | Python For Loop | While Loop Python | Python Training...
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Introduction to the basics of Python programming (part 1)
Python-Inheritance.pptx
Python ppt
Variables & Data Types In Python | Edureka
Loops in Python
Python Operators

What's hot (20)

PPTX
Functions in Python
PPTX
Introduction to Basics of Python
PDF
Python libraries
PPTX
Chapter 07 inheritance
PPTX
Looping statement in python
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPTX
CLASS OBJECT AND INHERITANCE IN PYTHON
PDF
What is Python Lambda Function? Python Tutorial | Edureka
PPTX
Python for loop
PDF
Python Basics | Python Tutorial | Edureka
PDF
Introduction To Python | Edureka
PDF
Python - gui programming (tkinter)
PPTX
Python 3 Programming Language
PDF
Python Programming Language | Python Classes | Python Tutorial | Python Train...
PDF
Datatypes in python
PPTX
Python Functions
PDF
Basic Concepts in Python
PDF
Functions in Python
Introduction to Basics of Python
Python libraries
Chapter 07 inheritance
Looping statement in python
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
CLASS OBJECT AND INHERITANCE IN PYTHON
What is Python Lambda Function? Python Tutorial | Edureka
Python for loop
Python Basics | Python Tutorial | Edureka
Introduction To Python | Edureka
Python - gui programming (tkinter)
Python 3 Programming Language
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Datatypes in python
Python Functions
Basic Concepts in Python
Ad

Viewers also liked (13)

PPT
Conditional Loops Python
PPT
Formatting Output
PPT
Intro to Lists
PPTX
Functions
PPTX
Function Returns
PPTX
Variables and Expressions
PPT
PPT
Intro to Python
PPTX
Logical Operators
PPT
Intro to Jeroo Python
PPTX
Computer Logic
PDF
DSL in Clojure
PPTX
Python - Lesson 1
Conditional Loops Python
Formatting Output
Intro to Lists
Functions
Function Returns
Variables and Expressions
Intro to Python
Logical Operators
Intro to Jeroo Python
Computer Logic
DSL in Clojure
Python - Lesson 1
Ad

Similar to For Loops and Nesting in Python (20)

PPTX
Nested Loops
PPTX
130707833146508191
PPT
Java Programming: Loops
PDF
2 Python Basics II meeting 2 tunghai university pdf
PPT
Java căn bản - Chapter6
PPTX
Loops in python including control statements and various test cases
PPTX
Loops in python including control statements and various test cases
PPTX
06.Loops
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PPT
Repetition Structure
PPT
Conditional Loops
PPTX
Cs1123 6 loops
PPTX
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
PDF
Notes2
PDF
Programming Fundamentals presentation slide
PPT
Iteration
PPT
Verilog Lecture3 hust 2014
PDF
DSA 103 Object Oriented Programming :: Week 3
PPT
industrial and production Line Balancing.ppt
DOCX
C++ Loops General Discussion of Loops A loop is a.docx
Nested Loops
130707833146508191
Java Programming: Loops
2 Python Basics II meeting 2 tunghai university pdf
Java căn bản - Chapter6
Loops in python including control statements and various test cases
Loops in python including control statements and various test cases
06.Loops
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
Repetition Structure
Conditional Loops
Cs1123 6 loops
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Notes2
Programming Fundamentals presentation slide
Iteration
Verilog Lecture3 hust 2014
DSA 103 Object Oriented Programming :: Week 3
industrial and production Line Balancing.ppt
C++ Loops General Discussion of Loops A loop is a.docx

More from primeteacher32 (20)

PPT
Software Development Life Cycle
PPTX
Variable Scope
PPTX
Returning Data
PPTX
Intro to Functions
PPTX
Introduction to GUIs with guizero
PPTX
Function Parameters
PPTX
Introduction to Repetition Structures
PPTX
Input Validation
PPTX
Windows File Systems
PPTX
Nesting Conditionals
PPTX
Conditionals
PPT
Intro to Python with GPIO
PPTX
Variables and Statements
PPTX
Variables and User Input
PPT
Intro to Python
PPTX
Raspberry Pi
PPT
Hardware vs. Software Presentations
PPTX
Block chain security
PPTX
PPTX
System Administration
Software Development Life Cycle
Variable Scope
Returning Data
Intro to Functions
Introduction to GUIs with guizero
Function Parameters
Introduction to Repetition Structures
Input Validation
Windows File Systems
Nesting Conditionals
Conditionals
Intro to Python with GPIO
Variables and Statements
Variables and User Input
Intro to Python
Raspberry Pi
Hardware vs. Software Presentations
Block chain security
System Administration

Recently uploaded (20)

PDF
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
PPTX
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
PDF
Biography of Mohammad Anamul Haque Nayan
DOCX
mcsp232projectguidelinesjan2023 (1).docx
PDF
esg-supply-chain-webinar-nov2018hkhkkh.pdf
PPTX
A slide for students with the advantagea
PDF
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
PPTX
The Stock at arrangement the stock and product.pptx
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PDF
Beginner’s Guide to Digital Marketing.pdf
PDF
iTop VPN Crack Latest Version 2025 Free Download With Keygen
PPTX
Condensed_Food_Science_Lecture1_Precised.pptx
PDF
APNCET2025RESULT Result Result 2025 2025
PDF
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
PDF
CV of Architect Professor A F M Mohiuddin Akhand.pdf
PPTX
Your Guide to a Winning Interview Aug 2025.
PPT
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
PPTX
Principles of Inheritance and variation class 12.pptx
PPTX
Definition and Relation of Food Science( Lecture1).pptx
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
Nervous_System_Drugs_PPT.pptxXXXXXXXXXXXXXXXXX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
Biography of Mohammad Anamul Haque Nayan
mcsp232projectguidelinesjan2023 (1).docx
esg-supply-chain-webinar-nov2018hkhkkh.pdf
A slide for students with the advantagea
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
The Stock at arrangement the stock and product.pptx
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
Beginner’s Guide to Digital Marketing.pdf
iTop VPN Crack Latest Version 2025 Free Download With Keygen
Condensed_Food_Science_Lecture1_Precised.pptx
APNCET2025RESULT Result Result 2025 2025
servsafecomprehensive-ppt-full-140617222538-phpapp01.pdf
CV of Architect Professor A F M Mohiuddin Akhand.pdf
Your Guide to a Winning Interview Aug 2025.
ALLIED MATHEMATICS -I UNIT III MATRICES.ppt
Principles of Inheritance and variation class 12.pptx
Definition and Relation of Food Science( Lecture1).pptx

For Loops and Nesting in Python

  • 1. For Loops and Nesting Looping Structures
  • 2. For Loops  For loops are a pre-test loop  In order to utilize a for loop you need 3 things: 1. Needs to initialize a counter 2. Must test the counter variable 3. It must update the counter variable  Code: for initialization in range(start, stop, increment): statement1 statement2
  • 3. Example for i in range(0, 5, 1): print(“Hello”) Step 1: Perform the initialization expression Step 2: Evaluate the test expressions Step 3: Execute the body of the loop Step 4: Perform the update Assign 0 to i i < 5 Update iPrint “Hello” True False
  • 4. Reasons to use a for loop  Know the number of times the loop should iterate  Using a counter  Need a false condition to terminate the loop
  • 5. Demonstrating when to use a for loop num = 1; while ( num <= 10): print( num, “tt”, num * num) num+=1; for i in range(1, 10, 1): print( num, “tt”, num * num) Initialization expression Initialization expression Test Update UpdateTest
  • 6. Other Ways to Update  Most programmers and books refer to the update expression as the incrementor  Not only can you increment the expression but decrement as well  Additionally you can increase and decrease the variable by values other than 1  i++ i+=1 i = i + 1  i-- i -=1 i = i - 1  i+=2  i -=5
  • 7. Determining Which Loop to Choose  Each of the 3 loops is ideal to use in different situations:  The while loop. The while loop is a pre-test conditional loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. For example, checking health in a game until someone dies  The for loop. The for loop is a pretest loop that has built in expressions for initializing, testing and updating a counter. This makes it convenient to control the number of times the loop executes. For
  • 8. Nested Loops  A nested loop is a loop inside the body of another loop  Inner (inside), outer (outside) loops: for row in range(1, 3, 1) //outer for col in range(1, 3, 1)//inner print(row * col)  Inner loop goes through all repetitions for each repetition of outer loop  Inner loop repetitions complete sooner than outer loop  Total number of repetitions for inner loop is product of number of repetitions of the two loops.  Can nest different styles of loops together
  • 9. Nesting Loops  Nested loops are necessary when a task is performs a repetitive operation and that task itself must be repeated.  for initialize in range(start, stop, update): while(condition): for initialize in range(start, stop, update): while(condition):  Just like when we worked with nested conditionals  if (condition): if (condition):  For example, a digital clock is a good example of needing to nest for loops.
  • 10. Digital Clock  How can we use loops to simulate a digital clock?  What would each loop be responsible for keeping track of?  There would be 3 loops:  One would track the hour  One would track the minutes  One would track the seconds  NOT AN ANALOG CLOCK, why??????  What would it look like?
  • 11. Digital Clock  First let’s begin by creating a loop to display the seconds up to a minute.  Can anyone describe that loop??? for sec in range(0, 60, 1): print(format(sec, ‘>2’)  Question: What is the initialization, test and update for the previous for loop?  Question: What would it look like if we printed to the left with setw(2)?
  • 12. Digital Clock  Now let’s add minutes by nesting our previous loop: for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(min, ‘>2’), ”:”, format(sec, ‘>2’)
  • 13. Digital Clock  Write out the digital clock code to print the hour as well
  • 14. Digital Clock for hour in range(0, 12, 1): for min in range(0, 60, 1): for sec in range(0, 60, 1): print(format(hour, ‘>2’), ”:”, format(min, ‘>2’), ”:”, format(sec, ‘>2’)  Question: What if we wanted to add AM/PM
  • 15. Digital Clock Assign 0 to hour hour < 12 Assign 0 to sec Assign 0 to min True False min < 60 False True sec < 60 Fals True Print clock
  • 16. Checkpoint: 1. Name the three expressions that appear inside the parentheses(header) of the for loop. 2. You want to write a for loop that displays “I love to program!” 50 times. A. What initialization expression will you use? B. What test expression will you use? C. What update expression will you use? D. Write the loop 3. What will the following code segments produce: A. for count in range(0, 6, 1): print(count + count) A. for j in range(20, 14, -2): print(j) 4. When should I use each type of loop?

Editor's Notes

  • #9: Think of it as searching a hotel. The outer loop operates the elevator and the inner loop searches the hotel rooms on each floor. Each loop controls a different aspect of the program.