Es. Python tutorial - Cicli 2

Es. Python tutorial - Cicli 2 Es. Python tutorial - Cicli 2

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

somma = 0
n = 1
while n <= 100:
    if n % 3 == 0:
        somma += n
    n += 1
print(somma)

Risposta 3: Stampa i Numeri da 1 a n

n = int(input("Inserisci un numero intero positivo: "))
for i in range(1, n + 1):
    print(i)

Risposta 4: Contare le Consonanti

stringa = input("Inserisci una stringa: ")
consonanti = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
contatore_consonanti = 0
for char in stringa:
    if char in consonanti:
        contatore_consonanti += 1
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

n = int(input("Inserisci un numero intero positivo: "))
somma = 0
contatore = 1
while contatore <= n:
    somma += contatore
    contatore += 1
print(somma)

Risposta 7: Stampa un Quadrato di Asterischi

n = int(input("Inserisci la dimensione del quadrato: "))
for i in range(n):
    print('*' * n)

Risposta 8: Contare i Numeri Positivi e Negativi

positivi = 0
negativi = 0
numero = None

while numero != 0:
    numero = int(input("Inserisci un numero (0 per terminare): "))
    if numero > 0:
        positivi += 1
    elif numero < 0:
        negativi += 1

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

stringa = input("Inserisci una stringa: ")
inverso = ""
for char in stringa:
    inverso = char + 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
n = 1
while n <= 100:
if n % 3 == 0:
sum += n
n += 1
print(sum)

Answer 3: Print Numbers from 1 to n

n = int(input("Enter a positive integer: "))
for i in range(1, n + 1):
print(i)

Answer 4: Count Consonants

string = input("Enter a string: ")
consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ"
consonant_counter = 0
for char in string:
if char in consonants:
consonant_counter += 1
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

n = int(input("Enter a positive integer: "))
sum = 0
counter = 1
while counter <= n:
sum += counter
counter += 1
print(sum)

Answer 7: Print a Square of Asterisks

n = int(input("Enter the size of the square: "))
for i in range(n):
print('*' * n)

Answer 8: Counting Positive and Negative Numbers

positive = 0
negative = 0
number = None

while number != 0:
number = int(input("Enter a number (0 to end): "))
if number > 0:
positive += 1
elif number < 0:
negative += 1

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

string = input("Enter a string: ")
inverse = ""
for char in string:
inverse = char + inverse
print(inverse)

Commenti