Es. Python tutorial - Cicli 2
Versione italiana
Esercizi sui Cicli in Python
Esercizio 1: Contare da 20 a 1
Scrivi un programma che utilizzi un ciclo for
per
contare da 20 a 1 e stampare ogni numero.
Esercizio 2: Somma dei Multipli di 3
Scrivi un programma che calcoli la somma di tutti i multipli di 3 da
1 a 100 utilizzando un ciclo while
.
Esercizio 3: Stampa i Numeri da 1 a n
Scrivi un programma che prenda un numero intero positivo
n
dall’utente e stampi tutti i numeri da 1 a n
utilizzando un ciclo for
.
Esercizio 4: Contare le Consonanti
Scrivi un programma che conti il numero di consonanti in una stringa
fornita dall’utente utilizzando un ciclo for
.
Esercizio 5: Potenze di 2
Scrivi un programma che stampi le potenze di 2 da 2^0 a 2^10
utilizzando un ciclo for
.
Esercizio 6: Somma dei Numeri Fino a n
Scrivi un programma che calcoli la somma di tutti i numeri interi da
1 a n
, dove n
è fornito dall’utente,
utilizzando un ciclo while
.
Esercizio 7: Stampa un Quadrato di Asterischi
Scrivi un programma che stampi un quadrato di asterischi di
dimensione n
, dove n
è fornito dall’utente.
Utilizza un ciclo for
.
Esercizio 8: Contare i Numeri Positivi e Negativi
Scrivi un programma che chieda all’utente di inserire numeri fino a
quando non viene inserito 0. Conta e stampa quanti numeri positivi e
quanti negativi sono stati inseriti utilizzando un ciclo
while
.
Esercizio 9: Stampa i Numeri FizzBuzz
Scrivi un programma che stampi i numeri da 1 a 100. Per i multipli di
3, stampa “Fizz” invece del numero, per i multipli di 5 stampa “Buzz”, e
per i multipli di entrambi stampa “FizzBuzz”. Utilizza un ciclo
for
.
Esercizio 10: Inverso di una Stringa
Scrivi un programma che prenda una stringa dall’utente e stampi la
stringa al contrario utilizzando un ciclo for
.
Risposte
Risposta 1: Contare da 20 a 1
for i in range(20, 0, -1):
print(i)
Risposta 2: Somma dei Multipli di 3
= 0
somma = 1
n while n <= 100:
if n % 3 == 0:
+= n
somma += 1
n print(somma)
Risposta 3: Stampa i Numeri da 1 a n
= int(input("Inserisci un numero intero positivo: "))
n for i in range(1, n + 1):
print(i)
Risposta 4: Contare le Consonanti
= input("Inserisci una stringa: ")
stringa = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
consonanti = 0
contatore_consonanti for char in stringa:
if char in consonanti:
+= 1
contatore_consonanti print(contatore_consonanti)
Risposta 5: Potenze di 2
for i in range(11):
print(f"2^{i} = {2 ** i}")
Risposta 6: Somma dei Numeri Fino a n
= int(input("Inserisci un numero intero positivo: "))
n = 0
somma = 1
contatore while contatore <= n:
+= contatore
somma += 1
contatore print(somma)
Risposta 7: Stampa un Quadrato di Asterischi
= int(input("Inserisci la dimensione del quadrato: "))
n for i in range(n):
print('*' * n)
Risposta 8: Contare i Numeri Positivi e Negativi
= 0
positivi = 0
negativi = None
numero
while numero != 0:
= int(input("Inserisci un numero (0 per terminare): "))
numero if numero > 0:
+= 1
positivi elif numero < 0:
+= 1
negativi
print(f"Numeri positivi: {positivi}")
print(f"Numeri negativi: {negativi}")
Risposta 9: Stampa i Numeri FizzBuzz
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Risposta 10: Inverso di una Stringa
= input("Inserisci una stringa: ")
stringa = ""
inverso for char in stringa:
= char + inverso
inverso print(inverso)
English version
Python Loop Exercises
Exercise 1: Counting from 20 to 1
Write a program that uses a for
loop to count from 20 to
1 and print each number.
Exercise 2: Summing Multiples of 3
Write a program that calculates the sum of all multiples of 3 from 1
to 100 using a while
loop.
Exercise 3: Printing Numbers from 1 to n
Write a program that takes a positive integer n
from the
user and prints all the numbers from 1 to n
using a
for
loop.
Exercise 4: Counting Consonants
Write a program that counts the number of consonants in a
user-supplied string using a for
loop.
Exercise 5: Powers of 2
Write a program that prints the powers of 2 from 2^0 to 2^10 using a
for
loop.
Exercise 6: Adding Numbers Up to n
Write a program that calculates the sum of all integers from 1 to
n
, where n
is supplied by the user, using a
while
loop.
Exercise 7: Printing a Square of Asterisks
Write a program that prints a square of asterisks of size
n
, where n
is supplied by the user. Use a
for
loop.
Exercise 8: Counting Positive and Negative Numbers
Write a program that prompts the user to enter numbers until 0 is
entered. Count and print how many positive and negative numbers were
entered using a while
loop.
Exercise 9: Print FizzBuzz Numbers
Write a program that prints the numbers 1 through 100. For multiples
of 3, print “Fizz” instead of the number, for multiples of 5 print
“Buzz”, and for multiples of both print “FizzBuzz”. Use a
for
loop.
Exercise 10: Reverse a String
Write a program that takes a string from the user and prints the
string backwards using a for
loop.
Answers
Answer 1: Count from 20 to 1
for i in range(20, 0, -1):
print(i)
Answer 2: Add Multiples of 3
sum = 0
= 1
n while n <= 100:
if n % 3 == 0:
sum += n
+= 1
n print(sum)
Answer 3: Print Numbers from 1 to n
= int(input("Enter a positive integer: "))
n for i in range(1, n + 1):
print(i)
Answer 4: Count Consonants
= input("Enter a string: ")
string = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
consonants = 0
consonant_counter for char in string:
if char in consonants:
+= 1
consonant_counter print(consonant_counter)
Answer 5: Powers of 2
for i in range(11):
print(f"2^{i} = {2 ** i}")
Answer 6: Sum of Numbers Up to n
= int(input("Enter a positive integer: "))
n sum = 0
= 1
counter while counter <= n:
sum += counter
+= 1
counter print(sum)
Answer 7: Print a Square of Asterisks
= int(input("Enter the size of the square: "))
n for i in range(n):
print('*' * n)
Answer 8: Counting Positive and Negative Numbers
= 0
positive = 0
negative = None
number
while number != 0:
= int(input("Enter a number (0 to end): "))
number if number > 0:
+= 1
positive elif number < 0:
+= 1
negative
print(f"Positive numbers: {positive}")
print(f"Negative numbers: {negative}")
Answer 9: Print FizzBuzz Numbers
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
Answer 10: Inverse of a String
= input("Enter a string: ")
string = ""
inverse for char in string:
= char + inverse
inverse print(inverse)
Commenti
Posta un commento