Es. Python tutorial - Strutture di dati 2

Es. Python tutorial - Strutture di dati 2 Es. Python tutorial - Strutture di dati 2

Es. Python tutorial - Strutture di dati 2

Versione italiana

Esercizi

  1. Liste
    • Crea una lista chiamata animali con i seguenti elementi: “cane”, “gatto”, “coniglio”, “tartaruga”.
    • Ordina la lista in ordine alfabetico e stampa il risultato.
  2. Tuple
    • Crea una tupla chiamata dimensioni che contenga la lunghezza, la larghezza e l’altezza di un oggetto: (10, 5, 2).
    • Calcola il volume dell’oggetto usando la formula: volume = lunghezza * larghezza * altezza.
  3. Dizionari
    • Crea un dizionario chiamato auto con le seguenti chiavi e valori: “marca”: “Fiat”, “modello”: “Panda”, “anno”: 2020.
    • Cambia l’anno dell’auto in 2021 e stampa il dizionario aggiornato.
  4. Set
    • Crea un set chiamato numeri_primi con i numeri primi da 1 a 20.
    • Verifica se il numero 11 è presente nel set.
  5. Liste di liste
    • Crea una lista di liste chiamata classi che rappresenti due classi di studenti: [“Classe A”, [“Anna”, “Marco”]], [“Classe B”, [“Luca”, “Sara”]].
    • Stampa il nome del primo studente della Classe A.
  6. Liste
    • Crea una lista chiamata parole con le parole “casa”, “auto”, “albero”, “mare”.
    • Usa un ciclo per creare una nuova lista chiamata lunghezze che contenga la lunghezza di ogni parola.
  7. Tuple
    • Crea una tupla chiamata data che rappresenti una data nel formato (giorno, mese, anno): (15, 8, 2023).
    • Scrivi un codice per stampare la data in formato “15/08/2023”.
  8. Dizionari
    • Crea un dizionario chiamato frutta con i seguenti elementi: “mela”: 3, “banana”: 2, “arancia”: 5.
    • Aggiungi una chiave “kiwi” con valore 4 e stampa il totale della frutta.
  9. Set
    • Crea un set chiamato colori con i colori “rosso”, “verde”, “blu”.
    • Rimuovi “verde” dal set e stampa il set risultante.
  10. Liste di liste
    • Crea una lista di liste chiamata voti che rappresenti i voti di tre studenti: [“Luca”, 28], [“Marco”, 30], [“Anna”, 25].
    • Calcola e stampa il voto medio degli studenti.

Risposte

  1. Liste

    animali = ["cane", "gatto", "coniglio", "tartaruga"]
    animali.sort()  # Ordina la lista in ordine alfabetico
  2. Tuple

    dimensioni = (10, 5, 2)
    volume = dimensioni[0] * dimensioni[1] * dimensioni[2]  # Calcolo del volume
  3. Dizionari

    auto = {"marca": "Fiat", "modello": "Panda", "anno": 2020}
    auto["anno"] = 2021  # Cambia l'anno
  4. Set

    numeri_primi = {2, 3, 5, 7, 11, 13, 17, 19}
    presente = 11 in numeri_primi  # Verifica se 11 è presente
  5. Liste di liste

    classi = [["Classe A", ["Anna", "Marco"]], ["Classe B", ["Luca", "Sara"]]]
    primo_studente_classe_a = classi[0][1][0]  # Nome del primo studente della Classe A
  6. Liste

    parole = ["casa", "auto", "albero", "mare"]
    lunghezze = [len(parola) for parola in parole]  # Lista delle lunghezze delle parole
  7. Tuple

    data = (15, 8, 2023)
    data_formattata = f"{data[0]:02}/{data[1]:02}/{data[2]}"  # Formato "15/08/2023"
  8. Dizionari

    frutta = {"mela": 3, "banana": 2, "arancia": 5}
    frutta["kiwi"] = 4  # Aggiungi kiwi
    totale_frutta = sum(frutta.values())  # Calcola il totale della frutta
  9. Set

    colori = {"rosso", "verde", "blu"}
    colori.remove("verde")  # Rimuovi verde
  10. Liste di liste python voti = [["Luca", 28], ["Marco", 30], ["Anna", 25]] somma_voti = sum(voto[1] for voto in voti) # Somma dei voti voto_medio = somma_voti / len(voti) # Calcolo del voto medio

English version

Exercises

  1. Lists
  • Create a list called animals with the following elements: “dog”, “cat”, “rabbit”, “turtle”.
  • Sort the list alphabetically and print the result.
  1. Tuples
  • Create a tuple called dimensions that contains the length, width and height of an object: (10, 5, 2).
  • Calculate the volume of the object using the formula: volume = length * width * height.
  1. Dictionaries
  • Create a dictionary called cars with the following keys and values: “brand”: “Fiat”, “model”: “Panda”, “year”: 2020.
  • Change the year of the car to 2021 and print the updated dictionary.
  1. Sets
  • Create a set called primes with the prime numbers from 1 to 20.
  • Check if the number 11 is present in the set.
  1. Lists of Lists
  • Create a list of lists called classes that represent two classes of students: [“Class A”, [“Anna”, “Marco”]], [“Class B”, [“Luca”, “Sara”]].
  • Print the name of the first student in Class A.
  1. Lists
  • Create a list called words with the words “house”, “car”, “tree”, “sea”.
  • Use a loop to create a new list called lengths that contains the length of each word.
  1. Tuples
  • Create a tuple called date that represents a date in the format (day, month, year): (15, 8, 2023).
  • Write a code to print the date in the format “15/08/2023”.
  1. Dictionaries
  • Create a dictionary called fruit with the following elements: “apple”: 3, “banana”: 2, “orange”: 5.
  • Add a key “kiwi” with value 4 and print the total of the fruit.
  1. Sets
  • Create a set called colors with the colors “red”, “green”, “blue”.
  • Remove “green” from the set and print the resulting set.
  1. Lists of lists
  • Create a list of lists called grades that represent the grades of three students: [“Luca”, 28], [“Marco”, 30], [“Anna”, 25].
  • Calculate and print the average grade of the students.

Answers

  1. Lists
animals = ["dog", "cat", "rabbit", "turtle"]
animals.sort() # Sort the list alphabetically
  1. Tuples
dimensions = (10, 5, 2)
volume = dimensions[0] * dimensions[1] * dimensions[2] # Calculate the volume
  1. Dictionaries
cars = {"brand": "Fiat", "model": "Panda", "year": 2020}
cars["year"] = 2021 # Change the year
  1. Sets
primes = {2, 3, 5, 7, 11, 13, 17, 19}
present = 11 in primes # Check if 11 is present
  1. Lists of lists
classes = [["Class A", ["Anna", "Marco"]], ["Class B", ["Luca", "Sara"]]]
first_student_class_a = classes[0][1][0] # Name of the first student in Class A
  1. Lists
words = ["house", "car", "tree", "sea"]
lengths = [len(word) for word in words] # List of word lengths
  1. Tuples
data = (15, 8, 2023)
formatted_date = f"{data[0]:02}/{data[1]:02}/{data[2]}" # Format "15/08/2023"
  1. Dictionaries
fruit = {"apple": 3, "banana": 2, "orange": 5}
fruit["kiwi"] = 4 # Add kiwi
total_fruit = sum(fruit.values()) # Calculate the total of the fruit
  1. Set
colors = {"red", "green", "blue"}
colors.remove("green") # Remove green
  1. Lists of lists
votes = [["Luca", 28], ["Marco", 30], ["Anna", 25]]
sum_votes = sum(vote[1] for vote in votes) # Sum of votes
average_vote = sum_votes / len(votes) # Calculate the average vote

Commenti