Skip to content

Commit db03750

Browse files
committed
version 1.7
1 parent 323a6fe commit db03750

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

03_strutture_dati.ipynb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,45 @@
444444
]
445445
},
446446
{
447+
"cell_type": "markdown",
448+
"metadata": {},
449+
"source": [
450+
"---\n",
451+
"## 7 Comprehensions ed Espressioni Generatrici \ud83c\udf1f\n",
452+
"\n",
453+
"Le *comprehensions* sono una sintassi compatta e leggibile per creare nuove collezioni a partire da iterabili esistenti. Sono molto usate in Python per scrivere codice conciso e veloce.\n",
454+
"\n",
455+
"### List Comprehension\n",
456+
"```python\n",
457+
"# Create a list of squares of the numbers from 0 to 9\n",
458+
"squares = [x**2 for x in range(10)]\n",
459+
"print(squares)\n",
460+
"```\n",
461+
"\n",
462+
"### Dict Comprehension\n",
463+
"```python\n",
464+
"# Create a dictionary with number:square\n",
465+
"squares_dict = {x: x**2 for x in range(5)}\n",
466+
"print(squares_dict)\n",
467+
"```\n",
468+
"\n",
469+
"### Set Comprehension\n",
470+
"```python\n",
471+
"# Create a set containing the cubes of the numbers\n",
472+
"cubes_set = {x**3 for x in range(5)}\n",
473+
"print(cubes_set)\n",
474+
"```\n",
475+
"\n",
476+
"### Generator Expression\n",
477+
"Simile a una list comprehension, ma produce un generatore, ossia gli elementi vengono calcolati su richiesta.\n",
478+
"```python\n",
479+
"gen = (x**2 for x in range(5))\n",
480+
"for value in gen:\n",
481+
" print(value)\n",
482+
"```"
483+
]
484+
},
485+
{
447486
"cell_type": "markdown",
448487
"id": "9197d902",
449488
"metadata": {},
@@ -536,6 +575,38 @@
536575
]
537576
},
538577
{
578+
"cell_type": "markdown",
579+
"metadata": {},
580+
"source": [
581+
"### Esercizio 10: List comprehension\n",
582+
"Crea una lista con i quadrati dei numeri da 1 a 10 usando una *list comprehension*."
583+
]
584+
},
585+
{
586+
"cell_type": "markdown",
587+
"metadata": {},
588+
"source": [
589+
"### Esercizio 11: Dict comprehension\n",
590+
"Crea un dizionario che mappa le lettere di una stringa alla loro lunghezza, usando una *dict comprehension*. Usa la stringa `words = ['python', 'ai', 'data']`."
591+
]
592+
},
593+
{
594+
"cell_type": "markdown",
595+
"metadata": {},
596+
"source": [
597+
"### Esercizio 12: Set comprehension\n",
598+
"Crea un set contenente le prime lettere di ciascuna parola in una lista, usando una *set comprehension*."
599+
]
600+
},
601+
{
602+
"cell_type": "markdown",
603+
"metadata": {},
604+
"source": [
605+
"### Esercizio 13: Generator expression\n",
606+
"Usa una *generator expression* per calcolare il cubo dei numeri da 0 a 4 e stampare i risultati in un ciclo `for`."
607+
]
608+
},
609+
{
539610
"cell_type": "markdown",
540611
"id": "8b2811d0",
541612
"metadata": {},
@@ -739,6 +810,77 @@
739810
"save_user(**credentials)"
740811
]
741812
},
813+
{
814+
"cell_type": "markdown",
815+
"metadata": {},
816+
"source": [
817+
"### Soluzione Esercizio 10: List comprehension"
818+
]
819+
},
820+
{
821+
"cell_type": "code",
822+
"execution_count": null,
823+
"metadata": {},
824+
"outputs": [],
825+
"source": [
826+
"squares = [x**2 for x in range(1, 11)]\n",
827+
"print(squares)"
828+
]
829+
},
830+
{
831+
"cell_type": "markdown",
832+
"metadata": {},
833+
"source": [
834+
"### Soluzione Esercizio 11: Dict comprehension"
835+
]
836+
},
837+
{
838+
"cell_type": "code",
839+
"execution_count": null,
840+
"metadata": {},
841+
"outputs": [],
842+
"source": [
843+
"words = ['python', 'ai', 'data']\n",
844+
"lengths = {word: len(word) for word in words}\n",
845+
"print(lengths)"
846+
]
847+
},
848+
{
849+
"cell_type": "markdown",
850+
"metadata": {},
851+
"source": [
852+
"### Soluzione Esercizio 12: Set comprehension"
853+
]
854+
},
855+
{
856+
"cell_type": "code",
857+
"execution_count": null,
858+
"metadata": {},
859+
"outputs": [],
860+
"source": [
861+
"words = ['python', 'ai', 'data']\n",
862+
"first_letters = {word[0] for word in words}\n",
863+
"print(first_letters)"
864+
]
865+
},
866+
{
867+
"cell_type": "markdown",
868+
"metadata": {},
869+
"source": [
870+
"### Soluzione Esercizio 13: Generator expression"
871+
]
872+
},
873+
{
874+
"cell_type": "code",
875+
"execution_count": null,
876+
"metadata": {},
877+
"outputs": [],
878+
"source": [
879+
"gen = (x**3 for x in range(5))\n",
880+
"for value in gen:\n",
881+
" print(value)"
882+
]
883+
},
742884
{
743885
"cell_type": "markdown",
744886
"id": "footer",

0 commit comments

Comments
 (0)