Harold Nelson
2/4/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.
## [52, 30, 49, 40, 36, 30, 1, 18, 35, 44, 27, 64, 79, 29, 17, 34, 45, 26, 84, 25]
Sort and print the list you created in the last problem.
## [1, 17, 18, 25, 26, 27, 29, 30, 30, 34, 35, 36, 40, 44, 45, 49, 52, 64, 79, 84]
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
## [1, 17, 18, 25, 26, 27, 29, 30, 30, 34, 35, 36, 40, 44, 45, 49, 52, 64, 79, 84, 150]
Note that bigger was not produced, but the append did modify randlist.
Create a function how_many_a_in_b with two parameters a and b. The parameter a is a single object of any type. The parameter b is a list. The function returns the number of times the object a is found in the list b. Test your function with a few different objects and lists.
# Create the function
def how_many_a_in_b(a,b):
count = 0
for item in b:
if a == item:
count = count + 1
return count
# Create some test data
l = list(range(5))
l = l + l + l
# Do a test
print(how_many_a_in_b(1,l))
## 3
## ['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?
## 4
No. I wrote the function thinking of b as a list, but the only restrictions on argument types are imposed by the usage of the parameter within the body of the function.