Exercises Python Lists

practical exercises python lists
k

12/07/2024

Practical Exercises Python Lists

In this post we are going to learn Python with practical exercises of Python lists aiming help you to understand them a little better.

The first exercise of python lists is about defining a function that we will call sum. This function will take a list as a parameter. The objective of the function is to add up all the elements of the list and return the result of the sum.

Example 1

# suppose we have a list
numbers = [ 2,5,8,9,10,12,20 ]

def sum(numbers):
    total_sum = 0

    for i in numbers:
        if str(i).isdigit():
           total_sum += i
           # print(total_sum)
    return total_sum

print(sum(numbers))
66

2
7
15
24
34
46
66

If we explain what we have seen previously, the first thing we do is to create a variable that collects the total sum called total_sum. Then we go through the elements of the list with a for -> for i in numbers. We check if the elements are digits or not to be able to add them. If they are, they are added to the value of total_sum -> sum_total += i. Then we return the total sum value -> return total_sum.

We have added a line of code that will help us to clearly see the sum step by step. It is print(total_sum) within the iteration.

In this way we can see that to the initial value of total_sum 0 we add the element of index 0 which is a 2 and we get 2, then to this we add the other element – 5 – and we get -7- and so on until we get to 66.

Example 2

def squares(numbers):
    square = []
    for i,element in enumerate(numbers):
        square.append(element**2)
        #print(square)
    return square

numbers = [1,2,3,4,5]
result = squares(numbers)
print(result)
#[1, 4, 9, 16, 25]

[1]
[1, 4]
[1, 4, 9]
[1, 4, 9, 16]
[1, 4, 9, 16, 25]

In the next example of Python lists we define a function that calculates the square of an element and adds it to a new list. The end result is a new list of squared elements.

A brief explanation of the above example starts by defining the function squares – > def squares(numbers): to which we pass a list – numbers – as a parameter. As we are going to display our result in a new list we create the variable square = [ ] which is an empty list.

Then we loop through the list numbers with a for i,element in enumerate(numbers): to access each element in the list. Once we access each element of the list numbers, we add it to the square to the new list square with the method append square.append(element**2)

Finally we return the value of the new list with return square.

In this example I have also included a #print(square) which is useful to see the evolution of the list element by element, until it becomes the final result [1, 4, 9, 16, 25].

Example 3

def sum(numbers):   
   total_sum = 0   
   for i in numbers:
     if str(i).isdigit():
        total_sum += i
   return total_sum

def odds_evens(numbers):
   odds = numbers[1::2]
   odds_sum = sum(odds)
   evens = numbers[::2]
   evens_sum = sum(evens)
   difference = odds_sum - evens_sum
   return difference

numbers = [1,5,13,64,50,26]

dif = odds_evens(numbers)

print(dif)

31

The last of the exercises python lists we are going to define a function that returns the difference between the elements that occupy the odd indexes, such as 1,3,5… and the elements that occupy the even indexes.

Explaining the previous code, we can use the function def sum(numbers) to calculate the total sum of the list that we pass as parameter, in this case, numbers.

Then focusing on the function def odds_evens(numbers): we can say that the first thing we will do is to set the odd index elements of the list numbers with odds = numbers[1::2]. In square brackets [ ] we put the 1, which is the index from where the first odd element starts to the end with a skip of 2, i.e. index element 1, index 3, etc.

Then we calculate the total_sum of the list stored in odds with the function sum().odds_sum = sum(odds).

We do the same for the even elements of numbers with evens = numbers[::2] , in this case the first element starts at index 0, until the end with a skip of 2.

We calculate the total sum of this list – pairs. sum_pairs = sum(pairs)

We calculate the difference of sum_pairs and sum_pairs and that difference is the value that the function returns difference. In this particular case the result is 31.

To learn Python with practical exampls is fundamental to be able to program in Python. If you want to continue learning how to program in Python, you can find more resources in this link.

Creatuwebpymes, is a web design and web positioning company in the Canary Islands that ❤️ programming.

creatuwebpymes quien somos

Francisco Brito Diaz

CEO of creatuwebpymes.com, web design and digital marketing company in the Canary Islands