== != > < >= <=
+ - * / % & | ^
'Hello, %s!' % name
123 0x10 1e+100
"String syntax.\r\n"
if
else
while
break
continue
for item in sequence:
print(item)
# Iterator-powered range-based “for”,
# like C++11
if (n = 0) then begin
writeln('N is now zero');
func := 1
end
int a[] = {4, 5, 6};
int a[] = {
4,
5,
6
};
# Leave it off
a = [4, 5, 6]
a = [
4,
5,
6
]
a = [4, 5, 6,]
a = [
4,
5,
6,
]
# Or, put it in
import json
j = json.load(file)
import pickle
p = pickle.load(file)
def write_csv(data, sep=',', eol='\n', quote='"'):
...
write_csv(rows)
write_csv(rows, ' ') # “data, sep”
write_csv(rows, quote=':')
# Like C++, give full access
# to raw UNIX system calls!
s = socket.socket()
s.connect(address)
s.recv(1024)
oflags & 0x80 == nflags & 0x80
oflags & (0x80 == nflags) & 0x80
→ 0
if 0: # False
if 0.0: # False
if "": # False
if []: # False
if not my_list:
# (list was empty)
int i = 2147483647;
i++;
printf("%d\n", i);
# -2147483648
i = 2147483647
print(i + 1)
# 2147483648
print(2 ** 257)
print(2 ** 257)
2315841784746323-
9084714197001737-
5815706539969331-
2811280789151680-
15826259279872
js> '4' - 3
js> '4' - 3
1
js> '4' - 3
1
js> '4' + 3
js> '4' - 3
1
js> '4' + 3
"43"
js> [] + []
js> [] + []
""
js> [] + {}
js> [] + {}
"[object Object]"
js> [] + {}
"[object Object]"
js> {} + []
js> [] + {}
"[object Object]"
js> {} + []
0
perl> "3" + 1
4
$ echo | awk '{print "3" + 1}'
4
>>> '4' - 3
TypeError
>>> '4' + 3
TypeError
>>> int('4') + 3
7
>>> '4' + str(3)
'43'
[{'ext': '.c', 'binary': 'gcc'},
{'ext': '.py', 'binary': 'python3'},
{'ext': '.sh', 'binary': 'bash'}]
{'linter': ['lint'],
'compile': ['gcc', 'strip'],
'archive': ['tar']}
errors = {('load.py', 8): '"foo" undefined',
('load.py', 47): 'unused name "n"',
('store.py', 12): 'syntax error'}
'store.py' 12
'load.py' 8
'load.py' 47
'store.py:12'
'load.py:8'
'load.py:47'
'load.py:47'
'load.py:8'
'store.py:12'
'load.py:000008'
'load.py:000047'
'store.py:0000012'
{('load.py', 8): '"foo" undefined',
('load.py', 47): 'unused name "n"',
('store.py', 12): 'Syntax error'}
[('dropbox.com', 443),
('google.com', 80),
('google.com', 443),
('stackoverflow.com', 443)]
openers = {
'.pdf': pdflib.parse,
'.png': imagelib.open,
'.txt': str.read,
}
file_types = {
'.pdf': PdfDocument,
'.png': BitmapImage,
'.txt': str,
}
struct {
struct _typeobject *ob_type;
/* followed by object’s data */
}
struct _typeobject {
...
getattrfunc tp_getattr;
setattrfunc tp_setattr;
...
newfunc tp_new;
freefunc tp_free;
...
binaryfunc nb_add;
binaryfunc nb_subtract;
...
richcmpfunc tp_richcompare;
...
}
a = b
my_list[0] = n
my_dict['Wrocław'] = 'CET'
struct _typeobject {
...
getattrfunc tp_getattr;
setattrfunc tp_setattr;
...
newfunc tp_new;
freefunc tp_free;
...
binaryfunc nb_add;
binaryfunc nb_subtract;
...
richcmpfunc tp_richcompare;
...
}
n = 3
some_function(n)
# Q: I just passed a pointer!
# Could some_function() have changed “3”?
min([8, 3, 5])
max([3.125, 3.16, 3.139])
min(['Warszawa', 'Kraków', 'Wrocław'])
class Address(object):
def __init__(self, host, port):
self.host = host
self.port = port
struct _typeobject {
...
getattrfunc tp_getattr;
setattrfunc tp_setattr;
...
newfunc tp_new;
...
struct {
struct _typeobject *ob_type;
/* followed by object’s data */
}
struct {
Py_ssize_t ob_refcnt;
struct _typeobject *ob_type;
/* followed by object’s data */
}
>>> 'byte string ' + u'unicode string'
u'byte string unicode string'
>>> b'byte string ' + u'unicode string'
TypeError: can't concat bytes to str
>>> sorted(['b', 1, 'a', 2])
[1, 2, 'a', 'b']
>>> sorted(['b', 1, 'a', 2])
TypeError: unorderable types: int() < str()
class Address(object):
def __init__(self, host, port):
self.host = host
self.port = port
# But what if the port wasn’t specified?
class Address(object):
def __init__(self, host, port=None):
self.host = host
if port is not None: # So terrible
self.port = port
# Code was forced to use introspection
# (terrible!)
if hasattr(addr, 'port'):
print(addr.port)
# Today’s best practice:
# every attribute always present
if addr.port is not None:
print(addr.port)
def Dataframe(object):
def __init__(self, columns):
if isinstance(columns, str):
columns = str.split(',')
self.columns = columns
Dataframe(['date', 'x', 'y'])
Dataframe('date,x,y')