Class 10 – Wednesday, September 18
Being loopy / That's so random
You did not expect – The repetition to come – You did not expect
Look both ways
Agenda
- Doing lots of loop the loops
Warning
- Anyone who asks me today about a function
sum()
earns negative ducks.
Hoos got your back — food insecurity
- Nationwide more than 10% of college students are not able to afford nutrious food. This problem occurs at UVA also. I am aware of two sources at UVA to help out.
- The Engineering School has a food pantry in Thornton A121. It operates on the honor system and is meant to help students with need.
- The unversity assists with the Community Food Pantry in the Runk Green Room. See the website for details.
- If you find yourself experiencing some financial hardships, please consider talking with anyone in the Undergraduate Programs Office. They can help you find even more resources.
- If you can pay it forward, please help by donating items to either pantry.
Downloads
- Program the_great_reverso.py
- Program list_creation.py
- Program list_support.py
- Program list_building.py
- Program numbing.py
- Program summing.py
- Program so_random.py
- Program and_the_winner_is.py
- Program bit_by_bit.py
- Program octane.py
- Program blast_off.py
- For the interested student — shows a counting down range.
- Recommend you try to understand it.
To do list
- Review class artifacts.
- Review epistles
- Complete the big_production.py as homework by Friday 4:30 PM.
Proof dogs are not as smart as they think they are
Answer accumulation
- Program the_great_reverso.py
- Produce and print the reverse of a user-supplied word (through accummulation)
Some possible program runs
Enter word: hannah
hannah
Enter word: reverse
esrever
Enter word: abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba
List basics
- Program list_creation.py
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
print( "values =", values )
print( "stuff =", stuff )
print( "digits =", digits )
print( "words =", words )
Program run
values = []
stuff = ['abc', 1112, 2.71]
digits = [3, 1, 4, 1, 5, 9, 2, 6]
words = ['we', 'are', 'in', 'it', 'together']
- Program list_support.py
s = "we are in it together"
values = [ ]
stuff = [ 'abc', 1112, 2.71, ]
digits = [ 3, 1, 4, 1, 5, 9, 2, 6 ]
words = s.split()
vlen = len( values )
slen = len( stuff )
dlen = len( digits )
wlen = len( words )
print( "size of", values, "=", vlen )
print( "size of", stuff, "=", slen )
print( "size of", digits, "=", dlen )
print( "size of", words, "=", wlen )
dmax = max( digits )
dmin = min( digits )
wmax = max( words )
wmin = min( words )
print( "max of", digits, "=", dmax )
print( "min of", digits, "=", dmin )
print( "max of", words, "=", wmax )
print( "min of", words, "=", wmin )
Program run
size of [] = 0
size of ['abc', 1112, 2.71] = 3
size of [3, 1, 4, 1, 5, 9, 2, 6] = 8
size of ['we', 'are', 'in', 'it', 'together'] = 5
max of [3, 1, 4, 1, 5, 9, 2, 6] = 9
min of [3, 1, 4, 1, 5, 9, 2, 6] = 1
max of ['we', 'are', 'in', 'it', 'together'] = we
min of ['we', 'are', 'in', 'it', 'together'] = are
- Program list_building.py
values = [ ]
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
values.append( " " )
values.append( "u" )
values.append( "v" )
values.append( "a" )
values.append( "!" )
print( "values =", values )
ints = []
for i in range( 0, 10 ) :
ints.append( i )
print( "ints =", ints
Program run
values = ['u', 'v', 'a', '!', ' ', 'u', 'v', 'a', '!']
ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Processing a numeric list
- Program numbing.py
- Demonstrates accumulation involving a list of numbers
- Prompts and gets as input a list of integer numbers. Converts the gotten input into a list of numeric strings. Then by accumulation, converts the numeric strings one-by-one to get a list of integers (a loop is needed), where each time through the loop the current accumulation is printed.
reply = input( 'Enter a list of numbers: ' )
slist = reply.split()
print( 'reply =', reply )
print( 'slist =', slist )
# first set up a holder of elements
nlist = [] # initialize holder to prepare for adding elements
# now get conversions of the elements of slist one-by-one
for s in slist :
# get numeric equivalent of s
nbr = int( s )
# add the equivalent to the numeric list
nlist.append( nbr )
# print the updated list
print( nlist )
Some possible program runs
Enter a list of numbers: 3 1 4 1 5 9
reply = 3 1 4 1 5 9
slist = ['3', '1', '4', '1', '5', '9']
nlist = [3]
nlist = [3, 1]
nlist = [3, 1, 4]
nlist = [3, 1, 4, 1]
nlist = [3, 1, 4, 1, 5]
nlist = [3, 1, 4, 1, 5, 9]
Enter a list of numbers: 58 2 37 16 99 1 9 23
reply = 58 2 37 16 99 1 9 23
slist = ['58', '2', '37', '16', '99', '1', '9', '23']
nlist = [58]
nlist = [58, 2]
nlist = [58, 2, 37]
nlist = [58, 2, 37, 16]
nlist = [58, 2, 37, 16, 99]
nlist = [58, 2, 37, 16, 99, 1]
nlist = [58, 2, 37, 16, 99, 1, 9]
nlist = [58, 2, 37, 16, 99, 1, 9, 23]
- Program summing.py
- Demonstrates accumulation involving a list of numbers
- Prompts, gets, and converts a list of integer inputs into a list of integers.
- Then by accumulation, sums the list of integers
reply = input( 'Enter a list of numbers: ' )
slist = reply.split()
nlist = []
for s in slist :
nbr = int( s )
nlist.append( nbr )
# by accumulation get the sum of the numbers
total = 0
for nbr in nlist :
total = total + nbr
# print summation
print( "sum(", nlist, "):", total )
Some possible program runs
Enter a list of numbers: 3 1 4 1 5 9
sum( [3, 1, 4, 1, 5, 9] ): 23
Enter a list of numbers: 58 2 37 16 99 1 9 23
sum( [58, 2, 37, 16, 99, 1, 9, 23] ): 245
Introducing module random
- Program so_random.py
- Demonstrates some simple usage of
random
module functionrandrange()
Some possible program runs
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 7 4 3 5
Four random values from interval [ 0, 100 ): 15 28 64 87
Enter four integers: 10 20 75 100
Four random values from interval [ 0, 10 ): 4 4 8 8
Four random values from interval [ 0, 100 ): 22 64 2 80
Enter two integers: 10 100
Four random values from interval [ 0, 10 ): 1 2 8 6
Four random values from interval [ 0, 100 ): 57 63 81 18
Enter four integers: 10 20 75 100
Four random values from interval [ 10 , 20 ): 11 11 19 15
Four random values from interval [ 75 , 100 ): 89 85 76 99
Enter two integers: 2 10
Four random values from interval [ 0, 2 ): 0 1 0 1
Four random values from interval [ 0, 10 ): 6 2 7 4
Enter four integers: 0 8 31 39
Four random values from interval [ 0 , 8 ): 5 4 6 2
Four random values from interval [ 31 , 39 ): 33 33 38 34
Enter two integers: 2 10
Four random values from interval [ 0, 2 ): 1 0 0 1
Four random values from interval [ 0, 10 ): 6 6 2 8
Enter four integers: 0 8 31 39
Four random values from interval [ 0 , 8 ): 7 2 1 3
Four random values from interval [ 31 , 39 ): 37 36 32 37
- Program and_the_winner_is.py
- Demonstrates some simple usage of
random
module functionrandrange()
Some possible program runs
Enter a string: abcdefghij
Four random characters from string abcdefghij : e a j d
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : ov lo la ov
Enter a string: abcdefghij
Four random characters from string abcdefghij : a i b h
Enter some words: th fo ju ov th la lo
Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : th fo th ju
Enter a string: simultaneously
Four random characters from string simultaneously : i y m t
Enter some words: 31 41 597 2 71
Four random words from list ['31', '41', '597', '2', '71'] : 71 597 31 71
Generating random lists of values
- Program bit_by_bit.py
- Generates a list of
n
bits (binary digits) for a user-suppliedn
.
Some possible program runs
How many bits: 8
[1, 0, 0, 0, 0, 0, 1, 1]
How many bits: 8
[1, 0, 0, 1, 1, 0, 0, 1]
How many bits: 28
[0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1]
How many bits: 28
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1]
- Program octane.py
- Prompts and gets three user-supplied integers
s
,n
, andd
. Uses integers
as a seed to the Python random number generator.
- Accumulates a list of
n
octal digits (base 8 digits; that is digits from the interval 0 to 7) one by one. Afterwards, prints the list of octal digits.
- Determines and prints the number of occurrences of
d
in the list of octals (check out the list module for a helpful function.
Some possible program runs
Enter three numbers: 1112 12 1
octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]
number of times value 1 in list: 4
Enter three numbers: 1112 12 5
octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]
number of times value 5 in list: 2
Enter three numbers: 8 9 6
octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]
number of times value 6 in list: 1
Enter three numbers: 8 9 6
octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]
number of times value 3 in list: 3
Decrementing range (for the interested student)
- Program blast_off.py
- Prints a user-requested count down and then prints the phrase
'Blast off!'
Some possible program runs
Enter starting and ending numbers for the countdown: 10 1
10
9
8
7
6
5
4
3
2
1
Blast off!
Enter starting and ending numbers for the countdown: 15 5
15
14
13
12
11
10
9
8
7
6
5
Blast off!
© 2019 Jim Cohoon | Resources from previous semesters are available. |