Python Loop Statement: By: Abhishek Saikia Class: 12 (C) Roll No: 17
Python Loop Statement: By: Abhishek Saikia Class: 12 (C) Roll No: 17
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
•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
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 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.
•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.