Python Programming unit 5
Python Programming unit 5
PROGRAMMING
Unit 5 Iterative Programming
Contents
• while
• for
• definite and indefinite loops
• nested loops
• abnormal termination of loops using break and continue
• while/else and for/else
while loop
• The “while” loop, repeat as long as the
conditional statements within them are
True.
• A “while” loop, looks similar to a for
loop, but it replaces the counting
portion with a CONDITIONAL
STATEMENT.
while loop
• While loops always start with the
keyword “while”, followed by a
Boolean expression, and then a colon
(:). The repeated code, is indented
below the first line.
while loop example
• In this example, you can make a password checker,
that will continue to loop until the user types in the
correct password. The Boolean expression checks if
the entered password is true. To do this,
• Create the variable “password”, and assign it the
value None. Create the variable “password”, before
you use it in the “while” loop.
• This while loop will continually loop as long as the
password entered is NOT the same as
“myPassword1234”.
• This condition will only run the print( ) function if
the password variable value is not equal to
“myPassword1234”.
• The print( ) function will run after the password
variable value DOES equal “myPassword1234”.
• Here, the program will continue the loop, and
prompt the user to enter the password, until the user
enters the correct password.
• When that happens, the loop ends, and the rest of the
program is allowed to run.
for loop
• The for loop, follows a very specific
syntax format; even the number of
spaces you use on certain lines is
important.
Steps for creating a for loop
• STEP 1 - Enter the keyword “for.” For loops, always start with “for”, to show that the
following code is a for loop.
• STEP 2 - Name the COUNTER VARIABLE. This variable’s value, increases each time the
loop repeats. For example, if you are using a “for loop” that loops 10 times, starting with 0,
and increasing by 1, each time the loop starts over, the counter variable will represent 0, in
the first run. then 1, in the next run, then 2, then 3, and so on.This variable increases by 1,
each time the loop starts over. You can name the counter variable anything you want. Many
programmers name their counter variable “i”, out of tradition.
• STEP 3 - Add the key word “in”, to show that you are about to specify how many times the
loop should repeat.
• STEP 4 - Set the number of times the loop should repeat. Use the range( ) function, to do
this. The range function, will count from zero up to, but not including, the number listed in
the function’s parentheses.
• STEP 5 - End the first line of the for loop, with a colon (:).
for loop- range() function
• Range( ) defines where a counter
starts, ends, and what number it
should count by.
• The parameters include, (start, stop,
and step). The start and stop
parameters, name the start and stop
numbers for the counter. The step
parameter, tells how much the counter
should count by.
for loop
• Below the first line of the for loop,
add the code to be repeated.
• Indent the code to be repeated, by
pressing the TAB key on the keyboard
once, or by using the space bar to add
FOUR SPACES.
• In Python, the indent indicates that the
code on that line, is part of the code
above it.
• You can add a print function to the
loop, by starting a new line and
indenting it in the same way.
for loop
• You can add code, that runs after the
loop has finished running.
• To do that, start a new line that’s not
indented.
• Every time you create a for loop, you
also create a new variable that counts
the number of times the loop is
repeated.
• That means, the variable can be used
within the repeated code.
Infinite loops
• In Python, infinite loops, or forever loops, are
“while” loops, that use a Boolean statement that
can never become false.
• These are all infinite loops because, there is no
way for the Boolean statements in each to become
false.
• None of these loops provide a way for the
conditional statement to ever become false, so
these loops will continue to loop forever, or
(infinitely).
• The print() statement in each example, will
continuously loop, and print over and over again,
forever.
• You can exit an infinite loop, by pressing Ctrl + C
on Windows, or command + C on a Mac.
Infinite Loops
• Sometimes, you write an infinite loop as part of your program on purpose. For
example, video games usually use an infinite loop to animate the characters,
continually updating character movement and interaction as the player plays the
game.
• But other times, an infinite loop may be written by accident, and it may crash your
computer because the program is too large, or tries to get the computer to process
too much information.
Abnormal termination of loops using break
and continue
• The break statement, breaks out of a loop, by causing program execution, to jump to
the statement that follows the loop.
• This causes the loop to end. The continue statement, continues a loop, by causing
execution to jump to the top of the loop. This causes the loop to execute again, by
re-evaluating its given condition.
break
continue
Nested Loops
• Nested loops mean loops inside a loop. For example, while loop inside the for loop,
for loop inside the for loop, etc.
while else
• With the else statement we can run a block of code once when the condition no longer is
true.
for else
• The else keyword in a for loop specifies a block of code to be executed when the
loop is finished
Practice Problems
1. Write a program to compute the factorial of a number using iteration.
2. Write a program to print Fibonacci series upto nth term, where n is entered by the
user.
3. What is the difference between range(10), range(0, 10), and range(0, 10, 1) in a for
loop?
4. Write a program which repeatedly reads numbers until the user enters “done”. Once
“done” is entered, print out the total, count, and average of the numbers. If the user
enters anything other than a number, detect their mistake using try and except and
print an error message and skip to the next number.
5. Write another program that prompts for a list of numbers as above and at the end
prints out both the maximum and minimum of the numbers instead of the average.