Last updated: Sunday 31st December 2017, 9:55 PT, AD

 

PLEASE REFRESH THIS PAGE TO GET THE LATEST VERSION







And now for something
completely different . . .

Part 5

 

Python 3 - Repetition and String Formatting

 

5.1\A0\A0\A0 The while loop

5.2\A0\A0\A0 The for loop

5.3\A0\A0\A0 String formatting

5.4\A0\A0\A0 Nested loops - loops within loops

 

 

 

The while statement

 

Like the if\A0 statement, the while\A0 statement makes use of a\A0 Boolean expression...

 

 

x = 1

while x < 5:

\A0\A0\A0 print ('Hi spam')

\A0\A0\A0 x = x + 1

print ('done')

 

 

 

The Boolean expression (condition) in this example is:

\A0 x < 5

The expression has a value of true or false (1 or 0).

 

If the condition is true,

the indented statements are executed,

otherwise, the statements are skipped.

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 

 

 

 

 

 

The break statement

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

The continue statement

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

A Sentinel-controlled while loop

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

#\A0 File:\A0\A0\A0\A0\A0\A0 05-07.py 
#\A0 Purpose:\A0\A0\A0 Example: 'sentinel-controlled' while loop
#\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0 Calculates average score of a class
#\A0 Programmer: Anne Dawson
#\A0 Course:\A0\A0\A0\A0 CSCI120A
#\A0 Date:\A0\A0\A0\A0\A0\A0 Tuesday 5th October 2004, 6:31 PT
 
 
# initialization phase
totalScore = 0\A0\A0\A0\A0 # sum of scores
numberScores = 0\A0\A0 # number of scores entered
 
# processing phase
score = input( "Enter score, (Enter -9 to end): " )\A0\A0 # get one score
score = int( score )\A0\A0 # convert string to an integer
 
while score != -9: # -9 is used as a sentinel ( a lookout or sentry value )
\A0\A0\A0 totalScore = totalScore + score
\A0\A0\A0 numberScores = numberScores + 1
\A0\A0\A0 score = input( "Enter score, (Enter -9 to end): " )\A0 
\A0\A0\A0\A0score = int( score )
\A0\A0 
# termination phase
if numberScores != 0: # division by zero would be a run-time error
\A0\A0 average = float( totalScore ) / numberScores
\A0\A0 print ("Class average is", average)
else:
\A0\A0 print ("No scores were entered")
 
 







 

 

Counter-controlled repetition with the for loop

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

Using continue with the for loop

 

for c in range (1,6):

\A0 if c == 3:

\A0\A0\A0 continue

\A0 print (c)

 

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 

 

Using break with the for loop

 

for c in range (1,6):

\A0 if c == 3:

\A0\A0\A0 break

\A0 print (c)

 

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 








Printing strings and numbers

 

d = 10

c = 75

print ('Total is: ', d, 'dollars and', c, ' cents')

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

Numbers can be printed from within a single string

by using a special method known as string formatting

 

 

 

 

 

 

An overloaded operator behaves differently

depending on the context.

 

In the following example we see the % operator

being used to specify how a string should be printed

(i.e. string formatting).

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 



 

 

 

Example program 05-14.py illustrates

how to repeat a program at the user's request:

 

#\A0 File:\A0\A0\A0\A0\A0\A0 05-14.py

#\A0 Purpose:\A0\A0\A0 Example: how to repeat a program at the user's request

#\A0 Programmer: Anne Dawson

#\A0 Course:\A0\A0\A0\A0 CSCI120A, CSCI165

#\A0 Date:\A0\A0\A0\A0\A0\A0 Thursday 19th October 2006, 7:58 PT

 

 

print ("This is the start of the program")

answer = 'y'

while (answer == 'y' or answer == 'Y'):

\A0\A0\A0 print ("This is a statement from within the while loop")

\A0\A0\A0 print ("This is another statement from within the while loop")

\A0\A0\A0 answer = input("Do you want to run this program again? y/n")

print ("Goodbye!")

 

 

http://www.annedawson.net/python3programs.html

 

 

 

 

Example program 05-15.py - 05-18.py

illustrate how to use loops inside loops:

 

#\A0 File:\A0\A0\A0\A0\A0\A0 05-17.py 
#\A0 Purpose:\A0\A0\A0 Example: how to use a loop within a loop
#\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0\A0 a nested for loop
 
#\A0 Programmer: Anne Dawson
#\A0 Course:\A0\A0\A0\A0 CSCI120A, CSCI165
#\A0 Date:\A0\A0\A0\A0\A0\A0 Wednesday 27th June 2007, 9:45 PT
 
print "This is the start of the program"
 
for i in range (1,6):
\A0\A0\A0 for j in range (1,6):
\A0\A0\A0\A0\A0\A0\A0 print ("i: " + str(i) + " j: " + str(j) )
\A0\A0\A0 print\A0\A0\A0\A0\A0\A0\A0 
'''
Notice that with a loop repeating 5 times,
***within*** a loop that repeats 5 times
means that you can control 25 processes.
'''

 

http://www.annedawson.net/python3programs.html

 

 

The output after the program runs:

 

This is the start of the program

i: 1 j: 1

i: 1 j: 2

i: 1 j: 3

i: 1 j: 4

i: 1 j: 5

 

i: 2 j: 1

i: 2 j: 2

i: 2 j: 3

i: 2 j: 4

i: 2 j: 5

 

i: 3 j: 1

i: 3 j: 2

i: 3 j: 3

i: 3 j: 4

i: 3 j: 5

 

i: 4 j: 1

i: 4 j: 2

i: 4 j: 3

i: 4 j: 4

i: 4 j: 5

 

i: 5 j: 1

i: 5 j: 2

i: 5 j: 3

i: 5 j: 4

i: 5 j: 5

 

 

 

 

 

\A0

This presentation uses the following program files:

 

http://www.annedawson.net/python3programs.html

05-01.py

05-02.py

05-03.py

05-04.py

05-05.py

05-06.py

05-07.py

05-08.py

05-09.py

05-10.py

05-11.py

05-12.py

05-13.py

05-14.py

05-15.py

05-16.py

05-17.py

05-18.py