Exercises With Solutions On Python lists

Exercise 1 || Solution

Write a python algorithm to display all elements of a given list by two different ways.

Exercise 2 || Solution

Write a Python algorithm to swap the first element with the last element of a given list. Example: if L = ["Python", "Java", "C++", "Javascript"], the algorithm returns the list: ["Javascript", "Java", "C++", "Python"]

Exercise 3 || Solution

Write a python algorithm as a function which takes a list l as parameters and returns a tuple of two lists (l_even, l_odd) where l_even is made up of the elements of l with an even index and l_old is made up of the elements with an odd index . Example: if: L = ["Python", "Java", "C++", "C#", "VB.Net", "Javascript"], the algorithm returns: ['Python', 'C+ +', 'VB.Net'], ['Java', 'C#', 'Javascript']

Exercise 4 || Solution

Write a Python program that asks the user to enter 5 integers numbers of his choice and displays the list of numbers entered.

Exercise 5 || Solution

Given a list of integers L, write a program in Python that returns the sum of the elements of the list L.

Exercise 6 || Solution

Write a Python algorithm that returns the length of a given list without using the len() method.

Exercise 7 || Solution

Write a Python algorithm that returns the list of divisors of a given integer. Example if n = 18 , the algorithm returns the list [1, 2, 3, 6, 9, 18]

Exercise 8 || Solution

For given a list of integers, write an algorithm in python that returns the average of the  list elements.

Exercise 9 || Solution

Write an algorithm in python as a function which takes as a parameter a tuple (L, a) formed by a list L and an element 'a' and which returns True if the element 'a'  is present in the list L and False if nope.

Exercise 10 || Solution

Write an algorithm in python in as a function which takes as a parameter a couple (L, a) formed by a list L and an element 'a' and which returns the position of the element 'a' in the list L. The function must return -1 if the element 'a'  is not in the list L.

Exercise 11 || Solution

Write an algorithm in python as a function which takes as parameters a tuple (L, n) formed by a list L of numbers and an integer n and which returns the list obtained from L by multiplying its elements by n .
Example if L = [13, 11, 7, 25] and n = 2 the function returns the list: [26, 22, 14, 50]

Exercise 12 || Solution

By using the sort() method, write an algorithm in python as a function which takes a list L of numbers as a parameter and which returns the tuple (min , max) formed from the minimum and the maximum of the list.

Younes Derfoufi
CRMEF OUJDA