Topic 2: Lists
Learning Outcomes:
(f) Identify pre-defined list functions (len( ), min( ), max( ), � sum( ) and a method (append( ))
CP125
Table of Contents
len() function
min() function
max() function
sum() function
In this lecture, we’ll mainly focus on these four (4) pre defined functions & one (1) method in list:
append() method
CP125
4. The sum() function
What does it do?
sum(iterable,start)
The list of numbers to sum up.
(Optional) The value that is added to the sum of elements. The default is 0.
CP125
Example 1: Sum of numbers in a list |
numbers = [1, 2, 3, 4, 5] total_sum = sum(numbers) print("Sum of all elements in the list:", total_sum) |
Output |
Sum of all elements in the list: 15 |
CP125
Example 2: Sum with a starting value. |
numbers = [1, 2, 3, 4, 5] total_sum = sum(numbers,100) print("Sum of all elements in the list:", total_sum) |
Output |
Sum of all elements in the list: 115 |
CP125
Example 3: sum() function on string list. |
# List of strings string_list = ["apple", "kiwi"] # Find the minimum string sum_string = sum(string_list) print(sum_string) |
Output |
ERROR! Traceback (most recent call last): File "<main.py>", line 5, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str' === Code Exited With Errors === |
CP125
Difference between a function and a method
METHOD
FUNCTION
VS
how to call/invoke
NOTE:
Method is similar to function but attached to an object. Later, you will use this in lists etc.
CP125
5. The append( ) method in Python
What does it do?
list.append(element)
The list you want to modify.
The item you want to add to the list.
CP125
Example 1: Using append() with a List. |
fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) |
Output |
['apple', 'banana', 'cherry', 'orange'] |
CP125
Example 2: Using append() with a Condition |
numbers_list = [1, 2, 3, 4, 5] new_number = 6 if new_number not in numbers_list: numbers_list.append(new_number) print(numbers_list) |
Output |
[1, 2, 3, 4, 5, 6] |
CP125
not in means that the number is not in the list
Example 3: Using append() in a Function. |
def add_item(my_list, item): if item: # Ensures item is not empty or None my_list.append(item) items = ["pen", "pencil"] add_item(items, "eraser") print(items) |
Output |
['pen', 'pencil', 'eraser'] |
CP125
Example 4: Appending Numbers from a Range |
number_list = [] # Empty list for i in range(5): number_list.append(i) # Add each number to the list print(number_list) |
Output |
[0, 1, 2, 3, 4] |
CP125
Example 5: Appending even numbers only |
even_numbers = [] for i in range(10): if i % 2 == 0: # Check if number is even even_numbers.append(i) print(even_numbers) |
Output |
[0, 2, 4, 6, 8] |
CP125
Example 6: Apply what you’ve learned so far. Count the number of positive number and find their sum |
def sum_count_positiveNumbers(numbers_list): positive_numbers = [] for num in numbers_list: if num > 0: positive_numbers.append(num) sum_of_positives = sum(positive_numbers) count_of_positives = len(positive_numbers) print("Sum of positive numbers:", sum_of_positives) print("Count of positive numbers:", count_of_positives)
# Example usage: sample_list = [-1, 2, 3, -4, 5] result = sum_count_positiveNumbers(sample_list ) |
Output |
Sum of positive numbers: 10 Count of positive numbers: 3 |
CP125
Write a Python program that allows the user to enter a series of numbers. The program should:
CP125