Es. Python tutorial - Strutture di dati
Es. Python tutorial - Strutture di dati
Es. Python tutorial - Strutture di dati
Versione italiana
Esercizi
- Liste
- Crea una lista chiamata
frutti
che contenga i seguenti elementi: “mela”, “banana”, “ciliegia”, “arancia”. - Aggiungi “kiwi” alla lista e rimuovi “banana”.
- Qual è la lunghezza finale della lista?
- Crea una lista chiamata
- Tuple
- Crea una tupla chiamata
dati_personali
che contenga il nome “Marco”, l’età 25 e la città “Roma”. - Scrivi un codice per accedere al secondo elemento della tupla e stampalo.
- Crea una tupla chiamata
- Dizionari
- Crea un dizionario chiamato
studente
con le seguenti chiavi e valori: “nome”: “Luca”, “età”: 20, “corso”: “Informatica”. - Aggiungi una nuova chiave “anno” con valore 2.
- Qual è il valore associato alla chiave “corso”?
- Crea un dizionario chiamato
- Set
- Crea un set chiamato
numeri
che contenga i numeri 1, 2, 3, 4, 5. - Aggiungi il numero 3 al set. Cosa succede?
- Rimuovi il numero 1 e poi verifica se il numero 1 è ancora presente nel set.
- Crea un set chiamato
- Liste di liste
- Crea una lista di liste chiamata
griglia
che rappresenti una griglia 3x3 con i seguenti valori: [1, 2, 3], [4, 5, 6], [7, 8, 9]. - Scrivi un codice per calcolare la somma di tutti gli elementi nella griglia.
- Crea una lista di liste chiamata
- Liste
- Crea una lista chiamata
numeri
che contenga i numeri da 1 a 10. - Usa un ciclo per creare una nuova lista chiamata
numeri_pari
che contenga solo i numeri pari dalla lista originale.
- Crea una lista chiamata
- Tuple
- Crea una tupla chiamata
colore
che contenga i valori RGB per il colore blu: (0, 0, 255). - Scrivi un codice per calcolare la luminosità del colore usando la formula: luminosità = (R * 0.299 + G * 0.587 + B * 0.114).
- Crea una tupla chiamata
- Dizionari
- Crea un dizionario chiamato
libro
con le seguenti chiavi e valori: “titolo”: “Il Gattopardo”, “autore”: “Tomasi di Lampedusa”, “anno”: 1958. - Aggiungi una nuova chiave “genere” con valore “romanzo storico”.
- Qual è l’autore del libro?
- Crea un dizionario chiamato
- Set
- Crea un set chiamato
frutti_set
che contenga i frutti “mela”, “banana”, “ciliegia”, “mela”. - Aggiungi “kiwi” al set e poi crea un altro set chiamato
frutti_esotici
con “mango” e “ananas”. - Trova l’unione dei due set.
- Crea un set chiamato
- Liste di liste
- Crea una lista di liste chiamata
studenti
che rappresenti un elenco di studenti con i seguenti dati: [“Anna”, 22], [“Marco”, 20], [“Luca”, 21]. - Scrivi un codice per trovare l’età media degli studenti.
Risposte con Codice
Liste
= ["mela", "banana", "ciliegia", "arancia"] frutti "kiwi") frutti.append("banana") frutti.remove(= len(frutti) # lunghezza finale è 4 lunghezza_finale
Tuple
= ("Marco", 25, "Roma") dati_personali = dati_personali[1] # secondo elemento è 25 secondo_elemento print(secondo_elemento) # Stampa 25
Dizionari
= {"nome": "Luca", "età": 20, "corso": "Informatica"} studente "anno"] = 2 studente[= studente["corso"] # valore associato è "Informatica" valore_corso
Set
= {1, 2, 3, 4, 5} numeri 3) # Non succede nulla perché 3 è già presente numeri.add(1) numeri.remove(= 1 in numeri # presente sarà False presente
Liste di liste
= [[1, 2, 3], [4, 5, 6], [7, 8, 9]] griglia = sum(sum(riga) for riga in griglia) # somma sarà 45 somma
Liste
= list(range(1, 11)) # Lista da 1 a 10 numeri = [num for num in numeri if num % 2 == 0] # Lista dei numeri pari numeri_pari
Tuple
= (0, 0, 255) # RGB per il blu colore = (colore[0] * 0.299 + colore[1] * 0.587 + colore[2] * 0.114) # Calcolo della luminosità luminosità
Dizionari
= {"titolo": "Il Gattopardo", "autore": "Tomasi di Lampedusa", "anno": 1958} libro "genere"] = "romanzo storico" libro[= libro["autore"] # L'autore è "Tomasi di Lampedusa" autore
Set
= {"mela", "banana", "ciliegia"} # "mela" appare solo una volta frutti_set "kiwi") frutti_set.add(= {"mango", "ananas"} frutti_esotici = frutti_set.union(frutti_esotici) # Unione dei due set unione_frutti
Liste di liste
= [["Anna", 22], ["Marco", 20], ["Luca", 21]]
studenti = sum(studente[1] for studente in studenti) # Somma delle età
età_totale = età_totale / len(studenti) # Calcolo dell'età media età_media
English version
Exercises
- Lists
- Create a list called
fruits
that contains the following elements: “apple”, “banana”, “cherry”, “orange”. - Add “kiwi” to the list and remove “banana”.
- What is the final length of the list?
- Tuples
- Create a tuple called
personal_data
that contains the name “Marco”, the age 25 and the city “Roma”. - Write a code to access the second element of the tuple and print it.
- Dictionaries
- Create a dictionary called
student
with the following keys and values: “name”: “Luca”, “age”: 20, “course”: “Computer Science”. - Add a new key “year” with value 2.
- What is the value associated with the key “course”?
- Sets
- Create a set called
numbers
that contains the numbers 1, 2, 3, 4, 5. - Add the number 3 to the set. What happens?
- Remove the number 1 and then check if the number 1 is still in the set.
- Lists of Lists
- Create a list of lists called
grid
that represents a 3x3 grid with the following values: [1, 2, 3], [4, 5, 6], [7, 8, 9]. - Write a code to calculate the sum of all the elements in the grid.
- Lists
- Create a list called
numbers
that contains the numbers 1 through 10. - Use a loop to create a new list called
even_numbers
that contains only the even numbers from the original list.
- Tuples
- Create a tuple called
color
that contains the RGB values for the color blue: (0, 0, 255). - Write a code to calculate the brightness of the color using the formula: brightness = (R * 0.299 + G * 0.587 + B * 0.114).
- Dictionaries
- Create a dictionary called
book
with the following keys and values: “title”: “The Leopard”, “author”: “Tomasi di Lampedusa”, “year”: 1958. - Add a new key “genre” with value “historical novel”.
- Who is the author of the book?
- Sets
- Create a set called
fruits_set
that contains the fruits “apple”, “banana”, “cherry”, “apple”. - Add “kiwi” to the set and then create another set called
exotic_fruits
with “mango” and “pineapple”. - Find the union of the two sets.
- Lists of lists
- Create a list of lists called
students
that represents a list of students with the following data: [“Anna”, 22], [“Marco”, 20], [“Luca”, 21]. - Write a code to find the average age of the students.
Answers with Code
- Lists
= ["apple", "banana", "cherry", "orange"]
fruits "kiwi")
fruits.append("banana")
fruits.remove(= len(fruits) # final length is 4 final_length
- Tuples
= ("Marco", 25, "Rome")
personal_data = personal_data[1] # second element is 25
second_element print(second_element) # Print 25
- Dictionaries
= {"name": "Luca", "age": 20, "course": "Computer Science"}
student "year"] = 2
student[= student["course"] # associated value is "Computer Science" course_value
- Sets
= {1, 2, 3, 4, 5}
numbers 3) # Nothing happens because 3 is already present
numbers.add(1)
numbers.remove(= 1 in numbers # present will be False present
- Lists of lists
= [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
grid sum = sum(sum(row) for row in grid) # sum will be 45
- Lists
= list(range(1, 11)) # List from 1 to 10
numbers = [num for num in numbers if num % 2 == 0] # List of even numbers even_numbers
- Tuples
= (0, 0, 255) # RGB for the blue
color = (color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114) # Calculate brightness brightness
- Dictionaries
= {"title": "The Leopard", "author": "Tomasi di Lampedusa", "year": 1958}
book "genre"] = "historical novel"
book[= book["author"] # The author is "Tomasi di Lampedusa" author
- Set
= {"apple", "banana", "cherry"} # "apple" appears only once
frutti_set "kiwi")
frutti_set.add(= {"mango", "pineapple"}
frutti_esotici = frutti_set.union(frutti_esotici) # Union of the two sets unione_frutti
- Lists of Lists
= [["Anna", 22], ["Marco", 20], ["Luca", 21]]
students = sum(student[1] for student in students) # Sum of ages
total_age = total_age / len(students) # Calculate average age average_age
Commenti
Posta un commento