Es. Python su numeri, stringhe, operatori aritmetici, operatori di confronto, input e output

Es. Python su numeri, stringhe, operatori aritmetici, operatori di confronto, input e output Es. Python su numeri, stringhe, operatori aritmetici, operatori di confronto, input e output

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

num1 = float(input("Inserisci il primo numero: "))
num2 = float(input("Inserisci il secondo numero: "))
somma = num1 + num2
print(f"La somma è: {somma}")

Soluzione 2

numero = int(input("Inserisci un numero: "))
parita = "pari" if numero % 2 == 0 else "dispari"
print(f"{numero} è {parita}.")

Soluzione 3

stringa = input("Inserisci una stringa: ")
lunghezza = len(stringa)
print(f"La lunghezza della stringa è: {lunghezza}")

Soluzione 4

import math

raggio = float(input("Inserisci il raggio del cerchio: "))
area = math.pi * (raggio ** 2)
print(f"L'area del cerchio è: {area}")

Soluzione 5

stringa1 = input("Inserisci la prima stringa: ")
stringa2 = input("Inserisci la seconda stringa: ")
concatenazione = stringa1 + stringa2
print(f"La concatenazione delle stringhe è: {concatenazione}")

Soluzione 6

num1 = float(input("Inserisci il primo numero: "))
num2 = float(input("Inserisci il secondo numero: "))
num3 = float(input("Inserisci il terzo numero: "))
media = (num1 + num2 + num3) / 3
print(f"La media è: {media}")

Soluzione 7

celsius = float(input("Inserisci la temperatura in gradi Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"La temperatura in gradi Fahrenheit è: {fahrenheit}")

Soluzione 8

eta = int(input("Inserisci la tua età: "))
maggiore_eta = "Sei maggiorenne." * (eta >= 18) + "Sei minorenne." * (eta < 18)
print(maggiore_eta)

Soluzione 9

base = float(input("Inserisci un numero (base): "))
esponente = int(input("Inserisci un esponente: "))
potenza = base ** esponente
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

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print(f"The sum is: {sum}")

Solution 2

number = int(input("Enter a number: "))
even = "even" if number % 2 == 0 else "odd"
print(f"{number} is {even}.")

Solution 3

string = input("Enter a string: ")
length = len(string)
print(f"The length of the string is: {length}")

Solution 4

import math

radius = float(input("Enter the radius of the circle: "))
area = math.pi * (radius ** 2)
print(f"The area of ​​the circle is: {area}")

Solution 5

string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
concatenation = string1 + string2
print(f"The concatenation of the strings is: {concatenation}")

Solution 6

num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
average = (num1 + num2 + num3) / 3
print(f"The average is: {average}")

Solution 7

celsius = float(input("Enter the temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"The temperature in Fahrenheit is: {fahrenheit}")

Solution 8

age = int(input("Enter your age: "))
age_age = "You are of legal age." * (age >= 18) + "You are a minor." * (age < 18)
print(greater_age)

Solution 9

base = float(input("Enter a number (base): "))
exponent = int(input("Enter an exponent: "))
power = base ** exponent
print(f"{base} raised to {exponent} is: {power}")

Commenti