0% found this document useful (0 votes)
9 views6 pages

TD Liste

The document contains a series of Python exercises focusing on list manipulation functions such as reversing a list, counting occurrences, removing elements, and calculating statistics like mean and standard deviation. Each exercise includes code snippets and their expected outputs. The exercises cover various programming concepts including loops, conditionals, and list operations.

Uploaded by

isjsjsvsks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

TD Liste

The document contains a series of Python exercises focusing on list manipulation functions such as reversing a list, counting occurrences, removing elements, and calculating statistics like mean and standard deviation. Each exercise includes code snippets and their expected outputs. The exercises cover various programming concepts including loops, conditionals, and list operations.

Uploaded by

isjsjsvsks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Exercice 1

In [1]: def inverser(L):


L2 =
[]
for i in range(1len(L) -1 ,-1, -1
L2.append(L[i])
return L2

inverser([1,2,3,4])

Out[1]: [4, 3, 2, 1]

In [2]: def afficher (L):


for in range(len(L)-1,-1,-1
i
print(L[i])

afficher([1 ,2,3,4])
4
3
2
1

Exercice 2
In [3]: def occurrences (x,L):
occ =
[]
for i in range(len(L)):
if L[i] == x:
OcC.append(i)
return occ

occurrences(2,[1,2,3,2,4,2])

Out[3]: 1, 3, 5]
In [5]: def supprimer (x,L):
OcC = occurrences (x,L)
k =0
for i in occ:
L.pop(i-k)
k+=1
return L

supprimer (2,
[1,2,3,2,4,2])

Out [5]: [1, 3, 4]

Exercice 3
In [10]: def trancher1 (L1,L2):
L3 = [J
middle = len(L1 )//2

for i in range(middle+1):
L3.insert(len (L3)-1 ,L1[i]

for i in range(len(L2) -1):


L3.insert(len(L3) -1,L2[i]

for iin range (middle+1,len(L


)
L3.insert (len (L3 -1 ,L1 [i]

return L3

trancher1([1,1,1,1,1,1], [2,2,2,2,

Out[10]: [1, 1, 1, 2, 2,2, 2, 1, 1, 1]

In [13]: def trancher2 (L1,L2):


L3 = []
middle = len(L1)//2

for i in range(middle) :

L3.append(L1 [i])

for iin range(len( L2)-1):


L3.append(L2 [i])
19:03 ll 11% 2

for i in range(middle, len(L1)


L3.append(L1[i])

return L3
trancher2([1,1,1,1,1,1].[2,2,2,2,

Out [13]: [1, 1, 1, 2, 2,2, 2, 1, 1, 1]

In [16]: # décalage a droite des él ements


L= [1,2,3,4]
L.insert(2, 11)

Out[16]: [1, 2, 11, 3, 4]

Exercice 4

In [22]: def duplique(L):


verified = []
for item in L:
if item in verified:
return True
verified.append(item)
return False

duplique([1,2,3,4,2,6])

True
Out [22]:

Exercice 5
In [24]: def maxProd(L):
maxprod = 1

num1= 1

num2 = 1

for i in range(len(L)):
for j in range(len(L) )
:
if i l= j:
prod = L[i] * L[j
F mavnra nrnd

<
19:03 11%

prod = L[i] * L[j


if maxprod < prod
maxprod = pro
num1 = L[i]
num2 = L[j]

return maxprod, num1, num2

maxProd( [1,2,3,4,2,6])
(24, 4, 6)
Out[24]:

Exercice 6
In [28]: def vider (L):
return []

def copier (L):


L2 = []
for i in L:
L2.append(i)
return L2
# retourne le nombre d'oc
count()
def compter (L,x) :
k = 0
for i in L:
if i == X:
k+=1
return k

def etend (
L1 ,
L2):
):
for i in range(len(L2)
L1.append(L2[i])

return L1

# index() retourne 1'indice de la


def indexe(L, x):
k = None
for i in range(len(L)):
if L[i] == x:
k =i
break
19:03 .l11% 2
if L[i] == x:
k = i
break
return k

Exercice 7

In [27]: def moyenne (L):


S = 0
for item in L:
S+= item
return s/len( L)

def maxi(L):
maxim= L[0]
for i in range(1, len(L)):
if L[i] > maxim:
maxim = L[i]

return maxim

def combien(L ):
k= 0

moy = moyenne(L)
for item in L:
if item >= moy:
k+=1
return k

import math
def ecartType(L):
moy = moyenne(L)
Somme = 0
for item in L:
somme += (item - moy) **2

variance = somme/len(L)
return math.sqrt(variance)

ecartType([1,2,3,4,5])

1.4142135623730951
Out [27]:
19:03 l 11%
break
return k

Exercice 7

In [27]: def moyenne (L):


S = 0
for item in L:
St= item
return s/len(L)

def maxi(L):
maxim = L[0]
for i in range(1, len(L)):
if L[i] > maxim:
maxim= L[i]
return maxim

def combien(L):
k =0
moy = moyenne(L)
for item inL:
if item >= moy:
k+=1
return k

import math
def ecartType(L):
moy =moyenne (L)
Somme = 0
for item in L:
somme t= (item - moy )**2
variance = somme/len(L)
return math.sqrt(variance)

ecartType([1,2,3,4,5])

1.4142135623730951
Out[27]:

You might also like