Es. Python su numeri, stringhe, operatori aritmetici, operatori di confronto, input e output
Versione italiana
Esercizio 1: Somma di Due Numeri
Chiedi all’utente di inserire due numeri e stampa la loro somma.
Esercizio 2: Verifica Parità
Chiedi all’utente di inserire un numero e stampa se è pari o dispari.
Esercizio 3: Lunghezza di una Stringa
Chiedi all’utente di inserire una stringa e stampa la sua lunghezza.
Esercizio 4: Calcolo dell’Area di un Cerchio
Chiedi all’utente di inserire il raggio di un cerchio e calcola l’area.
Esercizio 5: Concatenazione di Stringhe
Chiedi all’utente di inserire due stringhe e stampa la loro concatenazione.
Esercizio 6: Media di Tre Numeri
Chiedi all’utente di inserire tre numeri e calcola la loro media.
Esercizio 7: Conversione di Gradi
Chiedi all’utente di inserire una temperatura in gradi Celsius e converti in gradi Fahrenheit.
Esercizio 8: Controllo di Maggiore Età
Chiedi all’utente di inserire la propria età e stampa un messaggio che indica se è maggiorenne o meno.
Esercizio 9: Calcolo della Potenza
Chiedi all’utente di inserire un numero e un esponente e calcola la potenza.
Soluzioni
Soluzione 1
= float(input("Inserisci il primo numero: "))
num1 = float(input("Inserisci il secondo numero: "))
num2 = num1 + num2
somma print(f"La somma è: {somma}")
Soluzione 2
= int(input("Inserisci un numero: "))
numero = "pari" if numero % 2 == 0 else "dispari"
parita print(f"{numero} è {parita}.")
Soluzione 3
= input("Inserisci una stringa: ")
stringa = len(stringa)
lunghezza print(f"La lunghezza della stringa è: {lunghezza}")
Soluzione 4
import math
= float(input("Inserisci il raggio del cerchio: "))
raggio = math.pi * (raggio ** 2)
area print(f"L'area del cerchio è: {area}")
Soluzione 5
= input("Inserisci la prima stringa: ")
stringa1 = input("Inserisci la seconda stringa: ")
stringa2 = stringa1 + stringa2
concatenazione print(f"La concatenazione delle stringhe è: {concatenazione}")
Soluzione 6
= float(input("Inserisci il primo numero: "))
num1 = float(input("Inserisci il secondo numero: "))
num2 = float(input("Inserisci il terzo numero: "))
num3 = (num1 + num2 + num3) / 3
media print(f"La media è: {media}")
Soluzione 7
= float(input("Inserisci la temperatura in gradi Celsius: "))
celsius = (celsius * 9/5) + 32
fahrenheit print(f"La temperatura in gradi Fahrenheit è: {fahrenheit}")
Soluzione 8
= int(input("Inserisci la tua età: "))
eta = "Sei maggiorenne." * (eta >= 18) + "Sei minorenne." * (eta < 18)
maggiore_eta print(maggiore_eta)
Soluzione 9
= float(input("Inserisci un numero (base): "))
base = int(input("Inserisci un esponente: "))
esponente = base ** esponente
potenza print(f"{base} elevato a {esponente} è: {potenza}")
English version
Python exercises on numbers, strings, arithmetic operators, comparison operators, input and output
Exercise 1: Add Two Numbers
Ask the user to enter two numbers and print their sum.
Exercise 2: Check Parity
Ask the user to enter a number and print whether it is even or odd.
Exercise 3: Length of a String
Ask the user to enter a string and print its length.
Exercise 4: Calculating the Area of a Circle
Ask the user to enter the radius of a circle and calculate the area.
Exercise 5: Concatenation of Strings
Ask the user to enter two strings and print their concatenation.
Exercise 6: Average of Three Numbers
Ask the user to enter three numbers and calculate their average.
Exercise 7: Converting Degrees
Ask the user to enter a temperature in Celsius and convert to Fahrenheit.
Exercise 8: Age Check
Ask the user to enter their age and print a message indicating whether they are of age or not.
Exercise 9: Calculating Power
Ask the user to enter a number and an exponent and calculate the power.
Solutions
Solution 1
= float(input("Enter the first number: "))
num1 = float(input("Enter the second number: "))
num2 sum = num1 + num2
print(f"The sum is: {sum}")
Solution 2
= int(input("Enter a number: "))
number = "even" if number % 2 == 0 else "odd"
even print(f"{number} is {even}.")
Solution 3
= input("Enter a string: ")
string = len(string)
length print(f"The length of the string is: {length}")
Solution 4
import math
= float(input("Enter the radius of the circle: "))
radius = math.pi * (radius ** 2)
area print(f"The area of the circle is: {area}")
Solution 5
= input("Enter the first string: ")
string1 = input("Enter the second string: ")
string2 = string1 + string2
concatenation print(f"The concatenation of the strings is: {concatenation}")
Solution 6
= float(input("Enter the first number: "))
num1 = float(input("Enter the second number: "))
num2 = float(input("Enter the third number: "))
num3 = (num1 + num2 + num3) / 3
average print(f"The average is: {average}")
Solution 7
= float(input("Enter the temperature in Celsius: "))
celsius = (celsius * 9/5) + 32
fahrenheit print(f"The temperature in Fahrenheit is: {fahrenheit}")
Solution 8
= int(input("Enter your age: "))
age = "You are of legal age." * (age >= 18) + "You are a minor." * (age < 18)
age_age print(greater_age)
Solution 9
= float(input("Enter a number (base): "))
base = int(input("Enter an exponent: "))
exponent = base ** exponent
power print(f"{base} raised to {exponent} is: {power}")
Commenti
Posta un commento