Python!’) $ python my_file.py Generate the byte-code Execute the byte-code $ chmod +x my_ fi le.py $ ./my_ fi le.py top to bottom “line-by-line" execution You may see fi les ending with .pyc those are cached byte-code fi les >>> def foo(a, b): ... c = 0x20 ... return a * c + b ... >>> import dis >>> dis.dis(foo) 2 0 LOAD_CONST 1 (32) 3 STORE_FAST 2 (c) 3 6 LOAD_FAST 0 (a) 9 LOAD_FAST 2 (c) 12 BINARY_MULTIPLY 13 LOAD_FAST 1 (b) 16 BINARY_ADD 17 RETURN_VALUE cython
else: print('a is not equal to 10') if penLocation == 'table': print('the pen is on the table’) elif penLocation == 'floor’: print('the pen is on the floor') elif penLocation == ‘bag’: print(‘the pan is in the bag yoo’) else: print('I have no idea where the pen is') if a in (10, 20, 30, 40): print(‘a is in the cool range’) data = {‘k1’: 20, ‘k2’: 30} if a not in data: print(‘the key %s is not in the dict’ % a) keys = set([‘k1’, ‘k2’, ‘k3’} if a in data: print(‘the key %s is in the set’ % a) if/elif/else if in/not in a == 10 a != b a > 5 a <= 7 a == ‘foo’ a != ‘foo’ a < ‘foo’ a >= ‘foo’ a == 10 and b == 20 or c == 30 …there are no parenthesis, it’s all indentation based
= [i for i in range(20)] list comprehension x = [i * 2 for i in range(20)] x = [i * 2 for i in range(20) if i % 2 == 0] my_list = [‘a’, ‘b’, ‘c’] for value in my_list: print(‘%s’ % value) my_list = [‘a’, ‘b’, ‘c’] for i, value in enumerate(my_list): print(‘%s at index %d’ % (value, i)) my_dict = {‘k1’: 10, ‘k2’: 20} for key in my_dict: print(‘%s -> %s’ % (key, my_dict[key)) my_dict = {‘k1’: 10, ‘k2’: 20} for key, value in my_dict.items(): print(‘%s -> %s’ % (key, value) i = 0 while i < 20: print(‘%d’ % i) i += 1 for i in range(10, 20, 2): print(‘%d’ % i) Dict comprehension x = {i: i * 20 for i in range(20)} my_list_of_tuples = [(‘a’, 10, ‘foo’), (‘b’, 20, ‘bar)] for a, b, c in my_list_of_tuples: print(‘%s %d %s’ % (a, b, c)) i = 10 while i < 20: print(‘%d’ % i) i += 2
line = fd.readline() if not line: break print(line) finally: fd.close() with open(‘my_file.txt’, ‘r’) as fd: while True: buf = fd.read(512) if not buf: break print(buf) with open(‘my_file.txt’, ‘w’) as fd: fd.write(‘Hello File!\n’) fd.write(‘moar!\n’) using the with statement Writing a File Writing Binary Data a = 2 ** 23 # uint32 (I) b = 2 ** 56 # uint64 (Q) c = -300 # int16 (h) d = 0x7f # uint8 (B) import struct le_data = struct.pack(‘<IQhB’, a, b, c, d); be_data = struct.pack(‘>IQhB’, a, b, c, d); a, b, c, d = struct.unpack(‘<IQhB’, le_data) a, b, c, d = struct.unpack(‘>IQhB’, be_data) Reading Binary Data 0x00 0x00FF008F 0xFF 0x00 0x8F 0x8F 0x00 0xFF 0x00 Little Endian (Intel) Big Endian
func %d' % i) time.sleep(5) thread.start_new_thread(my_thread_func) Threads Yield def my_func(): for i in range(10): yield 'thread func %d' % i def my_func(): raise Exception(“bang!”) try: my_func() except Exception as e: print(‘got an exception: %r’ % e) Exceptions it = iter(my_func()) while True: try: v = next(it) print(v) print(‘yooo’) except StopIteration: break for v in my_func(): print(v) print(‘yooo’)
ctypes.create_string_buffer(16) my_lib.my_rand(buf, 16) Calling C methods from Python int my_method (void); int my_rand (void *buf, int n); my_lib.so/my_lib.dylib/…