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
fruttiche 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_personaliche 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
studentecon 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
numeriche 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
grigliache 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
numeriche contenga i numeri da 1 a 10. - Usa un ciclo per creare una nuova lista chiamata
numeri_pariche contenga solo i numeri pari dalla lista originale.
- Crea una lista chiamata
- Tuple
- Crea una tupla chiamata
coloreche 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
librocon 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_setche contenga i frutti “mela”, “banana”, “ciliegia”, “mela”. - Aggiungi “kiwi” al set e poi crea un altro set chiamato
frutti_esoticicon “mango” e “ananas”. - Trova l’unione dei due set.
- Crea un set chiamato
- Liste di liste
- Crea una lista di liste chiamata
studentiche 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
frutti = ["mela", "banana", "ciliegia", "arancia"] frutti.append("kiwi") frutti.remove("banana") lunghezza_finale = len(frutti) # lunghezza finale è 4Tuple
dati_personali = ("Marco", 25, "Roma") secondo_elemento = dati_personali[1] # secondo elemento è 25 print(secondo_elemento) # Stampa 25Dizionari
studente = {"nome": "Luca", "età": 20, "corso": "Informatica"} studente["anno"] = 2 valore_corso = studente["corso"] # valore associato è "Informatica"Set
numeri = {1, 2, 3, 4, 5} numeri.add(3) # Non succede nulla perché 3 è già presente numeri.remove(1) presente = 1 in numeri # presente sarà FalseListe di liste
griglia = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] somma = sum(sum(riga) for riga in griglia) # somma sarà 45Liste
numeri = list(range(1, 11)) # Lista da 1 a 10 numeri_pari = [num for num in numeri if num % 2 == 0] # Lista dei numeri pariTuple
colore = (0, 0, 255) # RGB per il blu luminosità = (colore[0] * 0.299 + colore[1] * 0.587 + colore[2] * 0.114) # Calcolo della luminositàDizionari
libro = {"titolo": "Il Gattopardo", "autore": "Tomasi di Lampedusa", "anno": 1958} libro["genere"] = "romanzo storico" autore = libro["autore"] # L'autore è "Tomasi di Lampedusa"Set
frutti_set = {"mela", "banana", "ciliegia"} # "mela" appare solo una volta frutti_set.add("kiwi") frutti_esotici = {"mango", "ananas"} unione_frutti = frutti_set.union(frutti_esotici) # Unione dei due setListe di liste
studenti = [["Anna", 22], ["Marco", 20], ["Luca", 21]]
età_totale = sum(studente[1] for studente in studenti) # Somma delle età
età_media = età_totale / len(studenti) # Calcolo dell'età mediaEnglish version
Exercises
- Lists
- Create a list called
fruitsthat 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_datathat 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
studentwith 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
numbersthat 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
gridthat 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
numbersthat contains the numbers 1 through 10. - Use a loop to create a new list called
even_numbersthat contains only the even numbers from the original list.
- Tuples
- Create a tuple called
colorthat 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
bookwith 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_setthat contains the fruits “apple”, “banana”, “cherry”, “apple”. - Add “kiwi” to the set and then create another set called
exotic_fruitswith “mango” and “pineapple”. - Find the union of the two sets.
- Lists of lists
- Create a list of lists called
studentsthat 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
fruits = ["apple", "banana", "cherry", "orange"]
fruits.append("kiwi")
fruits.remove("banana")
final_length = len(fruits) # final length is 4- Tuples
personal_data = ("Marco", 25, "Rome")
second_element = personal_data[1] # second element is 25
print(second_element) # Print 25- Dictionaries
student = {"name": "Luca", "age": 20, "course": "Computer Science"}
student["year"] = 2
course_value = student["course"] # associated value is "Computer Science"- Sets
numbers = {1, 2, 3, 4, 5}
numbers.add(3) # Nothing happens because 3 is already present
numbers.remove(1)
present = 1 in numbers # present will be False- Lists of lists
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
sum = sum(sum(row) for row in grid) # sum will be 45- Lists
numbers = list(range(1, 11)) # List from 1 to 10
even_numbers = [num for num in numbers if num % 2 == 0] # List of even numbers- Tuples
color = (0, 0, 255) # RGB for the blue
brightness = (color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114) # Calculate brightness- Dictionaries
book = {"title": "The Leopard", "author": "Tomasi di Lampedusa", "year": 1958}
book["genre"] = "historical novel"
author = book["author"] # The author is "Tomasi di Lampedusa"- Set
frutti_set = {"apple", "banana", "cherry"} # "apple" appears only once
frutti_set.add("kiwi")
frutti_esotici = {"mango", "pineapple"}
unione_frutti = frutti_set.union(frutti_esotici) # Union of the two sets- Lists of Lists
students = [["Anna", 22], ["Marco", 20], ["Luca", 21]]
total_age = sum(student[1] for student in students) # Sum of ages
average_age = total_age / len(students) # Calculate average age
Commenti
Posta un commento