Skip to main content

Exercises 9.25 Exercises

1.

Create a list called myList with the following six items: 76, 92.3, “hello”, True, 4, 76. Begin with the empty list shown below, and add 6 statements to add each item, one per item. The first three statements should use the append method to append the item to the list, and the last three statements should use concatenation.
Solution.
my_list = []
my_list.append(76)
my_list.append(92.3)
my_list.append("hello")
my_list = my_list + [True]
my_list = my_list + [4]
my_list = my_list + [76]
print(my_list)

2.

Starting with the list of the previous exercise, write Python statements to do the following:
  1. Append “apple” and 76 to the list.
  2. Insert the value “cat” at position 3.
  3. Insert the value 99 at the start of the list.
  4. Find the index of “hello”.
  5. Count the number of 76s in the list.
  6. Remove the first occurrence of 76 from the list.
  7. Remove True from the list using pop with an index argument.
Solution.
my_list = [76, 92.3, 'hello', True, 4, 76]

my_list.append("apple")         # a
my_list.append(76)              # a
my_list.insert(3, "cat")        # b
my_list.insert(0, 99)           # c

print(my_list.index("hello"))   # d
print(my_list.count(76))        # e
my_list.remove(76)              # f
my_list.pop(my_list.index(True)) # g

print (my_list)

3.

Write a function called average that takes a list of numbers as a parameter and returns the average of the numbers.
Solution.
def average(num_list):

    total = 0
    for num in num_list:
        total = total + num

    return total / len(num_list)

4.

Write a Python function named max that takes a parameter containing a nonempty list of integers and returns the maximum value. (Note: there is a built-in function named max, but do not use it.)
Solution.
def max(number_list):
    max_value = number_list[0]
    for element in number_list:
        if element > max_value:
            max_value = element
    return max_value

5.

Write a function sum_of_squares(num_list) that computes the sum of the squares of the numbers in the list num_list. For example, sum_of_squares([2, 3, 4]) should return 4 + 9 + 16, which is 29:

6.

Write a function to count how many odd numbers are in a list.
Solution.
import random

def count_odd(num_list):
    odd = 0
    for element in num_list:
        if element % 2 != 0:
            odd = odd + 1
    return odd

# make a random list to test the function
numbers = []
for i in range(10):
    numbers.append(random.randint(0, 1000))

print(numbers) # show the random numbers
print(count_odd(numbers))

7.

Sum up all the even numbers in a list.

8.

Calculate the product of all the negative numbers in a list. For example, product_negatives([-3, 2, -4, 7]) should be 12. If a list contains no negative numbers, return 1.
Solution.
import random

def product_negatives(number_list):
    product = 1
    for element in number_list:
        if element < 0:
            product = product * element
    return product

numbers = []
for i in range(10):
    numbers.append(random.randrange(-10, 10))

print(numbers) # show numbers
print(product_negatives(numbers))

9.

Count how many words in a list have length 5.

10.

Although Python provides us with many list methods, it is good practice and very instructive to think about how they are implemented. Implement Python functions that work like the following:
  1. count
  2. in (you will have to name this function is_in because in is a reserved word)
  3. reverse
  4. index
  5. insert
Solution.
def count(obj, a_list):
    count = 0
    for element in a_list:
        if element == obj:
            count = count + 1
    return count

def is_in(obj, a_list):  # cannot be called in() because in is a reserved keyword
    for element in a_list:
        if element == obj:
            return True
    return False

def reverse(a_list):
    reversed = []
    for i in range(len(a_list) - 1, -1, -1): # step through the original list backwards
        reversed.append(a_list[i])
    return reversed

def index(obj, a_list):
    for i in range(len(a_list)):
        if a_list[i] == obj:
            return i
    return -1

def insert(obj, index, a_list):
    new_list = []
    for i in range(len(a_list)):
        if i == index:
            new_list.append(obj)
        new_list.append(a_list[i])
    return new_list

a_list = [0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9]
print(count(1, a_list))
print(is_in(4, a_list))
print(reverse(a_list))
print(index(2, a_list))
print(insert('cat', 4, a_list))