0% ont trouvé ce document utile (0 vote)
7 vues3 pages

Correction TP Python pour MPSI-TSI

Le document présente une correction de travaux pratiques sur le langage Python pour la filière MPSI-TSI. Il contient plusieurs exercices abordant des concepts de base comme la division euclidienne, le produit de nombres, la détermination d'années bissextiles, et la suite de Syracuse. Chaque exercice est illustré par des exemples de code Python et des explications sur leur fonctionnement.

Transféré par

ggyoussef146
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
7 vues3 pages

Correction TP Python pour MPSI-TSI

Le document présente une correction de travaux pratiques sur le langage Python pour la filière MPSI-TSI. Il contient plusieurs exercices abordant des concepts de base comme la division euclidienne, le produit de nombres, la détermination d'années bissextiles, et la suite de Syracuse. Chaque exercice est illustré par des exemples de code Python et des explications sur leur fonctionnement.

Transféré par

ggyoussef146
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

CPGE : Salmane Al Farissi Année scolaire : 2025/2026

Filière : MPSI-TSI Matière : Informatique

Correction TP : Introduction au langage python

Exercice 1 :

1 a=i n t ( i n p u t ( " E n t r e r un p r e m i e r e n t i e r : " ) )


2 b=i n t ( i n p u t ( " E n t r e r un deuxieme e n t i e r : " ) )
3 p r i n t ( f " D i v i s i o n e u c l i d i e n n e : { a}={b}∗{ a //b}+{a%b} " )

Exercice 2 :

1 m=e v a l ( i n p u t ( " E n t r e r un p r e m i e r nombre : " ) )


2 n=e v a l ( i n p u t ( " E n t r e r un deuxieme nombre : " ) )
3 i f m==0 o r n==0:
4 p r i n t ( f " l e p r o d u i t de {m} e t {n} e s t n u l " )
5 e l i f m>0 and n<0 o r m<0 and n>0:
6 p r i n t ( f " l e p r o d u i t de {m} e t {n} e s t n e g a t i f " )
7 else :
8 p r i n t ( f " l e p r o d u i t de {m} e t {n} e s t p o s i t i f " )

Exercice 3 :

1 x=e v a l ( i n p u t ( " E n t r e r l e p r e m i e r nombre : " ) )


2 y=e v a l ( i n p u t ( " E n t r e r l e deuxieme nombre : " ) )
3 z=e v a l ( i n p u t ( " E n t r e r l e t r o i s i e m e nombre : " ) )
4 i f x>y and x>z :
5 p r i n t ( f " l e maximum de {x } , { y} e t { z } e s t : {x} " )
6 e l i f y>x and y>z :
7 p r i n t ( f " l e maximum de {x } , { y} e t { z } e s t : {y} " )
8 else :
9 p r i n t ( f " l e maximum de {x } , { y} e t { z } e s t : { z } " )

Exercice 4 :

1 annee=i n t ( i n p u t ( " E n t r e r une annee : " ) )


2 i f annee%400==0 o r annee%4==0 and annee %100!=0:
3 p r i n t ( f " { annee } e s t une annee b i s s e x t i l e " )
4 else :
5 p r i n t ( f " { annee } n ’ e s t pas une annee b i s s e x t i l e " )

1
Exercice 5 :

1 n=i n t ( i n p u t ( " E n t r e r un nombre e n t i e r : " ) )


2 p r i n t ( f " Les nombres p a i r s c o m p r i s e n t r e 0 e t {n} s o n t : " , end=" " )
3 f o r i i n r a n g e ( n+1) :
4 i f i %2==0:
5 p r i n t ( i , end=" # " )

Exercice 6 :
Première version :
1 e=i n t ( i n p u t ( " E n t r e r un nombre e n t i e r : " ) )
2 S=0;P=1
3 f o r i i n r a n g e ( 1 , e +1 ,2) :
4 S+=i
5 f o r i i n r a n g e ( 2 , e +1 ,2) :
6 P∗= i
7 p r i n t ( f " Le p r o d u i t d e s nombres p a i r s c o m p r i s e n t r e 1 e t { e } e s t : {P} " )
8 p r i n t ( f " La somme d e s nombres i m p a i r s c o m p r i s e n t r e 1 e t { e } e s t : {S} " )

Deuxième version :
1 e=i n t ( i n p u t ( " E n t r e r un nombre e n t i e r : " ) )
2 S=0;P=1
3 f o r i i n r a n g e ( 1 , e +1) :
4 i f i %2==0:
5 P∗= i
6 else :
7 S+=i
8 p r i n t ( f " Le p r o d u i t d e s nombres p a i r s c o m p r i s e n t r e 1 e t { e } e s t : {P} " )
9 p r i n t ( f " La somme d e s nombres i m p a i r s c o m p r i s e n t r e 1 e t { e } e s t : {S} " )

Exercice 7 :

1 nmb=i n t ( i n p u t ( " E n t r e r un e n t i e r s t r i c t e m e n t p o s i t i f : " ) )


2 d=0
3 p r i n t ( f " D i v i s e u r s p r o p r e s s a n s r e p e t i t i o n de {nmb} e s t : " , end=" " )
4 f o r i i n r a n g e ( 2 ,nmb) :
5 i f nmb%i ==0:
6 p r i n t ( i , end=" " )
7 d+=1
8 i f d!=0:
9 p r i n t ( f " ( S o i t {d} d i v i s e u r s p r o p r e s ) " )
10 else :
11 p r i n t ( " aucun ! i l e s t p r e m i e r " )

2
Exercice 8 :

1 p=i n t ( i n p u t ( " E n t r e r un e n t i e r : " ) )


2 S=1
3 f o r i i n r a n g e ( 2 , p//2+1) :
4 i f p%i ==0:
5 S+=i
6 i f S==p :
7 p r i n t ( f " {p} e s t un nombre p a r f a i t " )
8 else :
9 p r i n t ( f " {p} n ’ e s t pas un nombre p a r f a i t " )

Exercice 9 :
1.
1 a=i n t ( i n p u t ( " E n t r e r un nombre e n t i e r s t r i c t e m e n t p o s i t i f : " ) )
2 Un=a
3 p r i n t ( f " l e s 11 p r e m i e r s t e r m e s de l a s u i t e de s y r a c u s e de l ’ e n t i e r { a } : " )
4 p r i n t ( f "U{0}={Un} " , end=" " )
5 f o r i in range (1 ,11) :
6 i f Un%2==0:
7 Un=Un//2
8 else :
9 Un=3∗Un+1
10 p r i n t ( f "U{ i }={Un} " , end=" " )

2.
1 Un=a
2 n=0
3 w h i l e Un! = 1 :
4 i f Un%2==0:
5 Un=Un//2
6 else :
7 Un=3∗Un+1
8 n+=1
9 p r i n t ( f " L ’ i n d i c e du p r e m i e r terme de l a s u i t e de S y r a c u s e de l ’ e n t i e r { a } q u i a
a t t e i n t 1 e s t : {n} " )

Vous aimerez peut-être aussi