Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Andrew
intro
Commits
93af30a0
Commit
93af30a0
authored
May 07, 2017
by
Tancredi Orlando
Browse files
Various fixes.
parent
1eedd1e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
day_1.md
View file @
93af30a0
...
...
@@ -63,10 +63,6 @@ print('Hello, world!')
-
Noi useremo Python 3
-
Fatelo anche voi
Notes:
-
Ricordatevi il nome di Guido
-
Aggiungere statistiche sullo sviluppo di python
----
## Interpretato vs. compilato
...
...
@@ -76,7 +72,9 @@ Notes:
-
Python è interpretato (circa)
Notes:
-
Parlare della VM Python
Python è compilato in quanto la sintassi Python è compilata in bytecode, ed
è interpretato in quanto il bytecode è eseguito da un altro programma, la VM
Python e non eseguito direttamente dal processore.
----
...
...
@@ -113,8 +111,9 @@ Hello world!
Per uscire:
`quit()`
Notes:
-
Differenza tra interprete (il software che converte le istruzioni in bytecode
e poi esegue quest'ultimo) e REPL (il software che si interfaccia con l'utente)
Mostrare la differenza tra interprete (il software che converte le istruzioni
in bytecode e poi esegue quest'ultimo) e l'interfaccia REPL Python (il
software che si interfaccia con l'utente).
----
...
...
@@ -123,10 +122,14 @@ e poi esegue quest'ultimo) e REPL (il software che si interfaccia con l'utente)
```
>>> 2 + 2
4
```
```
>>> 3 * 3
9
```
```
>>> 4 < 5
True
```
...
...
@@ -135,7 +138,7 @@ True
## Commenti
-
Tutto ciò posto dopo il cancelletto (
`#`
) non è interpretato
Tutto ciò posto dopo il cancelletto (
`#`
) non è interpretato
.
```
>>> 1 + 1 # qui posso scrivere quello che voglio
...
...
@@ -161,19 +164,26 @@ Il segreto degli hacker? Leggere le scritte!
## Variabili
```
python
```
>>> x = 6
>>> y = 2
>>> x
6
>>> y
2
```
>>>
2
*
x
12
>>>
y
=
x
+
4
```
>>> 2 * y
4
>>> x + y
8
```
>>>
y
10
```
>>> z = x + y
>>> z
8
```
---
...
...
@@ -187,7 +197,9 @@ Il segreto degli hacker? Leggere le scritte!
```
>>> type(3)
<class 'int'>
```
```
>>> type(3.14)
<class 'float'>
```
...
...
@@ -213,10 +225,14 @@ Cifre con punto
```
>>> 3.14 + 1.234
4.3740000000000006
```
```
>>> 100 / 3
33.333333333333336
```
```
>>> 100 // 3
33
```
...
...
@@ -231,15 +247,17 @@ Cifre con punto
```
>>> int(4.20)
4
```
```
>>> x = float(13)
>>> x
13.0
```
```
>>> type(x)
float
>>> type(13)
int
```
...
...
@@ -257,13 +275,19 @@ int
```
>>> 4 < 5
True
```
```
>>> 3.14 < 2
False
```
```
>>> (4 < 5) or (3.14 < 2)
True
```
```
>>> (4 < 5) and (3.14 < 2)
False
```
...
...
@@ -273,7 +297,9 @@ False
```
>>> (3.14 < 2) or print('Hello world!')
Hello world!
```
```
>>> (3.14 < 2) and print('Hello world!')
False
```
...
...
@@ -308,72 +334,83 @@ False
## Liste (`list`)
-
Collezione ordinata di elementi, anche di tipi diversi
-
Si definiscono con le parentesi quadre
-
Collezione
**ordinata**
di elementi, anche di tipi diversi
-
Si definiscono ponendo all'interno di parentesi quadre gli elementi
intervallati da una virgola
```
python
lista
=
[
f
alse
,
1
,
"due"
,
3.0
,
4
,
5
]
```
lista = [
F
alse, 1, "due", 3.0, 4, 5]
```
-
Per accedere ad un elemento si usano le parentesi quadre
-
Il primo elemento ha indice 0 (zero-based)
-
Si accede a un elemento della lista aggiungendo l'indice dell'elemento
desiderato tra parentesi quadre
```
>>> lista[2]
'due'
```
-
Possono essere modificate
----
```
>>> lista = [False, 1, "due", 3.0, 4, 5]
>>> lista
[False, 1, 'due', 3.0, 4, 5]
```
```
>>> lista[0] = 0.0
>>> lista
[0.0, 1, 'due', 3.0, 4, 5]
```
```
>>> lista.append('sei')
>>> lista
[0.0, 1, "due", 3.0, 4, 5, 'sei']
```
----
## Tuple (`tuple`)
-
Esattamente come le liste, ma immutabili
-
Si definiscono con le parentesi tonde
```
python
tupla
=
(
'a'
,
1
,
2
,
'3.0'
)
```
>>> lista.remove('sei')
>>> lista
[0.0, 1, "due", 3.0, 4, 5]
```
```
>>> tupla[1] = 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> lista.reverse()
>>> lista
[5, 4, 3.0, 'due', 1, 0.0]
```
```
>>> tupla.append('sei')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
>>> len(lista)
6
```
----
## Unpacking
##
#
Unpacking
Estrarre valori da un contenitore
```
>>> t = (1, 2)
```
```
>>> a, b = t
>>> b
2
```
```
>>> a, b = b, a
>>> a
2
```
```
>>> c, _ = t
>>> c
1
...
...
@@ -381,7 +418,7 @@ Estrarre valori da un contenitore
----
## L'operatore `in`
##
#
L'operatore `in`
Valuta l'appartenenza di un elemento ad una collezione
...
...
@@ -402,6 +439,32 @@ False
----
## Tuple (`tuple`)
-
Esattamente come le liste, ma
**immutabili**
-
Sono quindi anche
**ordinate**
-
Si definiscono con le parentesi tonde
```
python
tupla
=
(
'a'
,
1
,
2
,
'3.0'
)
```
```
>>> tupla[1] = 1.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
```
```
>>> tupla.append('sei')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'
```
----
## Riferimenti
-
Quando assegniamo un oggetto ad una variabile, la variabile contiene solo un
...
...
@@ -410,10 +473,16 @@ False
```
>>> spesa_a = ['mela', 'mango']
```
```
>>> spesa_b = spesa_a
>>> spesa_b.append('carota')
>>> spesa_b
['mela', 'mango', 'carota']
```
```
>>> spesa_a
['mela', 'mango', 'carota']
```
...
...
@@ -423,12 +492,11 @@ False
## Insiemi (`set`)
-
Come gli insiemi matematici
-
Non sono ordinati
-
**
Non sono ordinati
**
-
Non contengono elementi duplicati
```
>>> frutta = {"mele", "pere", "zucchine", "mele"}
>>> frutta
{'mele', 2, 'arance', 'pere'}
```
...
...
@@ -440,16 +508,24 @@ Supportano le operazioni insiemistiche
```
>>> frutta = {"mele", "pere", "zucchine", "mele"}
>>> verdure = {"zucchine", "verze", "coste", "porri"}
```
```
>>> frutta.union(verdure)
{'porri', 'verze', 'pere', 'zucchine', 'coste', 'mele'}
```
```
>>> frutta.intersection(verdure)
{'zucchine'}
```
```
>>> frutta.difference(verdure)
{'pere', 'mele'}
```
```
>>> verdure.difference(frutta)
{'verze', 'coste', 'porri'}
```
...
...
@@ -461,16 +537,24 @@ La sintesi è anche espressività
```
>>> frutta = {"mele", "pere", "zucchine", "mele"}
>>> verdure = {"zucchine", "verze", "coste", "porri"}
```
```
>>> frutta | verdure
{'coste', 'verze', 'mele', 'pere', 'porri', 'zucchine'}
```
```
>>> frutta & verdure
{'zucchine'}
```
```
>>> frutta - verdure
{'mele', 'pere'}
```
```
>>> verdure - frutta
{'coste', 'verze', 'porri'}
```
...
...
@@ -480,13 +564,15 @@ La sintesi è anche espressività
## Dizionari (`dict`)
-
Associano una chiave ad un valore
-
Le chiavi devono essere immutabili
-
Le chiavi devono essere immutabili
(stringhe, tuple)
-
I valori possono essere cambiati
```
python
d
=
{
"chiave"
:
"valore"
,
"nome"
:
"Tancredi"
,
"cognome"
:
"Orlando"
"cognome"
:
"Orlando"
,
(
'immutable'
,
'types'
):
[
'are'
,
'cool'
]
}
```
...
...
@@ -497,6 +583,11 @@ d = {
'Tancredi'
```
```
>>> print('these things ' + ' '.join(d[('immutable', 'types')])
these things are cool
```
---
# Strutture di controllo
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment