Es. Python tutorial - Strutture di dati

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

Es. Python tutorial - Strutture di dati

Versione italiana

Esercizi

  1. 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?
  2. 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.
  3. 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”?
  4. 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.
  5. 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.
  6. 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.
  7. 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).
  8. 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?
  9. 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.
  10. 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

  1. Liste

    frutti = ["mela", "banana", "ciliegia", "arancia"]
    frutti.append("kiwi")
    frutti.remove("banana")
    lunghezza_finale = len(frutti)  # lunghezza finale è 4
  2. Tuple

    dati_personali = ("Marco", 25, "Roma")
    secondo_elemento = dati_personali[1]  # secondo elemento è 25
    print(secondo_elemento)  # Stampa 25
  3. Dizionari

    studente = {"nome": "Luca", "età": 20, "corso": "Informatica"}
    studente["anno"] = 2
    valore_corso = studente["corso"]  # valore associato è "Informatica"
  4. 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à False
  5. Liste di liste

    griglia = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    somma = sum(sum(riga) for riga in griglia)  # somma sarà 45
  6. Liste

    numeri = list(range(1, 11))  # Lista da 1 a 10
    numeri_pari = [num for num in numeri if num % 2 == 0]  # Lista dei numeri pari
  7. Tuple

    colore = (0, 0, 255)  # RGB per il blu
    luminosità = (colore[0] * 0.299 + colore[1] * 0.587 + colore[2] * 0.114)  # Calcolo della luminosità
  8. Dizionari

    libro = {"titolo": "Il Gattopardo", "autore": "Tomasi di Lampedusa", "anno": 1958}
    libro["genere"] = "romanzo storico"
    autore = libro["autore"]  # L'autore è "Tomasi di Lampedusa"
  9. 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 set
  10. Liste 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à media

English version

Exercises

  1. 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?
  1. 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.
  1. 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”?
  1. 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.
  1. 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.
  1. 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.
  1. 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).
  1. 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?
  1. 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.
  1. 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

  1. Lists
fruits = ["apple", "banana", "cherry", "orange"]
fruits.append("kiwi")
fruits.remove("banana")
final_length = len(fruits) # final length is 4
  1. Tuples
personal_data = ("Marco", 25, "Rome")
second_element = personal_data[1] # second element is 25
print(second_element) # Print 25
  1. Dictionaries
student = {"name": "Luca", "age": 20, "course": "Computer Science"}
student["year"] = 2
course_value = student["course"] # associated value is "Computer Science"
  1. 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
  1. 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
  1. 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
  1. Tuples
color = (0, 0, 255) # RGB for the blue
brightness = (color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114) # Calculate brightness
  1. Dictionaries
book = {"title": "The Leopard", "author": "Tomasi di Lampedusa", "year": 1958}
book["genre"] = "historical novel"
author = book["author"] # The author is "Tomasi di Lampedusa"
  1. 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
  1. 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