Built-in
Funzioni
Indice Funzioni |
|||||||||
Errori
Funzioni
_
1
__debug__
1
__doc__
1
__import__
1
__name__
1
abs
1 >>> abs(-2) # Ritorna il valore assoluto del numero
2 2
3 >>> abs(5)
4 5
5 >>> abs(0)
6 0
all
1 >>> all([1, 2, 3]) # Ritorna True se tutti gli elementi dell'Iterable sono True
2 True
3 >>> all([0, 1, 2]) # altrimenti False
4 False
5 >>> all(['foo', 'bar', 'baz'])
6 True
7 >>> all(['foo', 'bar', ''])
8 False
any
1 >>> any([1, 2, 3]) # Ritorna True se c'è almeno un elemento True nell'Iterable
2 True
3 >>> any([0, '', None]) # altrimenti False
4 False
5 >>> any([0, '', None, 8])
6 True
7 >>> any(['foo', 'bar', ''])
8 True
apply
1 >>> def func(x, y, z): return x*y - z
2 ...
3
4 >>> apply(func, (3,4,2)) # Passando una sequenza come secondo argomento si
5 10 # associano gli argomenti in base alla posizione
6 >>> func(3,4,2) # ed equivale a questa chiamata
7 10
8
9 >>> apply(func, (), {'z':2, 'x':3, 'y':4}) # Passando un dizionario come terzo argomento si
10 10 # associano gli argomenti in base al nome
11 >>> func(z=2, x=3, y=4) # ed equivale a questa chiamata
12 10
basestring
1
bool
1 >>> bool(5) # Ritorna True se l'argomento è True, altrimenti False
2 True
3 >>> bool('foo')
4 True
5 >>> bool(-2)
6 True
7 >>> bool([0])
8 True
9 >>> bool([0, None]) # La lista contiene elementi quindi è True
10 True
11 >>> bool([])
12 False
13 >>> bool('')
14 False
15 >>> bool(0)
16 False
17 >>> bool(5 > 50)
18 False
19 >>> bool(5 > 3)
20 True
21 >>> bool(5 == 5.0)
22 True
23 >>> bool(5 in [1,3,5,7,9])
24 True
25 >>> bool(2 in [1,3,5,7,9])
26 False
27 >>> bool('key1' in {'key1': 'value1', 'key2': 'value2'})
28 True
29 >>> bool('value1' in {'key1': 'value1', 'key2': 'value2'})
30 False
buffer
1
callable
1
chr
1 >>> chr(97) # Ritorna il carattere corrispondente al valore ascii.
2 'a'
3 >>> for x in range(97,123):
4 ... print chr(x),
5 ...
6 a b c d e f g h i j k l m n o p q r s t u v w x y z
classmethod
1
cmp
1 >>> cmp(1,2) # Ritorna -1 se il risultato del confronto tra 'x' e 'y' risulta negativo
2 -1
3 >>> cmp(2,1) # 1 in caso contrario
4 1
5 >>> cmp(1,1) # 0 in caso di uguaglianza
6 0
7 >>> cmp('foo', 'bar')
8 1
9 >>> cmp('foo', 'Foo') # Ritorna True perchè l'ascii di 'f' è 102, mentre quello di 'F' è di 70
10 1
11 >>> cmp('foo', 'foo')
12 0
13 >>> cmp('foo', 'foobar')
14 -1
15 >>> cmp([1,2,3], [1,2,3]) # Nelle liste e nei dizionari fa il confronto fra gli elementi interni
16 0
17 >>> cmp([1,2,3], [1,2,4])
18 -1
19 >>> cmp([1,2,4], [1,2,3])
20 1
21 >>> cmp({'k1': 'v1', 'k2': 'v2'}, {'k1': 'v1', 'k2': 'v2'})
22 0
23 >>> cmp({'k': 'v', 'k2': 'v2'}, {'k1': 'v1', 'k2': 'v2'})
24 -1
25 >>> cmp({'k1': 'v1', 'k2': 'v2'}, {'k1': 'v1', 'k': 'v'})
26 1
27 >>> cmp({'k1': 'v1', 'k2':'v2'}, [1,2,3]) # Tra dizionari e liste il confronto avviene per ordine alfabetico
28 -1
29 >>> cmp({'k1': 'v1', 'k2':'v2'}, 4) # Mentre per i numeri c'è un'eccezione alla regola.
30 1
coerce
1
compile
1
complex
1
copyright
1
credits
1
delattr
1
dict
1 >>> d = dict() # crea un dizionario vuoto
2 >>> d['key'] = 'value'
3 >>> d
4 {'key': 'value'}
5 >>> d = dict([('key','value'), ('key2','value2')]) # Crea un dizionario da una lista di tuple
6 >>> d
7 {'key2': 'value2', 'key': 'value'}
8 >>> d = dict([(x, x**2) for x in range(5)]) # Attraverso una list comprehension che crea tuple
9 >>> d
10 {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
11 >>> d = dict((x, x+2) for x in range(5)) # Tramite i generatori di espressioni
12 >>> d
13 {0: 2, 1: 3, 2: 4, 3: 5, 4: 6}
14 >>> d = dict(key='value', key2='value2') # crea un dizionario tramite l'operatore di assegnamento '='
15 >>> d
16 {'key2': 'value2', 'key': 'value'}
17 >>> d = dict(key=1234, key2=1234)
18 >>> d
19 {'key2': 1234, 'key': 1234}
dir
1 >>> dir() # Senza argomento restituisce i nomi dello scope corrente
2 ['__builtins__', '__doc__', '__name__']
3
4 >>> dir('') # Se l'argomento è un oggetto, restituisce la lista dei suoi metodi/attributi
5 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
6
7 >>> import re
8 >>> dir(re) # Se l'argomento è un modulo, restituisce la lista delle sue funzioni/classi/variabili
9 ['DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 'S', 'U', 'UNICODE', 'VERBOSE', 'X', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 'compile', 'engine', 'error', 'escape', 'findall', 'finditer', 'match', 'purge', 'search', 'split', 'sub', 'subn', 'template']
divmod
1
enumerate
1
eval
1
execfile
1
exit
1 >>> exit('bye') # Lascia un messaggio all'uscita. Vedi anche ```quit()```
2 bye
3 markon@YouthGoneWild:~$
4 >>> exit() # Esce senza messaggio
5 markon@YouthGoneWild:~$
file
1
filter
1
float
1
frozenset
1
getattr
1
globals
1
hasattr
1 >>> class Foo:
2 ... foo = 'bar'
3 ... bar = 'foo'
4 ...
5 >>> hasattr(Foo, 'foo') # Ritorna True se l'attributo è presente nella classe
6 True
7 >>> hasattr(Foo, 'bar')
8 True
9 >>> hasattr(Foo, 'foobar') # altrimenti False
10 False
hash
1
help
1
hex
1
id
1 >>> x = 'foo' # Ritorna l'id di un oggetto. Corrisponde all'indirizzo di memoria dell'oggetto stesso.
2 >>> id(x)
3 11019872
4 >>> id('foo')
5 11019872
6 >>> y = x
7 >>> id(y)
8 11019872
9 >>> x = 'bar'
10 >>> id(x)
11 10332160
input
1
int
1
intern
1
isinstance
1
issubclass
1
iter
1
len
1 >>> len('python') # Ritorna la lunghezza dell'oggetto dato
2 6
3 >>> len(['foo' ,'bar', 'baz'])
4 3
5 >>> len({1:'foo', 2:'bar', 3:'baz'}) # Ci sono 3 coppie chiave/valore
6 3
7 >>> len('')
8 0
license
1 >>> license() # Mostra la licenza di Python
2 A. HISTORY OF THE SOFTWARE
3 ==========================
4
5 Python was created in the early 1990s by Guido van Rossum at Stichting
6 Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands
7 as a successor of a language called ABC. Guido remains Python's
8 principal author, although it includes many contributions from others.
9
10 ............... # Continua...
list
1
locals
1