0% found this document useful (0 votes)
73 views

Python Loop Statement: By: Abhishek Saikia Class: 12 (C) Roll No: 17

The document discusses Python for loops. It explains that a for loop repeats a block of code a fixed number of times based on an iterable sequence like a list. The basic syntax of a for loop includes a variable, the in keyword, and the sequence. The body of the loop is indented and executed once for each item in the sequence. It also describes using the range() function to iterate a specific number of times, and using break, continue, else, nested loops, and pass statements with for loops.

Uploaded by

Madhurya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Python Loop Statement: By: Abhishek Saikia Class: 12 (C) Roll No: 17

The document discusses Python for loops. It explains that a for loop repeats a block of code a fixed number of times based on an iterable sequence like a list. The basic syntax of a for loop includes a variable, the in keyword, and the sequence. The body of the loop is indented and executed once for each item in the sequence. It also describes using the range() function to iterate a specific number of times, and using break, continue, else, nested loops, and pass statements with for loops.

Uploaded by

Madhurya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON LOOP STATEMENT

By: Abhishek Saikia


Class : 12 (C)
Roll no: 17
FOR LOOP

The simplest form of repetition is a for Loop. – Remember from Alice LOOP
repeated a fixed number of times. In Python that is called a for loop
The basic syntax of a for-loop in Python is:
• for <variable> in <sequence>:
<do something>
<do something>
....
FOR LOOP

•The loop specification begins with the keyword for which is followed by a
<variable> and then the keyword in and a <sequence> followed by a colon(:)
- This line sets up the number of times the repetition will be repeated.
•What follows is a set of statements, indented (again, indentation is important),
that are called a block that forms the body of the loop (stuff that is repeated).
FOR LOOP

• When a for loop is executed, the <variable> (which is called a loop index variable) is assigned successive
values in the <sequence> and for each of those values, the statements in the body of the loop are executed.
• - A <sequence> in Python is a list of values
• To execute a loop a certain number of times you can use the range() function. – To see what this function
does you can start IDLE and type:
>>> range (10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 91 – The range function simply returns a list of numbers from 0 to one less than the
number passed to the function
FOR LOOP EXAMPLE

If I have a dance function

• - I want to repeat it 10 timespeed,


for i in range (10):
dance ()
-i is a variable. Each time the loop is executed i has a
different value (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
LOOPING FOREVER

•In writing robot programs there will be times when you just want the robot to keep doing its behaviors
forever. While technically by forever we do mean eternity in reality what is likely to happen is either it runs
out of batteries, you decide to stop it (by hitting CTRL-C), or the period ends and you turn it off.
• The Python command to specify this uses a different loop statement, called a while-loop that can be written
as:
while True:
<do something>
<do something>
....
We will cover more of while-loops later.
PYTHON FOR LOOPS

A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated programming
languages.
• With the for loop we can execute a set of statements, once for each item in a
list, tuple, set etc.
• The for loop does not require an indexing variable to set beforehand.
LOOPING THROUGH A STRING

• Even strings are iterable objects, they contain a sequence of characters:


• Example
• Loop through the letters in the word "banana":
THE BREAK STATEMENT

With the break statement we can stop the loop before it has looped through all the
items:
Example
• Exit the loop when x is “banana”:

Example
• Exit the loop when x is “banana”, but this time the break comes before the print
THE CONTINUE STATEMENT

•With the continue statement we can stop the current iteration of the loop, and
continue with the next:
•Example: Do not print banana:

The range() Function


To loop through a set of code a specified number of times, we can use the range() function,
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by
default), and ends at a specified number. The range() function defaults to 0 as a starting value, however it is possible to
specify the starting value by adding a parameter: range(2, 6), which means
Note that range(6) is not the values of 0 to 6, but the values 0 to 5.
values from 2 to 6 (but not including 6):
Example
Using the range() function: Example
Using the start parameter:

for x in range(2, 6):


print(x)
ELSE IN FOR LOOP

The else keyword in a for loop specifies a block of code to be executed when the
loop is finished:
• Example: Print all numbers from 0 to 5, and print a message when the loop has
ended:
Note: The else block will NOT be executed if the loop is stopped by a break
statement.

Example -A nested loop is a loop inside a loop.


-The “inner loop” will be executed one time for each iteration of the “outer
Break the loop when x is 3, and see what loop”:
happens with the else block: Example
Print each adjective for every fruit:
THE PASS STATEMENT

•for loops cannot be empty, but if you for some reason have a for loop with no
content, put in the pass statement to avoid getting an error.

You might also like