Download presentation
Presentation is loading. Please wait.
Published byΟλυμπία Φιλιππίδης Modified over 6 years ago
1
And now for something completely different . . .
10-Nov-18 AHD c 2010
2
Part 5 Python 3 - Repetition and String Formatting
10-Nov-18 AHD c 2010
3
Python 3 Repetition and String Formatting
The while loop The for loop String formatting 10-Nov-18 AHD c 2010
4
Python Repetition and String Formatting
The while loop The for loop String formatting 10-Nov-18 AHD c 2010
5
The while statement Like the if statement, the while statement makes use of a Boolean expression... x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010
6
The while statement The Boolean expression (condition) in this example is: x < 5 The expression has a value of true or false (1 or 0). x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010
7
The while statement If the condition is true, the indented statements are executed, otherwise, the statements are skipped. x = 1 while x < 5: print ('Hi spam') x = x + 1 print ('done') 10-Nov-18 AHD c 2010
8
The while statement 05-01.py 05-02.py x = 1 while x < 5:
print ('Hi spam') x = x - 1 print ('done') 05-01.py 05-02.py These programs are examples of counter-controlled while loops. 10-Nov-18 AHD c 2010
9
The infinite while loop
print ('Hi spam') print ('done') 05-03.py 05-04.py 10-Nov-18 AHD c 2010
10
The break statement 05-05.py while 1: print ('Spam')
answer = input('Press y to end this loop') if answer == 'y': print ('Fries with that?') break print ('Have a ') print ('nice day!') 10-Nov-18 AHD c 2010
11
The continue statement
05-06.py while 1: print ('Spam') answer = input('Press y for large fries ') if answer == 'y': print ('Large fries with spam, mmmm, yummy ') continue answer = input('Had enough yet? ') break print ('Have a ') print ('nice day!') 10-Nov-18 AHD c 2010
12
A Sentinel-Controlled while loop
05-07.py 10-Nov-18 AHD c 2010
13
Python Repetition and String Formatting
The while loop The for loop String formatting 10-Nov-18 AHD c 2010
14
Counter-controlled repetition with the for loop
for c in range (10): print (c) 05-08.py This program prints the numbers 0 through 9. 10-Nov-18 AHD c 2010
15
Counter-controlled repetition with the for loop
for c in range (5,10): print (c) 05-09.py This program prints the numbers 5 through 9. 10-Nov-18 AHD c 2010
16
Using continue with the for loop
for c in range (1,6): if c == 3: continue print (c) 05-10.py This program prints the numbers 1, 2, 4, 5, (skips 3). 10-Nov-18 AHD c 2010
17
Using break with the for loop
for c in range (1,6): if c == 3: break print (c) 05-11.py This program prints the numbers 1 and 2. 10-Nov-18 AHD c 2010
18
Python Repetition and String Formatting
The while loop The for loop String formatting 10-Nov-18 AHD c 2010
19
Printing strings and numbers
We can print string values and number values from the same print statement by separating them with commas: d = 10 c = 75 print ('Total is: ', d, 'dollars and', c, ' cents ') >>> Total is: 10 dollars and 75 cents 05-12.py 10-Nov-18 AHD c 2010
20
Numbers can be printed from within a single string by using a special method known as string formatting. 10-Nov-18 AHD c 2010
21
String Formatting and %
In string formatting, we use the % symbol. The % operator can also be used for a different purpose as the modulus operator (finding the remainder after an integer division). The % symbol is said to be overloaded. 10-Nov-18 AHD c 2010
22
String Formatting and %
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). 10-Nov-18 AHD c 2010
23
Printing integers within a string
x = 20 y = 75 print ('The sum of %d and %d is %d' % (x, y, x + y)) >>> The sum of 20 and 75 is 95 The three %d formatting codes represent where the decimal integers shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010
24
Printing floats within a string
x = y = print ('The sum of %f and %f is %f' % (x, y, x + y)) >>> The sum of and is The three %f represent where the float values shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010
25
Specifying the number of decimal places
x = y = print ('The sum of %0.2f and %0.2f is %0.2f' % (x, y, x + y)) >>> The sum of and is 35.78 The three %0.2f represent where the float values (to 2 decimal places) shown in brackets after the % symbol should be printed. 05-13.py 10-Nov-18 AHD c 2010
26
String formatting codes
%d and %f are only two of a set of formatting codes available for string formatting. See: for more details. 10-Nov-18 AHD c 2010
27
Repeating a progam at the user’s request
print ("This is the start of the program") answer = 'y' while (answer == 'y' or answer == 'Y'): print ("This is a statement from within the while loop") print ("This is another statement from within the while loop") answer = input("Do you want to run this program again? y/n") print ("Goodbye! ") 05-14.py 10-Nov-18 AHD c 2010
28
Loops within loops - nested loops
for i in range (1,6): for j in range (1,6): print ("i: " + str(i) + " j: " + str(j) ) print() ''' Notice that with a loop repeating 5 times, ***within*** a loop that repeats 5 times means that you can control 25 processes. py 10-Nov-18 AHD c 2010
29
This presentation uses the following program files:
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 10-Nov-18 AHD c 2010
30
End of Python_Repetition_StringFormatting.ppt
10-Nov-18 AHD c 2010
31
Last updated: Sunday 24th May 2009, 15:58 PT, AHD
10-Nov-18 AHD c 2010
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.