Python. Mémo

Variable, types

entier

In [1]:
a = 5
In [2]:
type(a)
Out[2]:
int

flottant

In [3]:
b = 110.2
In [4]:
type(b)
Out[4]:
float

chaîne de caractères

In [5]:
c = 'bonjour'    #chaine de caracteres (string)
In [6]:
type(c)
Out[6]:
str

liste

In [7]:
d = [5,4,8,9]    # liste (tableau)
type(d)
Out[7]:
list

booléen

In [8]:
e = ( 5 == 4+1 )    # booleen
print(e)
True
In [9]:
type(e)
Out[9]:
bool
In [10]:
print(not e)    # negation
False
In [11]:
print( (4 <= 5) or (4 > 7))    # ou
True
In [12]:
print( (4 <= 5) and (4 != 7))    # et
True

Entrée

int convertit la saisie en un entier (sinon la saisie est une chaine).

In [13]:
age = int(input('Saisir votre âge : '))
Saisir votre âge : 20

Sortie

In [14]:
print("Vous avez ", age , " ans.")
Vous avez  20  ans.

Si ... alors ... sinon

In [15]:
if age < 18:
    print('Tu es mineur')
else:
    print('Vous êtes majeur')
Vous êtes majeur

Tant que ... faire ...

In [16]:
k = 0
while k < 5:
    print(k)
    k = k+1
print('fin')
    
0
1
2
3
4
fin

Listes

In [17]:
L1 = []        # liste vide
L2 = [0]*4

print(L1)
print(L2)
[]
[0, 0, 0, 0]
In [18]:
L3 = [1, False, "TRUC", 0.1, None]
print(L3)
print(L3[1])
[1, False, 'TRUC', 0.1, None]
False

longueur de liste

In [19]:
print(len(L3))
5

concaténation de listes

In [20]:
print([] + [1,2] + [3,4,5])
[1, 2, 3, 4, 5]

listes de listes (tableaux à deux dimensions)

In [21]:
L4 = [[1,9,300,3] , [5,6,7,8]]

print(len(L4))
print(L4[1])
print(L4[0][2])
2
[5, 6, 7, 8]
300

Pour i de ... à ...

In [22]:
for i in range(5):
    print(i)
0
1
2
3
4
In [23]:
for i in range(3, 6):
    print(i)
3
4
5
In [24]:
for i in range(3, 11, 2):
    print(i)
3
5
7
9
In [25]:
for i in range(6, 2, -1):
    print(i)
6
5
4
3
In [26]:
ma_liste = [2 , 'a' , True]
for i in ma_liste:
    print(i)
2
a
True
In [27]:
for i in range(len(ma_liste)):
    print(i, ":", ma_liste[i])
0 : 2
1 : a
2 : True

Chaînes de caractères

In [28]:
c1 = "unMot"
c2 = 'blabla57'
print(len(c1))
print(c1[0])
print(c1+c2)    #concaténation
5
u
unMotblabla57

Fonctions

In [29]:
def f(x):
    return x+10

print(f(7))
17
In [30]:
def g(a,b):
    if a>b :
        return a
    else:
        return b
    
print(g(4,7))
        
7

Calculs, tests

In [31]:
4 + 3*10
Out[31]:
34
In [32]:
2**10     # puissance
Out[32]:
1024
In [33]:
5/3    # division
Out[33]:
1.6666666666666667
In [34]:
5//3    #division entiere (quotient de la division euclienne de 5 par 3)
Out[34]:
1
In [35]:
5 % 3    #reste de la division euclidienne de 5 par 3
Out[35]:
2
In [36]:
from math import sqrt     #racine carree
sqrt(2)
Out[36]:
1.4142135623730951
In [37]:
from math import floor     #partie entière
floor(2.35)
Out[37]:
2
In [38]:
print((True and False) or not False)
for op in ["+", "*", "**",
           "-", "/", "//", "%",
           "==", "!=",
           "<", ">", ">=", "<="]:
    expr = "28" + op + "30"
    print(expr, eval(expr))
True
28+30 58
28*30 840
28**30 25986090120790645892257018950637850957185024
28-30 -2
28/30 0.9333333333333333
28//30 0
28%30 28
28==30 False
28!=30 True
28<30 True
28>30 False
28>=30 False
28<=30 True

 Nombres aléatoires

In [39]:
from random import randint

randint(0,10)    # entier aleatoire entre 0 et 10, 0 et 10 compris
Out[39]:
2
In [40]:
randint(-10,10)    # entier aleatoire entre -10 et 10 , bornes comprises
Out[40]:
-9