liste = [10, 8, 3, 12, 1]
#Premier élément
print(liste[0])
10
#Dernier élément
print(liste[-1])
1
temp = liste[0]
liste[0] = liste[2]
liste[2] = temp
print(liste)
[3, 8, 10, 12, 1]
liste.append(6)
print(liste)
[3, 8, 10, 12, 1, 6]
liste.extend([2,4,5,7,8,11])
print(liste)
[3, 8, 10, 12, 1, 6, 2, 4, 5, 7, 8, 11]
liste.index(5)
8
liste.remove(5)
print(liste)
[3, 8, 10, 12, 1, 6, 2, 4, 7, 8, 11]
for elt in liste:
if elt % 2 == 0:
print(elt)
8 10 12 6 2 4 8
for i, elt in enumerate(liste): #ici "i" représente l'indice, et "elt" la valeur
if i % 2 == 0:
print(elt)
3 10 1 2 7 11
liste.reverse() #renverser la liste
print(liste)
[11, 8, 7, 4, 2, 6, 1, 12, 10, 8, 3]
liste2 = []
for elt in liste:
if elt % 2 == 0:
liste2.append(elt)
print(liste2)
[8, 4, 2, 6, 12, 10, 8]
print(liste)
for i, elt in enumerate(liste):
liste[i] = elt+1
print(liste)
[11, 8, 7, 4, 2, 6, 1, 12, 10, 8, 3] [12, 9, 8, 5, 3, 7, 2, 13, 11, 9, 4]
max(liste)
13
liste_copy1 = liste
liste_copy2 = list(liste)
print(liste)
print(liste_copy1)
print(liste_copy2)
[13, 12, 11, 9, 9, 8, 7, 5, 4, 3, 2] [13, 12, 11, 9, 9, 8, 7, 5, 4, 3, 2] [13, 12, 11, 9, 9, 8, 7, 5, 4, 3, 2]
liste.sort(reverse=True)
print(liste)
print(liste_copy1)
print(liste_copy2)
[13, 12, 11, 9, 9, 8, 7, 5, 4, 3, 2] [13, 12, 11, 9, 9, 8, 7, 5, 4, 3, 2] [12, 9, 8, 5, 3, 7, 2, 13, 11, 9, 4]
dico = {}
dico[1] = "Lloris"
dico[7] = "Griezmann"
dico[6] = "Pogba"
dico[4] = "Varane"
dico[11] = "Dembele"
dico[10] = "MBappé"
print(dico)
{1: 'Lloris', 7: 'Griezmann', 6: 'Pogba', 4: 'Varane', 11: 'Dembele', 10: 'MBappé'}
print(dico.keys())
dict_keys([1, 7, 6, 4, 11, 10])
print(dico.values())
dict_values(['Lloris', 'Griezmann', 'Pogba', 'Varane', 'Dembele', 'MBappé'])
for elt in dico.keys():
if elt %2 ==0:
print(str(elt)+" "+dico[elt])
6 Pogba 4 Varane 10 MBappé
values = list(dico.keys())
values.sort()
for elt in values:
print(str(elt)+" "+dico[elt])
1 Lloris 4 Varane 6 Pogba 7 Griezmann 10 MBappé 11 Dembele
dico[11] = "Fékir"
print(dico)
{1: 'Lloris', 7: 'Griezmann', 6: 'Pogba', 4: 'Varane', 11: 'Fékir', 10: 'MBappé'}
dico[9] = "Giroud"
print(dico)
{1: 'Lloris', 7: 'Griezmann', 6: 'Pogba', 4: 'Varane', 11: 'Fékir', 10: 'MBappé', 9: 'Giroud'}
del dico[6]
print(dico)
{1: 'Lloris', 7: 'Griezmann', 4: 'Varane', 11: 'Fékir', 10: 'MBappé', 9: 'Giroud'}
menu = {}
menu["lundi"] = "Riz"
menu["mardi"] = "To"
menu["mercredi"]= "Couscous"
print(menu)
{'lundi': 'Riz', 'mardi': 'To', 'mercredi': 'Couscous'}
15