Online course

PYTHON

Général instructions

for lists in Python

TRAINING 1

Complet next table :

TRAINING 2

Write a function which takes as argument a list L and a number a and which returns True if the number a is in the list L and False otherwise.

Be careful, don’t just use the instruction “in” which already exists in Python.

TRAINING 3

Write a function which takes as argument a number a and a list L and which returns the number of occurrences of a in L , that is to say the number of times that the number a occurs in L.

TRAINING 4

1) Write a function which takes as argument a list of numbers L and which returns the sum of the elements contained in the list L.

Be careful, don’t just use the instruction “sum” which already exists in Python.

2) Write a function which takes as argument a list of numbers L and which returns the mean of the elements of L.

TRAINING 5

Write a function which takes as argument a list of numbers and which returns the maximum of this list.

TRAINING 6

1) What is the list obtained in each case ?

a. L1=[i for i in range(3,33,5)]

b. L2=[4*i for i in range(20)]

c. L3=[i**2 for i in range(20)]

d. L4=[i**2 for i in range(20) if i%5==2]

2) How can we obtain the following list ?

a. The list of all the multiples of 10 between 0 and 10 000.

b. The list of all the square of the even numbers between 2 and 100.


Training 1 - Solution

Training 2 - Solution

def test(L,a) : or def test(L,a) :

N=len(L) for k in range (len(L)) :

k=0 if L[k]==a :

while k<N : return True

if L[k]==a : return False

return True

k=k+1

return False

Training 3 - Solution

def number(L,a) :

N=0

for k in range(len(L)) :

if L[k]==a :

N=N+1.

return N

Training 4 - Solution

1) def sum_listL) :

S=0

for k in range(len(L)) :

S=S+L[k].

return S

2) def mean_list(L) :

S=0

for k in range(len(L)) :

S=S+L[k].

return S/len(L)

Training 5 - Solution

def max_list(L):

M=L[0]

for k in range(len(L)) :

if L[k]>M :

M=L[k]

return M

Training 6 - Solution

1/ a. [3, 8, 13, 18, 23, 28]

b. [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76]

c. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]

d. [4, 49, 144, 289] i%5==2 means i = i mod(5) so i in {2,7,12,17}

2/ a. L5=[i for i in range(0,10000,10)]

b. L6=[i**2 for i in range(1,100,2)]

RESOURCES

INVOLVED LESSONS