Méthodologie de la Programmation
Notes de cours - Séance I
Typage Fort
score = 42
print("le score est " + score)
str(score)
print("le score est " + str(score))
Typage Dynamique
a = 42
type(a)
a = "test"
type(a)
a = 42
type(a)
a = "test"
type(a)
def truc(a):
return "42" + a
truc("truc")
truc(42)
Definir None
def func_none():
pass
print(func_none())
Comparer == et is
list1 = []
list2 = []
list3 = list1
list1 == list2
list1 is list2
list1 is list3
list3 = list3 + list2
list1 is list3
Flottants
String
a = "bli"
b = 'bli'
a == b
"Je m'appelle Python"
"dites \"AAAAAAH\"."
'Je m\'appelle Python'
'dites "AAAAAAH".'
"cou" * 2
"MIT" + "SIC"
Tuples
nom, age = "Bob", 24
age, nom = nom, age
def return_tuple(a, b):
return b, a
c = return_tuple(42, 227)
type(c)
len(c)
c[0]
c[1]
Dictionnaires
mdlp = {
'niveau': 'L1',
'semestre': 1,
'enseignants': {
'C': 'JP Palus',
'B': 'Alice Millour',
'C': 'Anna Pappa'
}
}
mdlp['niveau']
mdlp['enseignants']['C']
Itération sur les listes
lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[n * 2 for n in lst]
[n * n for n in lst if (n % 2) == 0]
Fonctions
def fact(n):
result = 1
while n > 1:
result = n * result
n = n - 1
return result
a = fact(5)