Harold Nelson
9/24/2020
Google for help on the randint() function and use it to get and print a single random integer between 0 and 100.
Use randint() to create a list of 20 random integers between 0 and 100.
## [8, 8, 83, 95, 69, 52, 87, 76, 27, 0, 98, 10, 59, 32, 95, 70, 78, 81, 40, 47]
Sort and print the list you created in the last problem.
## [0, 8, 8, 10, 27, 32, 40, 47, 52, 59, 69, 70, 76, 78, 81, 83, 87, 95, 95, 98]
What happens if you use the sort() method on the right side of an assignment statement?
## None
Note that the sort method is void. It doesn’t return anything. What about append?
## None
## [0, 8, 8, 10, 27, 32, 40, 47, 52, 59, 69, 70, 76, 78, 81, 83, 87, 95, 95, 98, 150]
Note that bigger was not produced, but the append did modify randlist.
Create a function how_many_a_in_li with two parameters a and li. The parameter a is a single object of any type. The parameter li is a list. The function returns the number of times the object a is found in the list li. Test your function by creating a list with three copies of range(5). Verify that the number 1 occurs three times in this list.
# Create the function
def how_many_a_in_li(a,li):
count = 0
for item in li:
if a == item:
count = count + 1
return count
# Create the test data
li = list(range(5))
l3 = li + li + li
# Do the test
print(how_many_a_in_li(1,l3))
## 3
Test the function again using the line “Mary had a little lamb”. Count the number of times the letter ‘a’ appeared in this line.
## ['M', 'a', 'r', 'y', ' ', 'h', 'a', 'd', ' ', 'a', ' ', 'l', 'i', 't', 't', 'l', 'e', ' ', 'l', 'a', 'm', 'b']
## 4
Was it necessary to convert the string to a list in the previous exercise?