-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpython3.txt
More file actions
117 lines (102 loc) · 2.99 KB
/
python3.txt
File metadata and controls
117 lines (102 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Conversion
2to3
-w // modify file; backup is created: file.py.bak
-n // do not create backup file.py.bak
-o dir // write modified file to dir
3to2
http://python-future.org // write python3 with back compatibility
# print is function, no statement
print x // does no longer work
print(x, end=' ', file=sys.stderr)
# Extended unpacking
a, b = range(2)
a, b, *rest = range(10)
*rest, a, b = range(10)
first, *t, last = open(file).read().splitlines()
def keyword_only(*many_values, sum=True)
def keyword_only(two, values, *, sum=True)
## interpretation
*list // unpack values of list
l = [1, 2]
print(*l) // == print(l[0], l[1])
a, *l, b = values // == a, l[0], l[1], b = values
string.format(*l) // ==string.format(l[0], l[1])
**dict // unpack values of dict
d = {'a':1, 'b':2}
'{b} {a}'.format(**d) // == '{b} {a}'.format(a=d['a'], b=d['b'])
# Dictionary comprehensives
t = {('a', 1), ('b', 2)}
d = dict(t)
d = {k:v for k,v in t}
# Function that return iterator instead of list
range()
map(), filter(), zip()
d.keys(), d.values()
list(iterator) // iterator -> list
# Creating own iterator
def get_numbers(n):
for i in range(n):
yield i * 2
def get_numbers(n):
yield from range(n)
# Integers
long -> int // int has variable length; long does no longer exist
1 / 2 = 0.5 // integer division returns float
1 // 2 = 0 // floor division
# Enum
from enum import Enum
class Colors(Enum):
red = 1
blue = 1 // values may not be unique
green = 3
Colors.red
Colors.red.name
Colors.red.value
Colors['green'] -> green
Colors(3) -> green
for color in Colors:
print(color.name)
# Unicode strings
* unicode: code points
'h' -> 68
'r' -> 72
'hr' -> 6872
* code points can be stored in different byte formats
* bytes: bytes as stored on disk
* bytes / str completely separated in python3
* bytes / str can be mixed in python2
'Hello' = str('Hello') => unicode in python3
'Hello' = str('Hello') => bytes in python2
bytes() for binary data
s = str('Hello world')
b = b'binary data'
b = s.encode() // unicode -> bytes
s = b.decode() // bytes -> unicode
'123' + b'123' // exception; mixing unicode and bytes not allowed in python3
'123' + b'123'.decode() // works
open(file) // returns unicode/str in python3; bytes in python2
open(file, 'b') // returns bytes
# dict.iter() / iteritems()
## Python2
.keys(), .values(), .items() // returns list
.iterkeys(), .itervalues(), .iteritems() // returns iterator
## Python3
.iterX() does not exist anymore
.keys() // return dict_keys: list OR iterator
for k in d.keys()
* returns iterator implicitly
* == for k in iter(d.keys())
k = d.keys() // return dict_keys(), like list (?)
## six
import six
from six.moves import range
for k, v in six.iteritems(d) // iterator
for v in six.itervalues(d) // iterator
for k in six.iterkeys(d) // iterator
for i in range(10):
six.PY2 / six.PY3 // check Python version
## futures
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals