Programmation Python : Les Bases

Programmation Python : Les Bases

Inclusion de Bibliothèques

import numpy as np
import matplotlib.pyplot as plt

Les Variables

Valeurs Numériques

Le type des variables numériques n’est pas à préciser

x = 2     # int
y = 3.5   # float

variables.svg

Chaines de caractère

chaine_1 = 'bonjour'  

On peut utiliser indifféremment les simples quotes ou doubles quotes ".

chaine.svg

Chaque caractère correspond à un code ASCII
ex : ‘b’ = 0x62

Concatenation :

chaine_1 = 'bonjour'
chaine_2 = 'roger'

chaine_1+' '+chaine_2  
'bonjour roger'

Conversion valeur <-> Chaine de caractère

nb_str = '45' 
# '45' est une chaine de caractères ascii 0x34 0x35 
type(nb_str)
str
nb_str
'45'
z = int(nb_str)
type(z)
int
z
45
str(z)
'45'

conversion.svg


Fonction print()

print("x = ", x , "| y = ", y)
x =  2 | y =  3.5

REMARQUE : Equivalent en langage C à

printf("x = %d | y = %f", x,y);  

Format : Conversion en chaine de caractères

nb_1 = 2
nb_2 = 3
fruit_1 = 'pommes'
fruit_2 = 'abricots'

message="j'ai mangé {} {} et {} {}".format(nb_1, fruit_1, nb_2, fruit_2)
print(message)

# Equivalent à :  
message=f"j'ai mangé {nb_1} {fruit_1} et {nb_2} {fruit_2}"
print(message)
j'ai mangé 2 pommes et 3 abricots  
j'ai mangé 2 pommes et 3 abricots

REMARQUE : Equivalent en langage C à :

char str[30];
nb_1 = 2 ; nb_2 = 3 ;
char fruit_1[]="pommes"; char fruit_2[]="abricots"

sprintf(str, "j'ai mangé %d %s et %d %s", nb_1, fruit_1, nb_2, fruit_2);  

Opérations Arithmétiques et Logiques

a = 3.25 
b = 4 

a + b # Addition  
7.25
a - b # Soustraction 
-0.75
a * b # Multiplication 
13.0
b / a # Division 
1.2307692307692308
b // a # Division Entière ( on ne garde que la partie entière du résultat )  
1.0
b**4.0 # Puissance b^4.0  
256.0
np.sqrt(b) # Racine Carrée, dans bibliothèque numpy    
2.0
a = 0x15 # Hexadecimal
b = 0x23
print("a = ", a, bin(a)) # decimal | binaire
a =  21 0b10101
format(a, '#010b') # a --> string | #:0b prefix | 0 : fill with zeros | 10 = 8 bits + 2 bits prefix | 
0b00010101
res = a & b # ET Logique
print(format(a, '#010b'),' and ',format(b, '#010b'),' = ',format(res, '#010b'))
0b00010101  and  0b00100011  =  0b00000001
res = a | b # OU Logique
print(format(a, '#010b'),' or ',format(b, '#010b'),' = ',format(res, '#010b'))
0b00010101  or  0b00100011  =  0b00110111
res = a ^ b # OU Exclusif
print(format(a, '#010b'),' xor ',format(b, '#010b'),' = ',format(res, '#010b'))
0b00010101  xor  0b00100011  =  0b00110110

Algorithmie

Comparaisons

x = 2
y = 3.5

# COMPARAISONS
print(x <= y)
print(x >= y)
print(x == y)
print(x != y)
True
False
False
True

If then else

if_then_else.svg

T = 20.0

if T > 24.0:
    print('trop chaud')
elif T < 16:
    print('trop froid')       
else:
    print('temperature correcte')
temperature correcte

Boucles

FOR

boucle_for.svg

for i in range(0,5,1): #  START: i==0 | STOP: i<5 | STEP i=i+1 
    print('in the loop for, i==', i )
in the loop for, i== 0
in the loop for, i== 1
in the loop for, i== 2
in the loop for, i== 3
in the loop for, i== 4

WHILE

boucle_while.svg

i=0
while i<5 :  
    print('in the loop while, i==', i)
    i=i+1
in the loop while, i== 0
in the loop while, i== 1
in the loop while, i== 2
in the loop while, i== 3
in the loop while, i== 4

Fonctions

Fonction générales def

appel_fonction.svg

hypothenuse c= 5.0 m
5.0