====== Différences ======
Cette page vous affiche les différences entre la révision choisie et la version actuelle de la page.
|
cours:activite1:python [2013/10/03 12:01] r.doiteau créée |
cours:activite1:python [2019/05/11 14:35] (Version actuelle) |
||
|---|---|---|---|
| Ligne 59: | Ligne 59: | ||
| #import le module math | #import le module math | ||
| from math import* | from math import* | ||
| - | #affiche la dmeande de valeur | + | #affiche la demande de valeur |
| floa = float(input ('Saisir un flottant :')) | floa = float(input ('Saisir un flottant :')) | ||
| #indique les exceptions | #indique les exceptions | ||
| Ligne 123: | Ligne 123: | ||
| | | ||
| </code> | </code> | ||
| + | |||
| ==== Créer une boucle ==== | ==== Créer une boucle ==== | ||
| Ligne 173: | Ligne 174: | ||
| </code> | </code> | ||
| - | ==== Interrompre une séquence une séquence ==== | + | ==== Interrompre une séquence ==== |
| <code=python> | <code=python> | ||
| Ligne 181: | Ligne 182: | ||
| | | ||
| print (n) | print (n) | ||
| + | </code> | ||
| + | ==== Boucle en gérant une exception ==== | ||
| + | |||
| + | <code=python> | ||
| + | from math import* | ||
| + | for n in range(-3,3): | ||
| + | try : | ||
| + | print ('%.3f' %(sin(n)/n)) | ||
| + | except: | ||
| + | print (1) | ||
| </code> | </code> | ||
| + | ==== Liste ==== | ||
| + | <code=python> | ||
| + | list1 = [1,2,3,4,5] | ||
| + | n = int (input ("Entrer un nombre entier :")) | ||
| + | for n1 in list1: | ||
| + | if n1==n: | ||
| + | print ("trouvé") | ||
| + | break | ||
| + | else: | ||
| + | continue | ||
| + | if n1!=n: | ||
| + | print ("pas trouvé") | ||
| + | | ||
| + | n1= int(input("entrer un nombre entier : ")) | ||
| + | i=2 | ||
| + | |||
| + | while i != n1 : | ||
| + | |||
| + | if n1 % i ==0 and i != n1: | ||
| + | print (n1,"n'est pas premier","son diviseur est",i) | ||
| + | break | ||
| + | else : | ||
| + | i = i + 1 | ||
| + | | ||
| + | if i == n1: | ||
| + | print (n1,"est un nombre premier") | ||
| + | </code> | ||
| + | |||
| + | |||
| + | **liste trier** | ||
| + | <code=python> | ||
| + | n1 = [17,38,10,25,72,] | ||
| + | n2 = sorted (n1) | ||
| + | print (n1) | ||
| + | print (n2) | ||
| + | </code> | ||
| + | |||
| + | **ajouter un entier a la lister** | ||
| + | <code=python> | ||
| + | n1.append (12) | ||
| + | print (n1) | ||
| + | </code> | ||
| + | |||
| + | **enlever un entier a la lister** | ||
| + | <code=python> | ||
| + | n1.remove (38) | ||
| + | print (n1) | ||
| + | </code> | ||
| + | |||
| + | **liste renverser** | ||
| + | <code=python> | ||
| + | n1 = [17,38,10,25,72,12] | ||
| + | n1.reverse() | ||
| + | print (n1) | ||
| + | </code> | ||
| + | |||
| + | **sous liste en le 2eme et 3eme** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[2:3]) | ||
| + | </code> | ||
| + | |||
| + | **sous liste jusqu'au deuxiéme** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[:2]) | ||
| + | </code> | ||
| + | |||
| + | **sous liste jusqu'au deuxiéme** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[3:]) | ||
| + | </code> | ||
| + | |||
| + | **sous liste jusqu'au deuxiéme** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[:]) | ||
| + | </code> | ||
| + | |||
| + | **sous liste jusqu'au deuxiéme** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[-1]) | ||
| + | sous liste jusqu'au deuxiéme | ||
| + | </code> | ||
| + | |||
| + | **afficher une sous liste** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[1:4]) | ||
| + | </code> | ||
| + | |||
| + | **afficher le deernier élément en utilisant un indice négatif** | ||
| + | <code=python> | ||
| + | n1 = [17,10,25,72,12] | ||
| + | print (n1[-1]) | ||
| + | </code> | ||
| + | |||
| + | ==== Fonction ==== | ||
| + | |||
| + | <code=python> | ||
| + | ################################################################################ | ||
| + | # Description : Fonction qui recoit une chaine de caractere et # | ||
| + | # qui retourne un dictionnaire avec chaque mots et son nombre # | ||
| + | # de caractere. # | ||
| + | # Entrée : une chaine de caractère. # | ||
| + | # sortie : un dictionnaire avec comme clé un mots et en valeur # | ||
| + | # son nombre de caractere. # | ||
| + | ################################################################################ | ||
| + | |||
| + | def compter_mots (chaine): | ||
| + | |||
| + | #### Description fonction #### | ||
| + | |||
| + | ## Variable ## | ||
| + | dicomots = {} | ||
| + | listechaine = [] | ||
| + | |||
| + | ## Fonction ## | ||
| + | |||
| + | #sépare les mpots | ||
| + | listechaine = chaine.split() | ||
| + | for mot in listechaine: | ||
| + | if mot in dicomots: | ||
| + | dicomots[mot] = dicomots[mot]+1 | ||
| + | else: | ||
| + | dicomots [mot] = 1 | ||
| + | |||
| + | return dicomots | ||
| + | |||
| + | #### Programme #### | ||
| + | |||
| + | comptage = compter_mots(input("entrer une chaîne de caractére : ")) | ||
| + | print('------------------------------------------') | ||
| + | |||
| + | for i in comptage.keys(): | ||
| + | print(i, comptage[i]) | ||
| + | input () | ||
| + | </code> | ||
| + | |||
| + | === Turtle === | ||
| + | |||
| + | <code=python> | ||
| + | from turtle import * | ||
| + | |||
| + | #### fonction carre #### | ||
| + | def carre (taille,couleur): | ||
| + | |||
| + | ## variable ## | ||
| + | c=0 | ||
| + | color(couleur) | ||
| + | |||
| + | ## programme ## | ||
| + | while c<4: | ||
| + | right (90) | ||
| + | forward (taille) | ||
| + | c=c+1 | ||
| + | |||
| + | |||
| + | #### programme principale #### | ||
| + | i=int(input("entrer une taille: ")) | ||
| + | |||
| + | n=0 | ||
| + | |||
| + | u=-200 | ||
| + | up() | ||
| + | goto(-200,0) | ||
| + | down() | ||
| + | |||
| + | while n<10: | ||
| + | carre(i,'red') | ||
| + | n=n+1 | ||
| + | u=u+i+5 | ||
| + | up() | ||
| + | goto(u,0) | ||
| + | </code> | ||
| + | |||
| + | {{:cours:activite1:python.png|}} | ||
| + | |||
| + | <code=python> | ||
| + | ############################################# | ||
| + | # Programme Python type # | ||
| + | # auteur : R.Doiteau # | ||
| + | ############################################# | ||
| + | |||
| + | ############################################# | ||
| + | # Importation de fonctions externes : | ||
| + | |||
| + | from turtle import * | ||
| + | |||
| + | ############################################## | ||
| + | # Def fonction | ||
| + | |||
| + | def carre(taille, couleur): | ||
| + | #variable | ||
| + | color (couleur) | ||
| + | c=0 | ||
| + | |||
| + | #fonction | ||
| + | while c<4: | ||
| + | forward (taille) | ||
| + | right(90) | ||
| + | c=c+1 | ||
| + | |||
| + | |||
| + | ############################################## | ||
| + | # Corps du programme | ||
| + | |||
| + | up () | ||
| + | goto(-200, 20) | ||
| + | i=0 | ||
| + | taille2=int(input("entrer une taille: ")) | ||
| + | couleur2=input("entrer une couleur: ") | ||
| + | |||
| + | while i<10: | ||
| + | carre (taille2,couleur2) | ||
| + | up() | ||
| + | forward(taille2+5) | ||
| + | down() | ||
| + | i=i+1 | ||
| + | |||
| + | </code> | ||
| + | <code=python> | ||
| + | ############################################# | ||
| + | # Programme Python type # | ||
| + | # auteur : R.Doiteau # | ||
| + | ############################################# | ||
| + | |||
| + | ############################################# | ||
| + | # Importation de fonctions externes : | ||
| + | |||
| + | from turtle import * | ||
| + | |||
| + | ############################################## | ||
| + | # Def fonction | ||
| + | |||
| + | def carre(taille,angle,couleur): | ||
| + | #variable | ||
| + | color(couleur) | ||
| + | c=0 | ||
| + | right(angle) | ||
| + | #fonction | ||
| + | while c<4: | ||
| + | forward (taille) | ||
| + | right(90) | ||
| + | c=c+1 | ||
| + | |||
| + | def triangle(taille,angle,couleur): | ||
| + | color (couleur) | ||
| + | c=0 | ||
| + | right(angle) | ||
| + | while c<3: | ||
| + | forward(taille) | ||
| + | right(120) | ||
| + | c=c+1 | ||
| + | |||
| + | |||
| + | ############################################## | ||
| + | # Corps du programme | ||
| + | |||
| + | taille2=int(input("entrer une taille: ")) | ||
| + | couleur2=input("entrer une couleur pour les carrés: ") | ||
| + | couleur3=input("entrer une couleur pour les triangles: ") | ||
| + | |||
| + | |||
| + | up() | ||
| + | goto (-250, 50) | ||
| + | |||
| + | |||
| + | i=0 | ||
| + | u=0 | ||
| + | |||
| + | while i<10: | ||
| + | down() | ||
| + | triangle(taille2+u,0,couleur3) | ||
| + | up() | ||
| + | forward(taille2+5+u) | ||
| + | down() | ||
| + | carre(taille2+u,0,couleur2) | ||
| + | up() | ||
| + | forward(taille2+5+u) | ||
| + | |||
| + | i=i+1 | ||
| + | u=u+5 | ||
| + | </code> | ||
| + | |||
| + | |||
| + | === Base,début,inc === | ||
| + | |||
| + | <code=python> | ||
| + | def table(base, debut, fin,inc): | ||
| + | n = debut | ||
| + | |||
| + | while n <= fin: | ||
| + | print (n,'x', base,'=', n * base) | ||
| + | n += inc | ||
| + | # exemple d’appel : | ||
| + | table(7, 2, 11,1) | ||
| + | |||
| + | </code> | ||
| + | |||
| + | === Cube === | ||
| + | <code=python> | ||
| + | def cube(litre): | ||
| + | return litre**3 | ||
| + | |||
| + | print (cube (100),"Litres") | ||
| + | |||
| + | </code> | ||
| + | |||
| + | === Volume sphere === | ||
| + | <code=python> | ||
| + | from math import pi | ||
| + | |||
| + | def cube(x): | ||
| + | return x**3 | ||
| + | |||
| + | def volume_sphere (z): | ||
| + | return 4.0*pi*cube(z)/3.0 | ||
| + | |||
| + | |||
| + | rayon = float (input("entrer un rayon : ")) | ||
| + | |||
| + | print('Le volume de la sphére de rayon ',round(rayon,0),'est de',round(volume_sphere(rayon),0)) | ||
| + | </code> | ||
| + | |||
| + | === Equation === | ||
| + | <code=python> | ||
| + | ########################################################## | ||
| + | # fonction qui retourne f(x)=2x3+x-5 # | ||
| + | ########################################################## | ||
| + | | ||
| + | def maFonction(x): | ||
| + | return 2*x**3 + x-5 | ||
| + | |||
| + | def tabuler(fonction, bornInf, bornSup, nombrePas): | ||
| + | h=(bornSup-bornInf)/float(nombrePas) | ||
| + | x=bornInf | ||
| + | while x< bornSup: | ||
| + | y=fonction(x) | ||
| + | print("f({:.2f}) = {:.3f}".format(x, y)) | ||
| + | x+=h | ||
| + | |||
| + | |||
| + | |||
| + | borne_inf = float(input("Borne inférieur : ")) | ||
| + | borne_sup = float(input("Borne supérieur : ")) | ||
| + | nbPas = int(input("Nombre de pas: ")) | ||
| + | tabuler(maFonction, borne_inf, borne_sup, nbPas) | ||
| + | |||
| + | </code> | ||