# & for while while # * # & *, /01* for * 2 for <var> in <sequence>: <body>,var 0 2, /01* *** 0 *0 *& 4 00 *0* *
/01* 4 6, 4 * /01* Input the count of the numbers, n Initialize sum to 0 Loop n times Input a number, x Add x to sum Output average as sum/n 5 /01* # average1py # A program to average a set of numbers # Illustrates counted loop with accumulator def main: n = inputhow many numbers do you have? sum = 00 for i in rangen: x = inputenter a number >> sum = sum x print \nthe average of the numbers is, sum / n 8&99sum/n 6 /01* How many numbers do you have? 5 Enter a number >> 2 Enter a number >> 45 Enter a number >> 4 Enter a number >> 76 Enter a number >> 45 The average of the numbers is 464 7 9 # 0** * 4** 0 * for * 4 * 0* 4 0** * 46 0
while <condition>: <body> condition,#0 if 2, 4 0* *, :,while 99 i = 0 while i <= 10: print i i = i 1 for for i in range11: print i while 2 i & 9 for while * ; i = 0 while i <= 10: print i 4*< 4 i 29*9, 9 8* i 9, 5
4 < *0 *0 6 * * 1**** 00* < count 7 9 0 4 set moredata to yes while moredata is yes get the next data item process the item ask user if there is moredata * initialize sum to 00 initialize count to 0 set moredata to yes while moredata is yes input a number, x add x to sum add 1 to count ask user if there is moredata output sum/count # average2py # A program to average a set of numbers # Illustrates interactive loop with two accumulators def main: moredata = yes sum = 00 count = 0 while moredata[0] == y: x = inputenter a number >> sum = sum x count = count 1 moredata = raw_inputdo you have more numbers yes or no? print \nthe average of the numbers is, sum / count =, >?9@A* BCBCBC Enter a number >> 2 Do you have more numbers yes or no? y Enter a number >> 45 Do you have more numbers yes or no? yes Enter a number >> 4 Do you have more numbers yes or no? yup Enter a number >> 76 Do you have more numbers yes or no? y Enter a number >> 45 Do you have more numbers yes or no? nah The average of the numbers is 464
get the first data item while item is not the sentinel process the item get the next data item *,,* 4* *9 * # averagepy # A program to average a set of numbers # Illustrates sentinel loop using negative input as sentinel def main: sum = 00 count = 0 x = inputenter a number negative to quit >> while x >= 0: sum = sum x count = count 1 x = inputenter a number negative to quit >> print \nthe average of the numbers is, sum / count 5 Enter a number negative to quit >> 2 Enter a number negative to quit >> 45 Enter a number negative to quit >> 4 Enter a number negative to quit >> 76 Enter a number negative to quit >> 45 Enter a number negative to quit >> 1 The average of the numbers is 464 * * * 7 9
4 D* = 4 > A6 initialize sum to 00 initialize count to 0 input data item as a string, xstr while xstr is not empty convert xstr to a number, x add x to sum add 1 to count input next data item as a string, xstr Output sum / count # average4py # A program to average a set of numbers # Illustrates sentinel loop using empty string as sentinel def main: sum = 00 count = 0 xstr = raw_inputenter a number <Enter> to quit >> while xstr = : x = evalxstr sum = sum x count = count 1 xstr = raw_inputenter a number <Enter> to quit >> print \nthe average of the numbers is, sum / count Enter a number <Enter> to quit >> 4 Enter a number <Enter> to quit >> 2 Enter a number <Enter> to quit >> 0 Enter a number <Enter> to quit >> 25 Enter a number <Enter> to quit >> 44 Enter a number <Enter> to quit >> 227 Enter a number <Enter> to quit >> The average of the numbers is 8 40 9< # average5py # Computes the average of numbers listed in a file def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 for line in infilereadlines: sum = sum evalline count = count 1 print \nthe average of the numbers is, sum / count
E 016 4readline, readline BC line = infilereadline while line = #process line line = infilereadline *G0 < H * * readline *BIC 6JBC 5 # average6py # Computes the average of numbers listed in a file def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 line = infilereadline while line = : sum = sum evalline count = count 1 line = infilereadline print \nthe average of the numbers is, sum / count 8 **** if 4 * * >A 7 9 8 ** sum = 00 count = 0 line = infilereadline while line = : #update sum and count for values in line line = infilereadline print \nthe average of the numbers is, sum/count 8,* sum count * * * sum 4count 5
8 for xstr in stringsplitline,,: sum = sum evalxstr count = count 1 8for line* 8 # average7py # Computes the average of numbers listed in a file # Works with multiple numbers on a line import string def main: filename = raw_inputwhat file are the numbers in? infile = openfilename,r sum = 00 count = 0 line = infilereadline while line = : for xstr in stringsplitline,,: sum = sum evalxstr count = count 1 line = infilereadline print \nthe average of the numbers is, sum / count 8 while for 4, 8 ** * * * if while,,true alse *G, * >while x >= 0A,, **, 2 2 5
if p1getx == p2getx: if p1gety == p2gety: # points are the same else: # points are different else: # points are different G*0**,6 G0 andornot and or *, <expr> and <expr> <expr> or <expr> 7 9 and *,, *, 4 / / /,,* P and Q or *,*, / / or *, or *,G** BC 7
not, not, 4 0,,, a or not b and c :*< * notandor 2 a or not b and c G& * *and if p1getx == p2getx and p2gety == p1gety: # points are the same else: # points are different * * 5 * 2 :*,< scorea == 15 or scoreb == 15 4,, 4* H0 6 while notscorea == 15 or scoreb == 15: #continue playing 7 9 9
2 *5 while notscorea == 15 or scoreb == 15 or \ scorea == 7 and scoreb == 0 or scoreb == 7 and scorea == 0: #continue playing 0 * * * * # 9 a >= 15 and a b >= 2 or b >= 15 and b a >= 2 a >= 15 or b >= 15 and absa b >= 2 *, 0, * M9J9 MJ L9J JJ JJ JJ and or 0 1 or* a or true == true and or a or b and c == a or b and a or c a and b or c == a and b or a and c notnot a == a E G* nota or b == not a and not b nota and b == not a or not b 4, while notscorea == 15 or scoreb == 15: #continue playing 0B4 C E G* while not scorea == 15 and not scoreb == 15: #continue playing
while scorea = 15 and scoreb = 15 # continue playing G<B4 C * * * not E G* *2, 5 if while, *** 0 7 59 repeat get a number from the user until number is >= 0 4 *, * * while 5 5
4*, number = 1 while number < 0: number = inputenter a positive number: number ;*, break break, break,* 00 5 5 * break while True: number = inputenter a positive number: if x >= 0: break # Exit loop if number is valid *, True *0 06 4, break,* if :6 4G * **< 5 5 while *0* number = 1 while number < 0: number = inputenter a positive number: if number < 0: print The number you entered was not positive 4G 0* 6 * break else while True: number = inputenter a positive number: if x >= 0: break # Exit loop if number is valid else: print The number you entered was not positive 55 5
: * while True: number = inputenter a positive number: if x >= 0: break # Loop exit print The number you entered was not positive :, ** : * while True: get next data item if the item is the sentinel: break process the item 6 57 9 : : break 26 0 0* **,, * 0 * >0 A * while response[0] == y or response[0] == Y: 6H 0 while response[0] == y or Y: 4G*0< bool 9True alse 0== *bool
:** > A& alse True >>> bool0 alse >>> bool1 True >>> bool2 True >>> boolhello True >>> bool alse >>> bool[1,2,] True >>> bool[] alse 2 alse *2 0True 0, and, or not,,, *,, *, True *alse 5, and, *0*, **G,,,, *,* 7 79
* 0* and *, or*,*, response[0] == y or Y * :G2, response[0] == y or Y or,true?9@2bcbhc* 7 7 ** <Enter> ans ans = raw_inputwhat flavor fo you want [vanilla]: if ans: flavor = ans else: flavor = vanilla #<Enter>ans * * 7 7 46 ans = raw_inputwhat flavor fo you want [vanilla]: flavor = ans or vanilla 1* True,* 6 flavor = raw_inputwhat flavor do you want [vanilla]: or vanilla &N0 0 * 6 7 7