tp en python de structure conditionnelle

if ....then....else

En pseudo code (avec de nombreuses variantes pour identifier les blocs d'instructions), la structure conditionnelle s'écrit :

1 if condition then 2 bloc 1 3 else 4 bloc 2

Le bloc else n'est pas obligatoire.

En Python, voici la structure :

if condition :
	instruction(s)
else :
	instruction(s)

Vous remarquerez le symbole : très important en Python qui marque le début d'un bloc.

C'est l'indentation qui délimite le bloc d'instructions

Voici un exemple :

a=float(input("Entrer un nombre positif : "))
if a>=0 :
    print("Vous avez entré un nombre positif")
else :
    print("Vous avez entré un nombre négatif ?")
Qu'affiche ce programme avec a=-1 ? a=0? a=6? a="positif"?

La structure elif :

if condition1 :
	instruction(s)
elif condition2 :
	instruction(s)
elif condition3 :
	instruction(s)
else :
	instructions

Un autre exemple :

a=float(input("Entrer un nombre : "))
if a>0 :
    print("Vous avez entré un nombre strictement positif.")  
elif a==0 :
    print("Vous avez entré un nombre nul.")
else :
    print("Vous avez entré un nombre strictement négatif.")

A vous de jouer

Effectuer les exercices du dépôt

Espace vidéos

Les fonctions (en mode console EDUPYTHON) :