Question 1: Using Range
Python has a built in function range, which returns an iterable sequence of numbers.
Task: Write a function sum_range(start, end) which uses range to return the sum of the numbers from start up to, but not including, end. (Including start but excluding end is the default behaviour of range.)
Write a function sum_evens(start, end) which does the same thing, but which only includes even numbers. You should also use range.
sum_range(0, 0) -> 0
sum_range(5, 0) -> 0
sum_range(10, 12) -> 21
sum_evens(0, 0) -> 0
sum_evens(5, 0) -> 0
sum_evens(2, 5) -> 6
sum_evens(1, 5) -> 6
Explanation: For the sum_range function, a variable (total) is set to 0, to keep track of the sum from the for loop. By using a for loop with the range function, we can keep iterating so that it adds the value to the total in the range of start to end. It is important to remember that the range function will iterate everything from the start and to, but not including the end.
For the sum_evens, the approach to solving it is very similar. The only difference is that an if statement is used because the values in the range needs to be divisible by 2. To do this, we used c % 2 == 0. What this does is that the value will be added if the value will return no remainder if it is divided by 2 (% = modulus – Gives the remainder).
Question 2: Finding digits in strings
Finding digits in strings could be useful when there are a lot of strings (e.g. deciphering digits from a bill).
Task: Write a function get_digits(str1) that takes a string as input, and returns the string of digits in str1 in the order they appear in str1.
get_digits returns a string
get_digits(‘1234’) -> ‘1234’
get_digits(’12_34′) -> ‘1234’
get_digits(‘n12+34b’) -> ‘1234’
get_digits(”) -> ‘ ‘
Explanation: Firstly, a variable (c) is created so that the digits from the inputted strings can be kept in a string for us to see the digits only. Next, a for loop is used to iterate over the inputted string (str1), and an if statement is used in conjunction with a built-in function isdigit() , which checks each part of the string for a digit. If these digits are found, it is added to the variable (c). Note: There are many other ways to write this function, but this is most likely the easiest way.
Question 3: Processing Lists Using a For Loop
Lists are iterable and for loops are typically used to process them.
Task: Write a function has_gt that takes a list of numbers and a number and returns True if the first argument contains a number greater than the second argument and False otherwise.
has_gt(, 0) -> True
has_gt(, 1) -> False
has_gt([1, 2], 0) -> True
has_gt([1, 2], 3) -> False
has_gt([1, 2, 3], 2) -> True
has_gt([1, 2, 3], 5) -> False
has_gt(, 0) -> False
Explanation: A for loop is used so that it is iterated over the list of numbers. An if statement is used to check whether each number is larger than the value that is inputted in the function. If all the numbers in the list are larger than the value, then it returns True, else it will return False.
Question 4: Constructing a List from Another List
One typical list processing constructs a list from information in another list. This is typically done by iterating over the input list using a for loop and building up the result in another list.
Task: Write a function definition of all_gt that takes a list of numbers, say nums, and a number, say n and returns the list of numbers from nums that are greater than n. The order of elements should be preserved.
Result is a list
all_gt(, 1) -> 
all_gt([5, 6], 1) -> [5, 6]
all_gt([1, 2, 3], 1) -> [2, 3]
Correctly handles empty list
Doesn’t modify input list
Explanation: A variable second_number is created and it is assigned to an empty list. A for loop is used to iterate over the list of numbers (nums). An if statement is used after to check whether each number in the list is larger than the value n. If it is, that larger number is appended to the second_number list. .append() is used because it is needed to add elements into a list. The second_number list is then returned.
Question 5: Transforming a List
One type of list processing is transforming the elements of a list, which produces a copy of the list but with the elements transformed.
Task: Write a function definition of add_sizes using a for loop that takes a list of strings, say strings, and returns a list of pairs consisting of the elements of strings together with their lengths.
add_sizes([‘hello’]) -> [(‘hello’, 5)]
add_sizes([‘hello’, ‘world’]) -> [(‘hello’, 5), (‘world’, 5)]
add_sizes() -> 
does not modify input list
Explanation: A variable new_list is created which is assigned an empty list. A for loop is used to iterate over the list of strings (strings). The character c and its length len(c) is appended into the new_list. It is important to remember to bracket the two together inside the append function, because that is what is required by the task. The new_list is then returned.
Question 6: Summing Elements of a List
Python has an in-built function sum that adds all the elements of a list (of numbers) together.
Task: Write a function definition of sum_elems using a for loop that takes a list of numbers, and returns the sum of all the elements of the list. Your solution must not use the built-in function sum.
sum_elems() -> 1
sum_elems([1, 2]) -> 3
sum_elems() -> 0
does not modify input list
Explanation: A variable total is created to store the sum. A for loop is used to iterate over the a_list, and the numbers in the list are summed to the total. (How it works for example 2, the for loop goes once, and the total becomes 1, and the for loop goes again and the total becomes 1 + 2, which becomes 3). The total is then returned.
Question 7: Finding the mean
Finding the mean of a set of numbers from a list, a string, etc. is useful.
Task: Write a function mean that takes as input a non-empty list of numbers (of any length > 0) and returns the mean.
mean() -> 1.
mean([0, 2]) -> 1.
mean([0, 1]) -> 0.5
mean([2, 7, 3, 9, 13]) -> 6.8
Explanation: A variable total is created to store the total sum of numbers. Another variable no_of_variables is created and assigned the length of the list of numbers (no. of numbers). A for loop is used over the range of the length of the list of numbers. For each loop, each number in the list xs is added to the total. After the for loop ends, a variable average is used to get the mean (total/no_of_numbers). The average (mean) is then returned.