SlideShare a Scribd company logo
6
Most read
8
Most read
10
Most read
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 specified, by default
the list starts from 0.
If step is also not specified, 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

PPTX
Turing Test in Artificial Intelligence.pptx
PDF
Data Literacy Basics You Can Use Today.pdf
PPTX
Computability - Tractable, Intractable and Non-computable Function
PPTX
DataSructure-Time and Space Complexity.pptx
PPTX
Desicion tree and neural networks
PPT
Multidimensional array in C
PPTX
Machine Learning-Linear regression
PPTX
Types of inheritance in java
Turing Test in Artificial Intelligence.pptx
Data Literacy Basics You Can Use Today.pdf
Computability - Tractable, Intractable and Non-computable Function
DataSructure-Time and Space Complexity.pptx
Desicion tree and neural networks
Multidimensional array in C
Machine Learning-Linear regression
Types of inheritance in java

What's hot (20)

PPTX
Machine learning with ADA Boost
PPTX
AI: Learning in AI
PPTX
Multiclass classification of imbalanced data
PDF
Shortest Path in Graph
PPTX
Scalar expressions and control structures in perl
PPTX
Perl names values and variables
PPTX
Intelligent Agents
PPTX
Array in c++
PPT
prolog ppt
PPTX
PDF
Decision tree
PPTX
Forms of learning in ai
PPTX
Graph data structure and algorithms
PPTX
Data structure and algorithm
PPT
Lec 17 heap data structure
PPT
Machine Learning
PPTX
Parallel algorithms
PPTX
Types of environment in Artificial Intelligence
PPT
03 algorithm properties
Machine learning with ADA Boost
AI: Learning in AI
Multiclass classification of imbalanced data
Shortest Path in Graph
Scalar expressions and control structures in perl
Perl names values and variables
Intelligent Agents
Array in c++
prolog ppt
Decision tree
Forms of learning in ai
Graph data structure and algorithms
Data structure and algorithm
Lec 17 heap data structure
Machine Learning
Parallel algorithms
Types of environment in Artificial Intelligence
03 algorithm properties
Ad

Similar to loops python.pdf (20)

PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PPTX
PDF
2 Python Basics II meeting 2 tunghai university pdf
PPTX
python ppt.pptx
PPTX
Baabtra.com little coder chapter - 6
PPTX
Python for loop
PDF
Python_Module_2.pdf
PPTX
Python programming workshop session 2
PPTX
Loops in python including control statements and various test cases
PDF
While-For-loop in python used in college
PPTX
While_for_loop presententationin first year students
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
PPTX
Introduction-to-Iteration (2).pptx
PDF
From Beginner to Developer: Understanding Loops in Python
DOCX
An Introduction to Computer Science with Java, Python an.docx
PDF
011 LOOP.pdf cs project important chapters
PPTX
Python programming –part 3
PPT
Control structures pyhton
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
2 Python Basics II meeting 2 tunghai university pdf
python ppt.pptx
Baabtra.com little coder chapter - 6
Python for loop
Python_Module_2.pdf
Python programming workshop session 2
Loops in python including control statements and various test cases
While-For-loop in python used in college
While_for_loop presententationin first year students
Iterations FOR LOOP AND WHILE LOOP .pptx
Introduction-to-Iteration (2).pptx
From Beginner to Developer: Understanding Loops in Python
An Introduction to Computer Science with Java, Python an.docx
011 LOOP.pdf cs project important chapters
Python programming –part 3
Control structures pyhton
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
Ad

More from MUHAMMED MASHAHIL PUKKUNNUMMAL (14)

PPTX
IO and threads Java
PDF
flow of control python.pdf
PDF
Database Management using MS Access.pdf
PPTX
CA-Web Hosting-Slide.pptx
PDF
flow of control python.pdf
PPTX
java 4 Part 1 computer science.pptx
PPTX
Java 3 Computer Science.pptx
PPTX
Java 2 computer science.pptx
PPTX
java part 1 computer science.pptx
PPTX
Variable scope in php
PPTX
Vardump and printr in php
PPTX
Basic sql Commands
PPTX
Unit1 DBMS Introduction
IO and threads Java
flow of control python.pdf
Database Management using MS Access.pdf
CA-Web Hosting-Slide.pptx
flow of control python.pdf
java 4 Part 1 computer science.pptx
Java 3 Computer Science.pptx
Java 2 computer science.pptx
java part 1 computer science.pptx
Variable scope in php
Vardump and printr in php
Basic sql Commands
Unit1 DBMS Introduction

Recently uploaded (20)

PDF
Landforms and landscapes data surprise preview
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
Software Engineering BSC DS UNIT 1 .pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
PDF
Types of Literary Text: Poetry and Prose
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
High Ground Student Revision Booklet Preview
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
PPTX
Strengthening open access through collaboration: building connections with OP...
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Landforms and landscapes data surprise preview
How to Manage Bill Control Policy in Odoo 18
Software Engineering BSC DS UNIT 1 .pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Skill Development Program For Physiotherapy Students by SRY.pptx
Types of Literary Text: Poetry and Prose
Module 3: Health Systems Tutorial Slides S2 2025
High Ground Student Revision Booklet Preview
Cardiovascular Pharmacology for pharmacy students.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Strengthening open access through collaboration: building connections with OP...
How to Manage Loyalty Points in Odoo 18 Sales
Open Quiz Monsoon Mind Game Prelims.pptx
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
NOI Hackathon - Summer Edition - GreenThumber.pptx
How to Manage Starshipit in Odoo 18 - Odoo Slides
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...

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 specified, by default the list starts from 0. If step is also not specified, 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