Outils pour utilisateurs

Outils du site


2gy_2021:2gy06:s35_intro

Ceci est une ancienne révision du document !


Introduction

Python Exemples

Print

print "(2+2)*3=12"

Programmation modulaire

from gturtle import *

def triangle50():
    forward(50)
    right(120)
    forward(50)
    right(120)
    forward(50)

makeTurtle()
triangle50()
left(45)
triangle50()

Ici on peut appeler triangle50() aussi une fonction ou une méthode.

Boucles, input et paramètres

Polygone

from gturtle import *

def polygone (n, cote):
    repeat n:
        forward(cote)
        right(360 / n)
        
def dessine_polygone():
    nb_cotes = input("Nombre de côtés : ")
    perimetre = input("Mesure du périmètre : ")
    polygone(nb_cotes, perimetre / nb_cotes)

makeTurtle()
hideTurtle()
dessine_polygone()
dessine_polygone()
  

Boucles while

while <codition>:
    <consigne>

Le consigne est répétée tant que la condition est remplie.

from gturtle import *

def spirale(cote, allongement, cote_max):
    while cote<=cote_max:
        forward(cote)
        right(30)
        cote = cote + allongement
        
makeTurtle()
hideTurtle()
spirale(5, 4, 150)

Branchements / Instructions conditionnelles

Instructions avec if

if <condition>:
    <instruction>
prenom = input("Prénom?")
if prenom =="Lisa":
    print "Bonjour Lisa!"
    print "Quel plaisir de vous revoir"
if prenom == "Gaston":
    print "Bonjour Gaston"
    print "Quelle plaisir de vous retrouver"

Instructions if else

if <condition>:
    <instruction 1>
else :
    <instruction 2>
a = 35
b = 33
if b > a:
    print("b is greater than a")
else:
    print("b is less than or equal to a")  
def minmax (a, b, c):
    if a>b:
        minimum = a
        maximum = b 
    else:
        minimum = b
        maximum = a
    if c < minimum:
        minimum = c
    if c>maximum:
        maximum = c   
    print (minimum, maximum)
    
minmax(5,3,17)

Instructions if elif ... else

if <condition 1>:
    <instruction 1>
elif <condition 2>:
    <instruction 2>
else :
    <instruction 3>
a = 35
b = 33
if b > a:
    print("b is greater than a")
elif a == b:
    print("a and b are equal")
else:
    print("b is less than a")  
2gy_2021/2gy06/s35_intro.1599021879.txt.gz · Dernière modification: 2020/09/02 04:44 par ilko