Loops
A loop is a programming
construct that enables repetitive processing of a sequence of statements.
Python provides two types of loops to its users: the ‘for loop’ and the ‘while
loop’. The ‘for’ and ‘while’ loops are iteration statements that allow a block
of code (the body of the loop) to be repeated a number of times.
The For Loop
Python implements an
iterator-based ‘for loop’. It is a type of ‘for loop’ that iterates over a list
of items through an explicit or implicit iterator.
The loop is introduced by the
keyword ‘for’ which is followed by a random variable name which will contain
the values supplied by the object.
This is the syntax of Python’s ‘for loop’:
for variable in list:
statements
else:
statements
Here is an example of a ‘for loop’ in Python:
pizza = [“New York Style
Pizza”, “Pan Pizza”, “Thin n Crispy Pizza”, “Stuffed Crust Pizza”]
for choice in pizza:
if choice == “Pan
Pizza”:
print(“Please pay
$16. Thank you!”)
print(“Delicious,
cheesy ” + choice)
print(“Cheesy pan
pizza is my all-time favorite!”)
print(“Finally, I’m full!”)
Run this and you’ll get the following output on
Python Shell:
Delicious, cheesy New York Style Pizza
Please pay $16. Thank you!
Delicious, cheesy Pan Pizza
Delicious, cheesy Thin n Crispy Pizza
Delicious, cheesy Stuffed Crust Pizza
Cheesy pan pizza is my all-time favorite!
Finally, I’m full!
Using a break statement
A Python break statement ends
the present loop and instructs the interpreter to starts executing the next
statement after the loop. It can be used in both ‘for’ and ‘while’ loops.
Besides leading the program to the statement after the loop, a break statement
also prevents the execution of the ‘else’ statement.
To illustrate, a break
statement may be placed right after the print function of the ‘if statement’:
pizza = [“New York Style
Pizza”, “Pan Pizza”, “Thin n Crispy Pizza”, “Stuffed Crust Pizza”]
for choice in pizza:
if choice == “Pan
Pizza”:
print(“Please pay
$16. Thank you!”)
break
print(“Delicious,
cheezy ” + choice)
else:
print(“Finally, I’m full!”)
The Python Shell will now show:
Delicious, cheezy New York Style Pizza
Please pay $16. Thank you!
Finally, I’m full!
Using Continue Statement
The
continue statement brings back program control to the start of the loop. You
can use it for both ‘for’ and ‘while’ loops.
To
illustrate, the continue statement may be placed right after the print function
of the ‘for loop’ to replace the break statement:
pizza
= [“New York Style Pizza”, “Pan Pizza”, “Thin n Crispy Pizza”, “Stuffed Crust
Pizza”]
for choice in pizza:
if choice == “Pan
Pizza”:
print(“Please pay
$16. Thank you!”)
continue
print(“Delicious,
cheesy ” + choice)
else:
print(“Cheesy pan
pizza is my all-time favorite!”)
print(“Finally, I’m full!”)
The output will be:
Please pay $16. Thank you!
Delicious, cheesy Thin n Crispy Pizza
Delicious, cheesy Stuffed Crust Pizza
Cheesy pan pizza is my all-time favorite!
Finally, I’m full!
Using the range() Function with the for Loop
The range() function can be
combined with the ‘for loop’ to supply the numbers required by the loop. In the
following example, the range(1, x+1) provided the numbers 1 to 50 needed by the
‘for loop’ to add the sum of 1 until 50:
x = 50
total = 0
for number in range(1, x+1):
total = total +
number
print(“Sum of 1 until %d: %d” % (x, total))
The Python Shell will display:
l.py
Sum of 1 until 50: 1275
The While Loop
A Python ‘while loop’ repeatedly carries out a
target statement while the condition is true.
The loop iterates as long as the defined
condition is true. When it ceases to be true and becomes false, control passes
to the first line after the loop.
The ‘while loop’ has the following syntax:
while condition
statement
statement
Here is a simple ‘while loop’:
counter = 0
while (counter < 10):
print(‘The count
is:’ , counter)
counter = counter
+ 1
print(“Done!”)
If you run the code, you should see this output:
l.py
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
The count is: 9
Done!
Using Pass Statement
The pass statement tells the
Python interpreter to ‘do nothing’. The interpreter simply continues with the
program’s execution whenever the pass statement is encountered. This attribute
makes it a good placeholder whenever Python syntactically requires a line but
the program itself does not require action. It can be very useful when you’re creating
a program and you need to focus on specific areas of your code, but you still
want to reserve some loops or test run the incomplete code.
Here is how you would use a pass statement to
fill gaps within a code:
def function_name(x):
pass
Comments
Post a Comment
If you have any query, please let us know