Exercice 15
Ecire un programme en python qui génère l'ensemble des entiers de 1 à 30 de deux façons différentes.
Solution
1ère méthode:
1 2 3 4 5 6 7 |
A = { n for n in range(1, 31) } print(A) """ output: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30} |
2ème méthode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# initialiser l'ensemble demandé A = set({}) # parcourir l'ensemble des entiers de 1 à 30 for i in range(1 , 31): A.add(i) # afficher l'ensemble A print(A) """ output {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30} """ |
1 thought on “Solution Exercice 15: générer un ensemble d'entiers en python”