SlideShare a Scribd company logo
1 of 8
1
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
Python For Loop
2
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
Loops let you control the logic and flow structures of your
programs.
Specifically, a for loop lets you execute a block of similar code
operations, over and over again, until a condition is met.
You repeat certain code instructions for a set of values you
determine, and you perform actions on each value for a pre-
determined number of times.
3
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
A for loop can iterate over every item in a list or go through
every single character in a string and won't stop until it has
gone through every character.
Writing for loops helps reduce repetitiveness in your code,
following the DRY (Don't Repeat Yourself) principle. You don't
write the same block of code more than once.
4
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
The for loop generally keeps track of three things:
The initialization expression statement which is exactuted once, let i = 0;
The condition that needs to be met, i < 10;. This condition is evaluated as either
true or false. If it is false, the loop is terminated.
If the condition is true the body of the loop will be executed and the initialized
expression will take some action. In this case it will be incremented by 1 (i++), until
the condition set is met.
for i in range(0,5):
print(i)
5
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
One of Python’s built-in immutable sequence types is range(). In loops,
range() is used to control how many times the loop will be repeated.
When working with range(), you can pass between 1 and 3 integer
arguments to it:
start states the integer value at which the sequence begins, if this is not
included then start begins at 0
stop is always required and is the integer that is counted up to but not
included
step sets how much to increase (or decrease in the case of negative
numbers) the next iteration, if this is omitted then step defaults to 1.
for i in range(6):
print(i)
6
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
Next, we’ll look at range(start, stop), with values passed for
when the iteration should start and for when it should stop:
for i in range(20,25):
print(i)
7
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
With all three arguments, step comes in the final position: range(start, stop, step).
First, let’s use a step with a positive value:
in this case, the for loop is set up so that the numbers from 0 to 15 print out, but
at a step of 3, so that only every third number is printed, like so:
for i in range(0,15,3):
print(i)
8
www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com
Python
For
Loop
Chapter
22
We can also use a negative value for our step argument to iterate
backwards, but we’ll have to adjust our start and stop arguments
accordingly:
Here, 100 is the start value, 0 is the stop value, and -10 is the range, so
the loop begins at 100 and ends at 0, decreasing by 10 with each iteration.
This occurs in the output:
for i in range(100,0,-10):
print(i)

More Related Content

Similar to Python Functions.pptx

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
Amy Nightingale
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7
Vishal Dutt
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
SukhpreetSingh519414
 

Similar to Python Functions.pptx (20)

This is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptxThis is all about control flow in python intruducing the Break and Continue.pptx
This is all about control flow in python intruducing the Break and Continue.pptx
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdf
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
Going loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptxGoing loopy - Introduction to Loops.pptx
Going loopy - Introduction to Loops.pptx
 
Introduction to Python Part-1
Introduction to Python Part-1Introduction to Python Part-1
Introduction to Python Part-1
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7
 
Chapter08.pptx
Chapter08.pptxChapter08.pptx
Chapter08.pptx
 
Control Structures in Python
Control Structures in PythonControl Structures in Python
Control Structures in Python
 
Python Math Concepts Book
Python Math Concepts BookPython Math Concepts Book
Python Math Concepts Book
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
ppt python notes list tuple data types ope
ppt python notes list tuple data types opeppt python notes list tuple data types ope
ppt python notes list tuple data types ope
 
C# loops
C# loopsC# loops
C# loops
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
 
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
 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
 
Lect 3-4 Zaheer Abbas
Lect 3-4 Zaheer AbbasLect 3-4 Zaheer Abbas
Lect 3-4 Zaheer Abbas
 
Looping statements
Looping statementsLooping statements
Looping statements
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
 

More from MrSaem (8)

pseudocode practice.pptx
pseudocode practice.pptxpseudocode practice.pptx
pseudocode practice.pptx
 
Backus Naur Form.pptx
Backus Naur Form.pptxBackus Naur Form.pptx
Backus Naur Form.pptx
 
3 Monitoring and control.pptx
3 Monitoring and control.pptx3 Monitoring and control.pptx
3 Monitoring and control.pptx
 
6 The digital divide.pptx
6 The digital divide.pptx6 The digital divide.pptx
6 The digital divide.pptx
 
Chapter 6 Python Fundamentals.pptx
Chapter 6 Python Fundamentals.pptxChapter 6 Python Fundamentals.pptx
Chapter 6 Python Fundamentals.pptx
 
Chapter 8 system soft ware
Chapter 8 system soft wareChapter 8 system soft ware
Chapter 8 system soft ware
 
Adt
AdtAdt
Adt
 
Recursion
RecursionRecursion
Recursion
 

Recently uploaded

Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Recently uploaded (20)

ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!State of the Smart Building Startup Landscape 2024!
State of the Smart Building Startup Landscape 2024!
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
(Explainable) Data-Centric AI: what are you explaininhg, and to whom?
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
الأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهلهالأمن السيبراني - ما لا يسع للمستخدم جهله
الأمن السيبراني - ما لا يسع للمستخدم جهله
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
The Ultimate Prompt Engineering Guide for Generative AI: Get the Most Out of ...
 

Python Functions.pptx

  • 1. 1 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 Python For Loop
  • 2. 2 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 Loops let you control the logic and flow structures of your programs. Specifically, a for loop lets you execute a block of similar code operations, over and over again, until a condition is met. You repeat certain code instructions for a set of values you determine, and you perform actions on each value for a pre- determined number of times.
  • 3. 3 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 A for loop can iterate over every item in a list or go through every single character in a string and won't stop until it has gone through every character. Writing for loops helps reduce repetitiveness in your code, following the DRY (Don't Repeat Yourself) principle. You don't write the same block of code more than once.
  • 4. 4 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 The for loop generally keeps track of three things: The initialization expression statement which is exactuted once, let i = 0; The condition that needs to be met, i < 10;. This condition is evaluated as either true or false. If it is false, the loop is terminated. If the condition is true the body of the loop will be executed and the initialized expression will take some action. In this case it will be incremented by 1 (i++), until the condition set is met. for i in range(0,5): print(i)
  • 5. 5 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 One of Python’s built-in immutable sequence types is range(). In loops, range() is used to control how many times the loop will be repeated. When working with range(), you can pass between 1 and 3 integer arguments to it: start states the integer value at which the sequence begins, if this is not included then start begins at 0 stop is always required and is the integer that is counted up to but not included step sets how much to increase (or decrease in the case of negative numbers) the next iteration, if this is omitted then step defaults to 1. for i in range(6): print(i)
  • 6. 6 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 Next, we’ll look at range(start, stop), with values passed for when the iteration should start and for when it should stop: for i in range(20,25): print(i)
  • 7. 7 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 With all three arguments, step comes in the final position: range(start, stop, step). First, let’s use a step with a positive value: in this case, the for loop is set up so that the numbers from 0 to 15 print out, but at a step of 3, so that only every third number is printed, like so: for i in range(0,15,3): print(i)
  • 8. 8 www.mrsaem.com | www.sirsaem.com | mrsaem@yahoo.com Python For Loop Chapter 22 We can also use a negative value for our step argument to iterate backwards, but we’ll have to adjust our start and stop arguments accordingly: Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at 100 and ends at 0, decreasing by 10 with each iteration. This occurs in the output: for i in range(100,0,-10): print(i)