Es. Python tutorial - Strutture di dati 2
Es. Python tutorial - Strutture di dati 2
Es. Python tutorial - Strutture di dati 2
Versione italiana
Esercizi
- Liste
- Crea una lista chiamata
animali
con i seguenti elementi: “cane”, “gatto”, “coniglio”, “tartaruga”. - Ordina la lista in ordine alfabetico e stampa il risultato.
- Crea una lista chiamata
- 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.
- Crea una tupla chiamata
- 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.
- Crea un dizionario chiamato
- Set
- Crea un set chiamato
numeri_primi
con i numeri primi da 1 a 20. - Verifica se il numero 11 è presente nel set.
- Crea un set chiamato
- 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.
- Crea una lista di liste chiamata
- 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.
- Crea una lista chiamata
- 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”.
- Crea una tupla chiamata
- 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.
- Crea un dizionario chiamato
- Set
- Crea un set chiamato
colori
con i colori “rosso”, “verde”, “blu”. - Rimuovi “verde” dal set e stampa il set risultante.
- Crea un set chiamato
- 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.
- Crea una lista di liste chiamata
Risposte
Liste
= ["cane", "gatto", "coniglio", "tartaruga"] animali # Ordina la lista in ordine alfabetico animali.sort()
Tuple
= (10, 5, 2) dimensioni = dimensioni[0] * dimensioni[1] * dimensioni[2] # Calcolo del volume volume
Dizionari
= {"marca": "Fiat", "modello": "Panda", "anno": 2020} auto "anno"] = 2021 # Cambia l'anno auto[
Set
= {2, 3, 5, 7, 11, 13, 17, 19} numeri_primi = 11 in numeri_primi # Verifica se 11 è presente presente
Liste di liste
= [["Classe A", ["Anna", "Marco"]], ["Classe B", ["Luca", "Sara"]]] classi = classi[0][1][0] # Nome del primo studente della Classe A primo_studente_classe_a
Liste
= ["casa", "auto", "albero", "mare"] parole = [len(parola) for parola in parole] # Lista delle lunghezze delle parole lunghezze
Tuple
= (15, 8, 2023) data = f"{data[0]:02}/{data[1]:02}/{data[2]}" # Formato "15/08/2023" data_formattata
Dizionari
= {"mela": 3, "banana": 2, "arancia": 5} frutta "kiwi"] = 4 # Aggiungi kiwi frutta[= sum(frutta.values()) # Calcola il totale della frutta totale_frutta
Set
= {"rosso", "verde", "blu"} colori "verde") # Rimuovi verde colori.remove(
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
- Lists
- Create a list called
animals
with the following elements: “dog”, “cat”, “rabbit”, “turtle”. - Sort the list alphabetically and print the result.
- 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.
- 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.
- Sets
- Create a set called
primes
with the prime numbers from 1 to 20. - Check if the number 11 is present in the set.
- 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.
- 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.
- 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”.
- 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.
- Sets
- Create a set called
colors
with the colors “red”, “green”, “blue”. - Remove “green” from the set and print the resulting set.
- 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
- Lists
= ["dog", "cat", "rabbit", "turtle"]
animals # Sort the list alphabetically animals.sort()
- Tuples
= (10, 5, 2)
dimensions = dimensions[0] * dimensions[1] * dimensions[2] # Calculate the volume volume
- Dictionaries
= {"brand": "Fiat", "model": "Panda", "year": 2020}
cars "year"] = 2021 # Change the year cars[
- Sets
= {2, 3, 5, 7, 11, 13, 17, 19}
primes = 11 in primes # Check if 11 is present present
- Lists of lists
= [["Class A", ["Anna", "Marco"]], ["Class B", ["Luca", "Sara"]]]
classes = classes[0][1][0] # Name of the first student in Class A first_student_class_a
- Lists
= ["house", "car", "tree", "sea"]
words = [len(word) for word in words] # List of word lengths lengths
- Tuples
= (15, 8, 2023)
data = f"{data[0]:02}/{data[1]:02}/{data[2]}" # Format "15/08/2023" formatted_date
- Dictionaries
= {"apple": 3, "banana": 2, "orange": 5}
fruit "kiwi"] = 4 # Add kiwi
fruit[= sum(fruit.values()) # Calculate the total of the fruit total_fruit
- Set
= {"red", "green", "blue"}
colors "green") # Remove green colors.remove(
- Lists of lists
= [["Luca", 28], ["Marco", 30], ["Anna", 25]]
votes = sum(vote[1] for vote in votes) # Sum of votes
sum_votes = sum_votes / len(votes) # Calculate the average vote average_vote
Commenti
Posta un commento