2
Typy a operátory
% python
>>> a = 3 >>> b = 4
>>> b / 2 + a 5
>>> b / (2.0 + a) 0.8
>>> x = 1 >>> x << 2 4
>>> x | 2 3
>>> x & 1 1
>>> 9999999999999999999999999999 + 1
10000000000000000000000000000L
>>> 9999999999999999999999999999L + 1
10000000000000000000000000000L
>>> 1j * 1J
(-1+0j)
>>> 2 + 1j * 3
(2+3j)
>>> (2+1j)*3
(6+3j)
>>> abs(-42), 2**4, pow(2, 4)
(42, 16, 16)
>>> import math
>>> math.pi
3.1415926535897931
% python
>>> len('abc') 3
>>> 'abc' + 'def' 'abcdef'
>>> 'Ni!' * 4 'Ni!Ni!Ni!Ni!'
>>> myjob = "hacker"
>>> for c in myjob: print c, ...
h a c k e r
>>> "k" in myjob 1
>>> S = 'spam'
>>> S[0], S[-2] ('s', 'a')
>>> S[1:3], S[1:], S[:-1] ('pa', 'pam', 'spa')
>>> S = 'spam'
>>> S[0] = "x" >>> S = S + 'Spam!' >>> S
'spamSpam!'
>>> S = S[:4] + 'Burger' + S[-1]
>>> S
'spamBurger!'
>>> 'That is %d %s bird!' % (1, 'dead') That is 1 dead bird!
>>> exclamation = "Ni"
>>> "The knights who say %s!" % exclamation
'The knights who say Ni!'
>>> "%d %s %d you" % (1, 'spam', 4)
'1 spam 4 you'
>>> "%s -- %s -- %s" % (42, 3.14159, [1, 2, 3])
'42 -- 3.14159 -- [1, 2, 3]'
>>> import string >>> S = "spammify"
>>> string.upper(S) 'SPAMMIFY'
>>> string.find(S, "mm") 3
>>> string.atoi("42"), `42` (42, '42')
>>> string.join(string.split(S, "mm"), "XX")
'spaXXify'
>>> "spam" + 42 >>> "spam" + '42'
'spam42'
>>> string.atoi("42") + 1
43
>>> mixed = "Guido's" >>> mixed
"Guido's"
>>> mixed = 'Guido"s' >>> mixed
'Guido"s'
>>> mixed = 'Guido\'s' >>> mixed
"Guido's"
>>> split = "This" "is" "concatenated"
>>> split
'Thisisconcatenated'
>>> big = """Python puts
... an end-of-line marker
... after each line."""
>>> big
'Python puts\nan end-of-line marker\nafter each line.'
% python
>>> len([1, 2, 3]) 3
>>> [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6]
>>> ['Ni!'] * 4 ['Ni!', 'Ni!', 'Ni!', 'Ni!']
>>> for x in [1, 2, 3]: print x, ...
1 2 3
>>> `[1, 2]` + "34" '[1, 2]34'
>>> [1, 2] + list("34") [1, 2, '3', '4']
>>> L = ['spam', 'Spam', 'SPAM!']
>>> L[2] 'SPAM!'
>>> L[-2] 'Spam'
>>> L[1:] ['Spam', 'SPAM!']
>>> L = ['spam', 'Spam', 'SPAM!']
>>> L[1] = 'eggs' >>> L
['spam', 'eggs', 'SPAM!']
>>> L[0:2] = ['eat', 'more'] >>> L ['eat', 'more', 'SPAM!']
>>> L.append('please') >>> L
['eat', 'more', 'SPAM!', 'please']
>>> L.sort() >>> L
['SPAM!', 'eat', 'more', 'please']
>>> L
['SPAM!', 'eat', 'more', 'please']
>>> del L[0] >>> L
['eat', 'more', 'please']
>>> del L[1:] >>> L ['eat']
% python
>>> d2 = {'spam': 2, 'ham': 1,
... 'eggs': 3}
>>> d2['spam'] 2
>>> len(d2) 3
>>> d2.has_key('ham') 1
>>> d2.keys() ['eggs', 'spam', 'ham']
>>> d2['ham'] = ['grill', 'bake', 'fry'] >>> d2
{'eggs': 3, 'spam': 2, 'ham': ['grill', 'bake', 'fry']}
>>> del d2['eggs'] >>> d2
{'spam': 2, 'ham': ['grill', 'bake', 'fry']}
>>> d2['brunch'] = 'Bacon' >>> d2
{'brunch': 'Bacon', 'spam': 2, 'ham': ['grill', 'bake', 'fry']}
>>> table = {'Python': 'Guido van Rossum',
... 'Perl': 'Larry Wall',
... 'Tcl': 'John Ousterhout' }
... >>> language = 'Python'
>>> creator = table[language]
>>> creator
'Guido van Rossum'
>>> for lang in table.keys(): print lang, '\t', table[lang]
...
Tcl John Ousterhout
Python Guido van Rossum
Perl Larry Wall
>>> soubor = open('soubor', 'w') >>> soubor.write('Ahoj soubore\n') >>> soubor.close()
>>> soubor = open('soubor', 'r') >>> soubor.readline() 'hello text file\n'
>>> soubor.readline() ''
>>> L = ['abc', [(1, 2), ([3], 4)], 5]
>>> L[1]
[(1, 2), ([3], 4)]
>>> L[1][1]
([3], 4)
>>> L[1][1][0]
[3]
>>> L[1][1][0][0]
3
>>> X = [1, 2, 3]
>>> L = ['a', X, 'b']
>>> D = {'x':X, 'y':2}
>>> X[1] = 'Ivan' >>> L
['a', [1, 'Ivan', 3], 'b']
>>> D
{'x': [1, 'Ivan', 3], 'y': 2}
>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 3)] >>> L1 == L2, L1 is L2 (1, 0)
>>> L1 = [1, ('a', 3)]
>>> L2 = [1, ('a', 2)]
>>> L1 < L2, L1 == L2, L1 > L2 (0, 0, 1)
>>> L = [1, 2, 3]
>>> M = ['X', L, 'Y'] >>> M
['X', [1, 2, 3], 'Y']
>>> L[1] = 0 >>> M
['X', [1, 0, 3], 'Y']
>>> L = [1, 2, 3]
>>> M = ['X', L[:], 'Y'] >>> L[1] = 0 >>> L
[1, 0, 3]
>>> M
['X', [1, 2, 3], 'Y']
>>> L = [4, 5, 6]
>>> X = L * 4 >>> Y = [L] * 4 >>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 5, 6], [4, 5, 6], [4, 5, 6], [4, 5, 6]]
>>> L[1] = 0 >>> X
[4, 5, 6, 4, 5, 6, 4, 5, 6, 4, 5, 6]
>>> Y
[[4, 0, 6], [4, 0, 6], [4, 0, 6], [4, 0, 6]]
>>> L = ['ha']; L.append(L) >>> L >>> T = (1, 2, 3)
>>> T[2] = 4 >>> T = T[:2] + (4,) 2 ** 16
2 / 5, 2 / 5.0
"spam" + "eggs"
S = "ham"
"eggs " + S
S * 5
S[:0]
"green %s and %s" % ("eggs", S)
('x',)[0]
('x', 'y')[1]
L = [1,2,3] + [4,5,6]
L, L[:], L[:0], L[-2], L[-2:]
([1,2,3]+[4,5,6])[2:4]
[L[2], L[3]]
L.reverse(); L
L.sort(); L
L.index(4); L
{'a':1, 'b':2}['b']
D = {'x':1, 'y':2, 'z':3}
D['w'] = 0
D['x'] + D['w']
D[(1,2,3)] = 4
D.keys(), D.values(), D.has_key((1,2,3))
[[]], ["",[],(),{},None]
X = 'spam'
Y = 'eggs'
X, Y = Y, X
X
Y
D = {}
D[1] = 'a'
D[2] = 'b'
>>> D[(1, 2, 3)] = 'c'
>>> D
{1: 'a', 2: 'b', (1, 2, 3): 'c'}