SlideShare a Scribd company logo
1 of 11
Download to read offline
LOOPS
Loops /
Iteration
Sometimes we need to repeat
certain things for a particular
number of times. For example, a
program has to display attendance
for every student of a class.
Here the program has to execute
the print statement for every
student. In programming, this kind
of repetition is called looping or
iteration
Loops
Conditional Loops
Counting Loops
The loop that repeat until
certain thing happens
eg:-While
The loop that repeat certain
number of times.
eg:-for
Types of Loops
for loop
ā— The for statement is used to iterate over
range of values or a sequence
ā— The loop executed each item in range,
The values can be numeric, string, List
or tuple.
ā— When all the items in the range are
exhausted, the statements within loop
are not executed and Python interpreter
starts executing the statements
immediately following the for loop.
ā— While using for loop, we should know in
advance the number of times the loop
will execute.
for <control-variable> in <sequence/items in range>:
<statements inside body of the loop>
Eg:-
Program to print even numbers in a given sequence using for loop.
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
for loop
range()
It is used to create a list containing a
sequence of integers.
from the given start value upto stop
value(excluding stop value), with a
difference of the given step value.
If start value is not speciļ¬ed, by default
the list starts from 0.
If step is also not speciļ¬ed, by default the
value is incremented by 1 in each
iteration.
range([start], stop[, step])
Example
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#start value is given as 2
>>> list(range(2, 10))
[2, 3, 4, 5, 6, 7, 8, 9]
#step value is 5 and start value is 0
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
#step value is -1. Hence, decreasing sequence is generated
>>> list(range(0, -9, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8]
Example
P
#Print multiples of 10 for numbers in a range
for num in range(5):
if num > 0:
print(num * 10)
Program 3-5 Program to print the multiples of 10 for
numbers in a given range.
Output:
10
20
30
40
A loop may contain another loop inside it.
A loop inside another loop is called a nested loop.
Nested loop
Example
Nested Loop
Let us code..

More Related Content

Similar to loops python.pdf

Python basics
Python basicsPython basics
Python basics
Fraboni Ec
Ā 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
Carlo Fanara
Ā 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
Ā 

Similar to loops python.pdf (20)

Python basics
Python basicsPython basics
Python basics
Ā 
Python basics
Python basicsPython basics
Python basics
Ā 
Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )Introduction to python programming ( part-3 )
Introduction to python programming ( part-3 )
Ā 
Loops_in_Rv1.2b
Loops_in_Rv1.2bLoops_in_Rv1.2b
Loops_in_Rv1.2b
Ā 
Slicing in Python - What is It?
Slicing in Python - What is It?Slicing in Python - What is It?
Slicing in Python - What is It?
Ā 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
Ā 
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPUR ESC 111M Lec12.pptx
Ā 
Python for loop
Python for loopPython for loop
Python for loop
Ā 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
Ā 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
Ā 
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Python Exam (Questions with Solutions Done By Live Exam Helper Experts)
Ā 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
Ā 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
Ā 
Python basics
Python basicsPython basics
Python basics
Ā 
Ch08
Ch08Ch08
Ch08
Ā 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Ā 
Introduction to Python Values, Variables Data Types Chapter 2
Introduction to Python  Values, Variables Data Types Chapter 2Introduction to Python  Values, Variables Data Types Chapter 2
Introduction to Python Values, Variables Data Types Chapter 2
Ā 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
Ā 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
Ā 
Python if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_StatementPython if_else_loop_Control_Flow_Statement
Python if_else_loop_Control_Flow_Statement
Ā 

More from MUHAMMED MASHAHIL PUKKUNNUMMAL

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
Ā 
flow of control python.pdf
flow of control python.pdfflow of control python.pdf
flow of control python.pdf
Ā 
Database Management using MS Access.pdf
Database Management using MS Access.pdfDatabase Management using MS Access.pdf
Database Management using MS Access.pdf
Ā 
CA-Web Hosting-Slide.pptx
CA-Web Hosting-Slide.pptxCA-Web Hosting-Slide.pptx
CA-Web Hosting-Slide.pptx
Ā 
flow of control python.pdf
flow of control python.pdfflow of control python.pdf
flow of control python.pdf
Ā 
java 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptxjava 4 Part 1 computer science.pptx
java 4 Part 1 computer science.pptx
Ā 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
Ā 
Java 2 computer science.pptx
Java 2 computer science.pptxJava 2 computer science.pptx
Java 2 computer science.pptx
Ā 
java part 1 computer science.pptx
java part 1 computer science.pptxjava part 1 computer science.pptx
java part 1 computer science.pptx
Ā 
2-Arrays.pdf
2-Arrays.pdf2-Arrays.pdf
2-Arrays.pdf
Ā 
Variable scope in php
Variable scope in phpVariable scope in php
Variable scope in php
Ā 
Vardump and printr in php
Vardump and printr in phpVardump and printr in php
Vardump and printr in php
Ā 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
Ā 
Unit1 DBMS Introduction
Unit1 DBMS IntroductionUnit1 DBMS Introduction
Unit1 DBMS Introduction
Ā 

Recently uploaded

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
Ā 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
Ā 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
Ā 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
Ā 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Ā 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Ā 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
Ā 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
Ā 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
Ā 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
Ā 
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
Ā 
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
Ā 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Ā 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
Ā 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
Ā 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Ā 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
Ā 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
Ā 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
Ā 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
Ā 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
Ā 

loops python.pdf

  • 2. Loops / Iteration Sometimes we need to repeat certain things for a particular number of times. For example, a program has to display attendance for every student of a class. Here the program has to execute the print statement for every student. In programming, this kind of repetition is called looping or iteration
  • 4. Conditional Loops Counting Loops The loop that repeat until certain thing happens eg:-While The loop that repeat certain number of times. eg:-for Types of Loops
  • 5. for loop ā— The for statement is used to iterate over range of values or a sequence ā— The loop executed each item in range, The values can be numeric, string, List or tuple. ā— When all the items in the range are exhausted, the statements within loop are not executed and Python interpreter starts executing the statements immediately following the for loop. ā— While using for loop, we should know in advance the number of times the loop will execute.
  • 6. for <control-variable> in <sequence/items in range>: <statements inside body of the loop> Eg:- Program to print even numbers in a given sequence using for loop. numbers = [1,2,3,4,5,6,7,8,9,10] for num in numbers: if (num % 2) == 0: print(num,'is an even Number') Output: 2 is an even Number 4 is an even Number 6 is an even Number 8 is an even Number 10 is an even Number for loop
  • 7. range() It is used to create a list containing a sequence of integers. from the given start value upto stop value(excluding stop value), with a difference of the given step value. If start value is not speciļ¬ed, by default the list starts from 0. If step is also not speciļ¬ed, by default the value is incremented by 1 in each iteration. range([start], stop[, step])
  • 8. Example >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] #start value is given as 2 >>> list(range(2, 10)) [2, 3, 4, 5, 6, 7, 8, 9] #step value is 5 and start value is 0 >>> list(range(0, 30, 5)) [0, 5, 10, 15, 20, 25] #step value is -1. Hence, decreasing sequence is generated >>> list(range(0, -9, -1)) [0, -1, -2, -3, -4, -5, -6, -7, -8] Example
  • 9. P #Print multiples of 10 for numbers in a range for num in range(5): if num > 0: print(num * 10) Program 3-5 Program to print the multiples of 10 for numbers in a given range. Output: 10 20 30 40
  • 10. A loop may contain another loop inside it. A loop inside another loop is called a nested loop. Nested loop Example Nested Loop