Dopo aver visto nella prima lezione http://www.sciax2.it/forum/guide-tutorial/python-1-lezione-662549.html
la funzione print , in questa guida vedremo come far interagire il programma in base alle nostre scelte, In questa guida per spiegare cosa stiamo facendo userò direttamente nel codice # perchè con # si indica al programma di non considerare quello che viene dopo esempio:
oggi spiegheremo la funzione input che sta per ingresso quindi , noi dovremmo scrivere un input da tastiera e ci restituirà l'output
esempio:
il risultato da terminale linux è il seguente
altro esempio:
risultato
IO SONO ABITUATO AD USARE (" ") ANCHE NEL PYTHON 2 MA NON CAMBIA NULLA
vi invito a leggere le differenze tra python 2 e python3 :
print
print non è più un’istruzione ma una funzione. E come tale vuole le parentesi.
Python 2: print "The answer is", 42
Python 3: print("The answer is", 42)
Output: The answer is 42
Python 2: print
Python 3: print()
Output: newline
Per formattare l’output py2 usa una sintassi speciale mentre py3 usa le keyword sep e end.
Python 2:
print "The answer is", # comma suppresses newline
print 42
Output: The answer is 42
Python 3:
print("The answer is", end=" ")
print(42)
Output: The answer is 42
Python 3: print("01","12","1981",sep="-")
Output: 01-12-1981
Si può usare print() anche in py2, basta importare print_function dal modulo future
from future import print_function
Divisione fra interi
In py2 la divisione fra interi ritorna un intero troncando il risultato. Per avere un float si deve avere almeno un float ( o fare un cast). In py3 la divisione ritorna un float, anche se il risultato è un intero. Esiste l’operatore // che si comporta come / in py2.
Python 2: 4/3 # result is 1
Python 2: 3/3 # result is 1
Python 2: 4.0/3 # result is 1.33333
Python 2: 3.0/3 # result is 1.0
Python 3: 4/3 # result is 1.33333
Python 3: 3/3 # result is 1.0
Python 3: 4//3 # result is 1
Python 3: 3//3 # result is 1
Al solito in py2 si possono usare gli operatori di py3 impotando division dal modulo future
from future import division
input
La funzione raw_input() di py2 è stata rinominata input() in py3. Notare che le due funzioni ritornano una string; eseguire un cast se del caso
Python 2: the_input = raw_input() # the_input is of type string
Python 3: the_input = input() # the_input is of type string
Python 2: the_input = float(raw_input()) # the_input is of type float
Python 2: the_input = int(raw_input()) # the_input is of type int
Python 3: the_input = float(input()) # the_input is of type float
Python 3: the_input = int(input()) # the_input is of type int
file
Il comando file di py2 è stato rimosso. Occorre usare la funzione open() (che in realtà c’era da tempo in py2).
Python 2:
for line in file("my_file.txt"):
print line
Python 3:
myFile = open("my_file.txt",'rne in myFile:
print line
range
La funzione range() in py3 è l’equivalente di xrange() di py2, non ritorna una lista e può gestire un intervallo arbitrariamente ampio
Python 2: L = range(10) # L is [0,1,2,3,4,5,6,7,8,9]
Python 3: L = list(range(10)) # L is [0,1,2,3,4,5,6,7,8,9]
questa istruzione genera un errore in py2 ma è valida in py3
for i in range(1000000000000): # 1'000'000'000'000 = mille miliardi
FONTE DELLE DIFERENZE =
la funzione print , in questa guida vedremo come far interagire il programma in base alle nostre scelte, In questa guida per spiegare cosa stiamo facendo userò direttamente nel codice # perchè con # si indica al programma di non considerare quello che viene dopo esempio:
Codice:
Perfavore,
Entra
oppure
Registrati
per vedere i codici!
esempio:
Codice:
Perfavore,
Entra
oppure
Registrati
per vedere i codici!
il risultato da terminale linux è il seguente
altro esempio:
Codice:
Perfavore,
Entra
oppure
Registrati
per vedere i codici!
IO SONO ABITUATO AD USARE (" ") ANCHE NEL PYTHON 2 MA NON CAMBIA NULLA
vi invito a leggere le differenze tra python 2 e python3 :
print non è più un’istruzione ma una funzione. E come tale vuole le parentesi.
Python 2: print "The answer is", 42
Python 3: print("The answer is", 42)
Output: The answer is 42
Python 2: print
Python 3: print()
Output: newline
Per formattare l’output py2 usa una sintassi speciale mentre py3 usa le keyword sep e end.
Python 2:
print "The answer is", # comma suppresses newline
print 42
Output: The answer is 42
Python 3:
print("The answer is", end=" ")
print(42)
Output: The answer is 42
Python 3: print("01","12","1981",sep="-")
Output: 01-12-1981
Si può usare print() anche in py2, basta importare print_function dal modulo future
from future import print_function
Divisione fra interi
In py2 la divisione fra interi ritorna un intero troncando il risultato. Per avere un float si deve avere almeno un float ( o fare un cast). In py3 la divisione ritorna un float, anche se il risultato è un intero. Esiste l’operatore // che si comporta come / in py2.
Python 2: 4/3 # result is 1
Python 2: 3/3 # result is 1
Python 2: 4.0/3 # result is 1.33333
Python 2: 3.0/3 # result is 1.0
Python 3: 4/3 # result is 1.33333
Python 3: 3/3 # result is 1.0
Python 3: 4//3 # result is 1
Python 3: 3//3 # result is 1
Al solito in py2 si possono usare gli operatori di py3 impotando division dal modulo future
from future import division
input
La funzione raw_input() di py2 è stata rinominata input() in py3. Notare che le due funzioni ritornano una string; eseguire un cast se del caso
Python 2: the_input = raw_input() # the_input is of type string
Python 3: the_input = input() # the_input is of type string
Python 2: the_input = float(raw_input()) # the_input is of type float
Python 2: the_input = int(raw_input()) # the_input is of type int
Python 3: the_input = float(input()) # the_input is of type float
Python 3: the_input = int(input()) # the_input is of type int
file
Il comando file di py2 è stato rimosso. Occorre usare la funzione open() (che in realtà c’era da tempo in py2).
Python 2:
for line in file("my_file.txt"):
print line
Python 3:
myFile = open("my_file.txt",'rne in myFile:
print line
range
La funzione range() in py3 è l’equivalente di xrange() di py2, non ritorna una lista e può gestire un intervallo arbitrariamente ampio
Python 2: L = range(10) # L is [0,1,2,3,4,5,6,7,8,9]
Python 3: L = list(range(10)) # L is [0,1,2,3,4,5,6,7,8,9]
questa istruzione genera un errore in py2 ma è valida in py3
for i in range(1000000000000): # 1'000'000'000'000 = mille miliardi
FONTE DELLE DIFERENZE =
Perfavore,
Entra
oppure
Registrati
per vedere i Link!
Ultima modifica: