Memento Python

if…elif…else

x = -2
if x > 0:
    print(x, "est positif")
else:
    print(x, "est négatif ou nul")
print("Fin")
x = 0
if x > 0:
    print(x, "est positif")
elif x < 0:
    print(x, "est négatif")
else:
    print(x, "est nul")
print("Fin")

Boucles while et for

i = 0
while i < 7 :
    i+=1
    print(i)
for i in range(4):
    print("i a pour valeur", i)
for x in range(2, 6):
  print(x)
for i in [0, 1, 2, 3]:
    print("i a pour valeur", i)

Tableaux et parcours de tableaux

c = ["Marc", "est", "dans", "le", "jardin"]
for i in range(len(c)):
    print("i vaut", i, "et c[",i,"] vaut", c[i])
c = ["Marc", "est", "dans", "le", "jardin"]
for i in c:
    print("i vaut", i)

Fonction (sans et avec valeur de retour)

def compteur_complet(start, stop, step):
    i = start
    while i < stop:
        print(i)
        i = i + step

compteur_complet(1, 7, 2)
def cube(w):
    return w**3

resultat = cube(4)
print(resultat)

Importation de librairie externe

import math  
  
# print the square root of  0  
print(math.sqrt(0))  
  
# print the square root of 4 
print(math.sqrt(4))  
  
# print the square root of 3.5 
print(math.sqrt(3.5))  
import math

math.pow(5,4)