Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> >>> 'spam' + 'ni' 'spamni' >>> 2 / 3 0.6666666666666666 >>> ================================ RESTART ================================ >>> C:\class 1267650600228229401496703205376 SpamSpamSpamSpamSpamSpamSpamSpam >>> ================================ RESTART ================================ >>> C:\class 1267650600228229401496703205376 Traceback (most recent call last): File "C:\class\script1.py", line 5, in x = 'Spam' + Y NameError: name 'Y' is not defined >>> ================================ RESTART ================================ >>> C:\class 1267650600228229401496703205376 SpamSpamSpamSpamSpamSpamSpamSpam >>> >>> >>> import script1 C:\class 1267650600228229401496703205376 SpamSpamSpamSpamSpamSpamSpamSpam >>> import script1 >>> import script1 >>> from imp import reload >>> reload(script1) C:\class 1267650600228229401496703205376 SpamSpamSpamSpamSpamSpamSpamSpam >>> >>> exec(open('script1.py').read()) C:\class 1267650600228229401496703205376 SpamSpamSpamSpamSpamSpamSpamSpam >>> >>> >>> x = 'spam' >>> y = 32 >>> >>> x + y Traceback (most recent call last): File "", line 1, in x + y TypeError: Can't convert 'int' object to str implicitly >>> >>> x + str(y) 'spam32' >>> >>> >>> >>> >>> S = 'spam' >>> len(S) 4 >>> S + 'NI' 'spamNI' >>> S * 8 'spamspamspamspamspamspamspamspam' >>> >>> S[0] 's' >>> S[1] 'p' >>> S[-1] 'm' >>> S[-2] 'a' >>> S[len(S)-1] 'm' >>> S 'spam' >>> S[3] 'm' >>> S[5] Traceback (most recent call last): File "", line 1, in S[5] IndexError: string index out of range >>> >>> S 'spam' >>> S[1:3] 'pa' >>> S[1:] 'pam' >>> S[:3] 'spa' >>> S[:-1] 'spa' >>> S[:] 'spam' >>> L = 'abcdefghujkl' >>> L[::2] 'aceguk' >>> L[1::2] 'bdfhjl' >>> L 'abcdefghujkl' >>> >>> L[::-1] 'lkjuhgfedcba' >>> >>> >>> L 'abcdefghujkl' >>> L.upper() 'ABCDEFGHUJKL' >>> S 'spam' >>> >>> S.replace('pa', '$!') 's$!m' >>> >>> S 'spam' >>> >>> X = S.replace('pa', '$!') >>> X 's$!m' >>> S 'spam' >>> S[0] = '>' Traceback (most recent call last): File "", line 1, in S[0] = '>' TypeError: 'str' object does not support item assignment >>> >>> S = '>' + S[1:] >>> S '>pam' >>> >>> dir(S) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>> >>> S '>pam' >>> S + 'NI' '>pamNI' >>> S.__add__('NI') '>pamNI' >>> help(S.rstrip) Help on built-in function rstrip: rstrip(...) S.rstrip([chars]) -> str Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. >>> >>> >>> line = 'abcdefg\n' >>> >>> line[:-1] 'abcdefg' >>> >>> line.rstrip() 'abcdefg' >>> >>> >>> line = '111,222,333,444\n' >>> >>> line.split(',') ['111', '222', '333', '444\n'] >>> >>> line.rstrip().split(',') ['111', '222', '333', '444'] >>> >>> line.find('222') 4 >>> >>> '222' in line True >>> '2xx' in line False >>> >>> line[4] '2' >>> line[3] ',' >>> ord('\n') 10 >>> '222' in line True >>> line '111,222,333,444\n' >>> >>> cols = line.rstrip().split(',') >>> line '111,222,333,444\n' >>> cols ['111', '222', '333', '444'] >>> cols[1] '222' >>> help(line.find) Help on built-in function find: find(...) S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. >>> line '111,222,333,444\n' >>> >>> file = line * 4 >>> file '111,222,333,444\n111,222,333,444\n111,222,333,444\n111,222,333,444\n' >>> lines = file.split('\n') >>> lines ['111,222,333,444', '111,222,333,444', '111,222,333,444', '111,222,333,444', ''] >>> >>> file '111,222,333,444\n111,222,333,444\n111,222,333,444\n111,222,333,444\n' >>> >>> print(file) 111,222,333,444 111,222,333,444 111,222,333,444 111,222,333,444 >>> >>> >>> L = [123, 'spam', 3.14] >>> L[0] = 789 >>> L [789, 'spam', 3.14] >>> len(L) 3 >>> L + [6, 7, 8] [789, 'spam', 3.14, 6, 7, 8] >>> L [789, 'spam', 3.14] >>> >>> L * 4 [789, 'spam', 3.14, 789, 'spam', 3.14, 789, 'spam', 3.14, 789, 'spam', 3.14] >>> >>> L [789, 'spam', 3.14] >>> L[-1] 3.14 >>> L[1:] ['spam', 3.14] >>> L [789, 'spam', 3.14] >>> >>> L.append('NI!') >>> L [789, 'spam', 3.14, 'NI!'] >>> L.index(3.14) 2 >>> L.remove('spam') >>> L [789, 3.14, 'NI!'] >>> L.insert(1, 'Spam') >>> L [789, 'Spam', 3.14, 'NI!'] >>> dir(L) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] >>> >>> help(L.insert) Help on built-in function insert: insert(...) L.insert(index, object) -- insert object before index >>> >>> M = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> >>> M[1] [4, 5, 6] >>> M[1][2] 6 >>> col2 = [row[1] for row in M] >>> col2 [2, 5, 8] >>> >>> diag = [M[i][i] for i in [0, 1, 2]] >>> diag [1, 5, 9] >>> >>> file '111,222,333,444\n111,222,333,444\n111,222,333,444\n111,222,333,444\n' >>> >>> lines ['111,222,333,444', '111,222,333,444', '111,222,333,444', '111,222,333,444', ''] >>> >>> file '111,222,333,444\n111,222,333,444\n111,222,333,444\n111,222,333,444\n' >>> >>> [line.split(',') for line in lines] [['111', '222', '333', '444'], ['111', '222', '333', '444'], ['111', '222', '333', '444'], ['111', '222', '333', '444'], ['']] >>> >>> >>> D = {'a': 1, 'b': 2, 'c': 3} >>> D['b'] 2 >>> D.keys() dict_keys(['b', 'c', 'a']) >>> D.values() dict_values([2, 3, 1]) >>> >>> K = 'd' >>> D[K] = 4 >>> D {'b': 2, 'c': 3, 'a': 1, 'd': 4} >>> >>> D['a'] = 0 >>> D {'b': 2, 'c': 3, 'a': 0, 'd': 4} >>> >>> D = {'a': 123, 'b': 'spam', 'c': 3.14} >>> D {'b': 'spam', 'c': 3.14, 'a': 123} >>> D['a'] 123 >>> D['c'] 3.14 >>> >>> >>> >>> rec = {'name': {'first': 'Bob', 'last': 'Smith'}, 'age': 40.5, 'job': ['dev', 'mgr']} >>> >>> rec['name'] {'first': 'Bob', 'last': 'Smith'} >>> rec['name']['first'] 'Bob' >>> rec['job'][1] 'mgr' >>> rec['job'] ['dev', 'mgr'] >>> rec['job'].append('janitor') >>> >>> rec {'age': 40.5, 'name': {'first': 'Bob', 'last': 'Smith'}, 'job': ['dev', 'mgr', 'janitor']} >>> >>> >>> rec.has_key('name') Traceback (most recent call last): File "", line 1, in rec.has_key('name') AttributeError: 'dict' object has no attribute 'has_key' >>> >>> 'name' in rec True >>> KeyboardInterrupt >>> >>> >>> S = 'abcdefghij' >>> S[3:1:-1] 'dc' >>> >>> >>> >>> file = open('data.txt', 'w') >>> file.write('Hello\n') 6 >>> file.write('world...\n') 9 >>> file.close() >>> >>> file = open('data.txt', 'r') >>> file.read() 'Hello\nworld...\n' >>> >>> file = open('data.txt', 'r') >>> text = file.read() >>> text 'Hello\nworld...\n' >>> print(text) Hello world... >>> import csv >>> >>> file = open('data.txt', 'w') >>> file.write('111\t222\t333\n') 12 >>> file.write('444\t555\t666\n') 12 >>> file.close() >>> >>> file = open('data.txt') >>> print(file.read()) 111 222 333 444 555 666 >>> >>> file = open('data.txt') >>> data = file.read() >>> data '111\t222\t333\n444\t555\t666\n' >>> lines = data.splitlines() >>> lines ['111\t222\t333', '444\t555\t666'] >>> >>> cols = [line.split() for line in lines] >>> cols [['111', '222', '333'], ['444', '555', '666']] >>> >>> S = '111' >>> int(S) 111 >>> float(S) 111.0 >>> >>> S = {1, 2, 3, 4, 5} >>> S {1, 2, 3, 4, 5} >>> T = {2, 4, 6} >>> >>> S & T {2, 4} >>> S | T {1, 2, 3, 4, 5, 6} >>> >>> L = [1, 2, 1, 3, 4, 2, 1, 3, 3] >>> set(L) {1, 2, 3, 4} >>> >>> list(set(L)) [1, 2, 3, 4] >>> >>> >>> None >>> >>> >>> L = [None] * 100 >>> L [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] >>> >>> >>> 'spam' + None Traceback (most recent call last): File "", line 1, in 'spam' + None TypeError: Can't convert 'NoneType' object to str implicitly >>> >>> >>> >>> S = 'spam' >>> S + ('' * (10 - len(S))) 'spam' >>> S + (' ' * (10 - len(S))) 'spam ' >>> >>> L [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] >>> >>> >>> L[0] = 0 >>> L [0, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] >>> >>> L = [0] * 100 >>> L [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> >>> None >>> >>> L = [None] * 100 >>> L [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None] >>> >>> >>> for i in range(100): L[i] = i >>> L [0, 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] >>> >>> range(5) range(0, 5) >>> >>> list(range(5)) [0, 1, 2, 3, 4] >>> >>> >>> for i in range(100): L[i] = float(i) >>> L [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0, 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0, 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0, 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0, 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0] >>> >>> >>> >>> >>> A = 4 >>> B = A >>> >>> A = A + 1 >>> A 5 >>> B 4 >>> >>> A = [1, 2] >>> B = A >>> A[0] = 5 >>> A [5, 2] >>> B [5, 2] >>> >>> >>> >>> A = [1, 2] >>> B = A[:] >>> A [1, 2] >>> B [1, 2] >>> A[0] = 5 >>> A [5, 2] >>> B [1, 2] >>> >>> >>> >>> A = [1, 2] >>> B = A >>> >>> A == B True >>> A is B True >>> >>> A = [1, 2] >>> B = A[:] >>> >>> A == B True >>> A is B False >>> >>> >>> msg = """ aaaaaaaaaaaaa bbbbbbbb'''bb""bbb cccccccccccccc""" >>> >>> msg '\naaaaaaaaaaaaa\nbbbbbbbb\'\'\'bb""bbb\ncccccccccccccc' >>> >>> >>> def func(): """ stuff about func.... """ KeyboardInterrupt >>> >>> open(r'C:\new\text.dat') KeyboardInterrupt >>> >>> >>> >>> >>> >>> X = 123 >>> Y = 'spam' >>> Z = 1.2345 >>> >>> import struct >>> data = struct.pack('", line 1, in data = struct.pack('>> >>> >>> >>> X = 123 >>> Y = b'spam' >>> Z = 1.2345 >>> >>> import struct >>> data = struct.pack('>> data b'{\x00\x00\x00spam\x19\x04\x9e?' >>> >>> file = open('data.bin', 'wb') >>> file.write(data) 12 >>> file.close() >>> >>> file = open('data.bin', 'rb') >>> data = file.read() >>> data b'{\x00\x00\x00spam\x19\x04\x9e?' >>> >>> vals = struct.unpack('>> vals (123, b'spam', 1.2345000505447388) >>> >>> 1.2345 1.2345 >>> vals[2] 1.2345000505447388 >>> >>> >>> >>> X = 123 >>> Y = b'spam' >>> Z = 1.2345 >>> >>> import pickle >>> str = pickle.dumps([X, Y, Z]) >>> str b'\x80\x03]q\x00(K{C\x04spamq\x01G?\xf3\xc0\x83\x12n\x97\x8de.' >>> >>> file = open('data.pkl', 'wb') >>> file.write(str) 27 >>> file.close() >>> >>> file = open('data.pkl', 'rb') >>> data = file.read() >>> data b'\x80\x03]q\x00(K{C\x04spamq\x01G?\xf3\xc0\x83\x12n\x97\x8de.' >>> >>> vals = pickle.loads(data) >>> vals [123, b'spam', 1.2345] >>> help(pickle) KeyboardInterrupt >>> >>> >>> int('123') 123 >>> ================================ RESTART ================================ >>> enter age:50 2500 enter age:40 1600 enter age:30 900 enter age:20 400 enter age:stop bye >>> ================================ RESTART ================================ >>> enter age:99 9801 enter age:spam Traceback (most recent call last): File "C:/class/interact.py", line 4, in print(int(answer) ** 2) ValueError: invalid literal for int() with base 10: 'spam' >>> >>> x = 'spam' >>> x.isdigit() False >>> x = '99' >>> x.isdigit() True >>> ================================ RESTART ================================ >>> enter age:20 400 enter age:spam Bad! enter age:80 6400 enter age:stop Bad! enter age: Traceback (most recent call last): File "C:/class/interact.py", line 2, in answer = input('enter age:') File "C:\Python33\lib\idlelib\PyShell.py", line 1381, in readline line = self._line_buffer or self.shell.readline() KeyboardInterrupt >>> >>> ================================ RESTART ================================ >>> enter age:90 8100 enter age:spam Bad! enter age:stop bye >>> ================================ RESTART ================================ >>> enter age:80 6400 enter age:40.5 Bad! enter age:stop bye >>> 123.99E+100 1.2399e+102 >>> 1.0 1.0 >>> ================================ RESTART ================================ >>> enter age:90 8100.0 enter age:1.23 1.5129 enter age:40.0 1600.0 enter age:123.45e+50 1.5239902500000003e+104 enter age:spam Bad! enter age: Bad! enter age:stop bye >>> ================================ RESTART ================================ >>> enter age: Traceback (most recent call last): File "C:/class/interact.py", line 2, in answer = input('enter age:') File "C:\Python33\lib\idlelib\PyShell.py", line 1381, in readline line = self._line_buffer or self.shell.readline() KeyboardInterrupt >>> ================================ RESTART ================================ >>> enter age:90 8100.0 enter age: No... 8100.0 enter age:stop bye >>> file = open('data.bin', 'rb') KeyboardInterrupt >>> >>> >>> >>> X = [1, 2, 3, 4] >>> X = 0 >>> >>> >>> X = [1, 2, 3, 4] >>> Y = X >>> X = 0 >>> Y [1, 2, 3, 4] >>> X = 3 >>> while X > 0: print(X) X = X -1 3 2 1 >>> while X > 0: print(X) X = X -1 >>> >>> X 0 >>> >>> T Traceback (most recent call last): File "", line 1, in T NameError: name 'T' is not defined >>> >>> T = T + 1 Traceback (most recent call last): File "", line 1, in T = T + 1 NameError: name 'T' is not defined >>> >>> T = S = 0 >>> T = T+1 >>> T 1 >>> S 0 >>> >>> T = S = [] >>> T.append(1) >>> T [1] >>> S [1] >>> >>> T, S = [], [] >>> T.append(1) >>> T [1] >>> S [] >>> a, b, c = [1, 2, 3] >>> a 1 >>> b 2 >>> c 3 >>> >>> a, b = [1, 2, 3] Traceback (most recent call last): File "", line 1, in a, b = [1, 2, 3] ValueError: too many values to unpack (expected 2) >>> >>> a, *b = [1, 2, 3] >>> a 1 >>> b [2, 3] >>> *a, b = [1, 2, 3] >>> a [1, 2] >>> b 3 >>> a, *b, c = [1, 2, 3, 4] >>> a 1 >>> b [2, 3] >>> c 4 >>> X 0 >>> X += 1 >>> X 1 >>> X = X + 1 >>> X 2 >>> X = 3 >>> while X > 0: print(X) X -= 1 3 2 1 >>> L = [1, 2] >>> M = [3, 4] >>> L += M >>> L [1, 2, 3, 4] >>> L.extend(M) >>> L [1, 2, 3, 4, 3, 4] >>> >>> >>> L = [] >>> L.append(22) >>> L [22] >>> >>> L = L.append(33) >>> print(L) None >>> L >>> L = [] >>> print(L.append(44)) None >>> L [44] >>> D = {'a': 1, 'b': 2, 'c': 3} >>> D {'a': 1, 'b': 2, 'c': 3} >>> >>> D.keys() dict_keys(['a', 'b', 'c']) >>> >>> ks = D.keys() >>> ks = list(ks) >>> ks.sort() >>> ks ['a', 'b', 'c'] >>> >>> ks = D.keys().sort() KeyboardInterrupt >>> >>> >>> >>> D {'a': 1, 'b': 2, 'c': 3} >>> for key in D.keys(): print(key, '=>', D[key]) a => 1 b => 2 c => 3 >>> ord('s') 115 >>> >>> L [44] >>> >>> >>> L.sort(reverse=True) >>> >>> L.sort() >>> L.reverse() >>> >>> print L SyntaxError: invalid syntax >>> print(L) [44] >>> >>> >>> D {'a': 1, 'b': 2, 'c': 3} >>> >>> D.values() dict_values([1, 2, 3]) >>> Vs = D.values() >>> Vs = list(Vs) >>> Vs.sort() >>> Vs [1, 2, 3] >>> >>> L = [1, 'a', 3.14, 'b'] >>> L.sort() Traceback (most recent call last): File "", line 1, in L.sort() TypeError: unorderable types: str() < int() >>> >>> >>> >>> print(1, 2, 3) 1 2 3 >>> >>> print(1, 2, 3, sep='++') 1++2++3 >>> >>> print(1, 2, 3, sep='++', end='???') 1++2++3??? >>> print(1, 2, 3, sep='++', end='???'); print('more') 1++2++3???more >>> >>> print(1, 2, 3, sep='++', file=open('output', 'w')) >>> >>> open('output').read() '1++2++3\n' >>> print(1, 2, 3, sep='++', file=open('output', 'w'), flush=True) >>> >>> print('%s = %e' % ('spam', 3.1415)) spam = 3.141500e+00 >>> >>> print('%s = %+.2e' % ('spam', 3.1415)) spam = +3.14e+00 >>> print('%s = %0.2e' % ('spam', 3.1415)) spam = 3.14e+00 >>> >>> print('%s = %010.2e' % ('spam', 3.1415)) spam = 003.14e+00 >>> >>> >>> import docstrings >>> >>> docstrings.__doc__ '\nModule documentation\nWords Go Here\n' >>> print(docstrings.__doc__) Module documentation Words Go Here >>> print(docstrings.square.__doc__) function documentation can we have your liver then? >>> print(docstrings.MyClass.__doc__) things about my class >>> help(docstrings) Help on module docstrings: NAME docstrings DESCRIPTION Module documentation Words Go Here CLASSES builtins.object MyClass class MyClass(builtins.object) | things about my class | | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | x = 1 FUNCTIONS square(x) function documentation can we have your liver then? DATA spam = 40 FILE c:\class\docstrings.py >>> >>> help(docstrings.square) Help on function square in module docstrings: square(x) function documentation can we have your liver then? >>> ================================ RESTART ================================ >>> >>> ================================ RESTART ================================ >>> >>> >>> >>> x, y, z = 1, 2, 3 >>> >>> if x < y and y < z: print('yes') yes >>> if x < y or y > z: print('yes') yes >>> if x < y or not y > z: print('yes') yes >>> >>> if f1() or f2(): KeyboardInterrupt >>> >>> x = f1(); y = f2() KeyboardInterrupt >>> x ^ y 3 >>> >>> >>> >>> file = open('script1.py') >>> file.readline() '# a first script\n' >>> file.readline() 'import os\n' >>> file.readline() 'print(os.getcwd())\n' >>> file.readline() 'print(2 ** 100)\n' >>> file.readline() "x = 'Spam'\n" >>> file.readline() 'print(x * 8)\n' >>> file.readline() '' >>> >>> file = open('script1.py') >>> file.__next__() '# a first script\n' >>> file.__next__() 'import os\n' >>> file.__next__() 'print(os.getcwd())\n' >>> file.__next__() 'print(2 ** 100)\n' >>> file.__next__() "x = 'Spam'\n" >>> file.__next__() 'print(x * 8)\n' >>> file.__next__() Traceback (most recent call last): File "", line 1, in file.__next__() StopIteration >>> >>> >>> for line in open('script1.py'): print(line.upper(), end='') # A FIRST SCRIPT IMPORT OS PRINT(OS.GETCWD()) PRINT(2 ** 100) X = 'SPAM' PRINT(X * 8) >>> >>> for line in open('script1.py').readlines(): print(line.upper(), end='') KeyboardInterrupt >>> >>> >>> res = [line.rstrip() for line in open('script1.py')] >>> res ['# a first script', 'import os', 'print(os.getcwd())', 'print(2 ** 100)', "x = 'Spam'", 'print(x * 8)'] >>> >>> res = [] >>> for line in open('script1.py'): res.append(line.rstrip()) >>> res ['# a first script', 'import os', 'print(os.getcwd())', 'print(2 ** 100)', "x = 'Spam'", 'print(x * 8)'] >>> >>> res = [line.rstrip() for line in open('script1.py') if line[0] == 'p'] >>> res ['print(os.getcwd())', 'print(2 ** 100)', 'print(x * 8)'] >>> >>> res = [] >>> for line in open('script1.py'): if line[0] == 'p': res.append(line.rstrip()) >>> res ['print(os.getcwd())', 'print(2 ** 100)', 'print(x * 8)'] >>> >>> [x + y for x in 'abc' for y in 'lmn'] ['al', 'am', 'an', 'bl', 'bm', 'bn', 'cl', 'cm', 'cn'] >>> >>> res = [] >>> for x in 'abc': for y in 'lmn': res.append(x + y) >>> res ['al', 'am', 'an', 'bl', 'bm', 'bn', 'cl', 'cm', 'cn'] >>> >>> map(ord, 'spam') >>> list(map(ord, 'spam')) [115, 112, 97, 109] >>> >>> M = map(ord, 'spam') >>> M >>> >>> M.__next__() 115 >>> M.__next__() 112 >>> M.__next__() 97 >>> M.__next__() 109 >>> M.__next__() Traceback (most recent call last): File "", line 1, in M.__next__() StopIteration >>> >>> for c in map(ord, 'spam'): print(c ** 2) 13225 12544 9409 11881 >>> M = map(ord, 'spam') >>> next(M) 115 >>> next(M) 112 >>> next(M) 97 >>> next(M) 109 >>> next(M) Traceback (most recent call last): File "", line 1, in next(M) StopIteration >>> >>> map(ord, 'spam') >>> >>> list(map(ord, 'spam')) [115, 112, 97, 109] >>> >>> [ord(c) for c in 'spam'] [115, 112, 97, 109] >>> >>> >>> [x ** 2 for x in [1, 2, 3, 4]] [1, 4, 9, 16] >>> >>> >>> def f(X): return X ** 2 >>> list(map(f, [1, 2, 3, 4])) [1, 4, 9, 16] >>> [x ** 2 for x in [1, 2, 3, 4]] [1, 4, 9, 16] >>> >>> >>> D Traceback (most recent call last): File "", line 1, in D NameError: name 'D' is not defined >>> >>> D = dict(a=1, b=2, c=3) >>> D {'b': 2, 'c': 3, 'a': 1} >>> >>> for key in D: print(key, D[key]) b 2 c 3 a 1 >>> D {'b': 2, 'c': 3, 'a': 1} >>> D.__next__() Traceback (most recent call last): File "", line 1, in D.__next__() AttributeError: 'dict' object has no attribute '__next__' >>> >>> I = iter(D) >>> I.__next__() 'b' >>> I.__next__() 'c' >>> I.__next__() 'a' >>> I.__next__() Traceback (most recent call last): File "", line 1, in I.__next__() StopIteration >>> >>> >>> L = [x ** 2 for x in [1, 2, 3, 4]] >>> L [1, 4, 9, 16] >>> >>> L = (x ** 2 for x in [1, 2, 3, 4]) >>> L at 0x0000000003350B88> >>> >>> I = iter(L) >>> I.__next__() 1 >>> I.__next__() 4 >>> I.__next__() 9 >>> I.__next__() 16 >>> I.__next__() Traceback (most recent call last): File "", line 1, in I.__next__() StopIteration >>> >>> for n in (x ** 2 for x in [1, 2, 3, 4]): print(n * 10) 10 40 90 160 >>> >>> {ord(c) for c in 'sppaaaammmmmm'} {112, 97, 115, 109} >>> >>> {c: ord(c) for c in 'sppaaammmmm'} {'s': 115, 'p': 112, 'a': 97, 'm': 109} >>> >>> range(50) range(0, 50) >>> list(range(50)) [0, 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] >>> >>> >>> L = [1, 2, 3, 4, 5] >>> >>> for x in L: x += 10 >>> x 15 >>> L [1, 2, 3, 4, 5] >>> >>> L = [1, 2, 3, 4, 5] >>> for i in range(len(L)): L[i] += 10 >>> L [11, 12, 13, 14, 15] >>> >>> >>> L [11, 12, 13, 14, 15] >>> E = enumerate(L) >>> >>> E.__next__() (0, 11) >>> E.__next__() (1, 12) >>> E.__next__() (2, 13) >>> E.__next__() (3, 14) >>> E.__next__() (4, 15) >>> E.__next__() Traceback (most recent call last): File "", line 1, in E.__next__() StopIteration >>> >>> >>> >>> >>> list(zip('abc', 'def', 'ghi', 'jkl')) [('a', 'd', 'g', 'j'), ('b', 'e', 'h', 'k'), ('c', 'f', 'i', 'l')] >>> >>> list(zip('abc', 'def', 'ghi', 'jklxxx')) [('a', 'd', 'g', 'j'), ('b', 'e', 'h', 'k'), ('c', 'f', 'i', 'l')] >>> >>> >>> [x ** 2 for x in range(5)] [0, 1, 4, 9, 16] >>> list((x ** 2 for x in range(5))) [0, 1, 4, 9, 16] >>> {x ** 2 for x in range(5)} {0, 1, 4, 16, 9} >>> {x: x ** 2 for x in range(5)} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16} >>> >>> [ord(c) for c in 'spam'] [115, 112, 97, 109] >>> list((ord(c) for c in 'spam')) [115, 112, 97, 109] >>> {ord(c) for c in 'spam'} {112, 97, 115, 109} >>> {c: ord(c) for c in 'spam'} {'s': 115, 'p': 112, 'a': 97, 'm': 109} >>> ================================ RESTART ================================ >>> 66 66 >>> ================================ RESTART ================================ >>> 66 66 >>> ================================ RESTART ================================ >>> 77 66 >>> ================================ RESTART ================================ >>> 77 77 >>> len = 99 >>> len 99 >>> del len >>> len >>> >>> import builtins >>> dir(builtins) ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip'] >>> True = False SyntaxError: assignment to keyword >>> def open(...): SyntaxError: invalid syntax >>> >>> def open(args): ... >>> builtins.open = open KeyboardInterrupt >>> >>> >>> >>> def maker(N): def action(X): return X ** N return action >>> f = maker(3) >>> f .action at 0x00000000034BA488> >>> >>> f(2) 8 >>> g = maker(4) >>> g .action at 0x00000000034BA510> >>> g(2) 16 >>> >>> f(2) 8 >>> h = maker(100) >>> h(2) 1267650600228229401496703205376 >>> >>> f(2) 8 >>> g(2) 16 >>> f(3) 27 >>> h(3) 515377520732011331036461129765621272702107522001 >>> >>> import dis >>> dir(h) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] >>> >>> changer(X, L[:]) KeyboardInterrupt >>> >>> >>> >>> def f(a, *pargs, **kargs): print(a, pargs, kargs) >>> f(1) 1 () {} >>> f(1, 2, 3, 4) 1 (2, 3, 4) {} >>> f(1, a=1, b=2, c=3, d=4) Traceback (most recent call last): File "", line 1, in f(1, a=1, b=2, c=3, d=4) TypeError: f() got multiple values for argument 'a' >>> >>> f(1, b=2, c=3, d=4) 1 () {'d': 4, 'b': 2, 'c': 3} >>> >>> f(1, b=2, c=3, d=4, 9) SyntaxError: non-keyword arg after keyword arg >>> f(b=2, c=3, d=4) Traceback (most recent call last): File "", line 1, in f(b=2, c=3, d=4) TypeError: f() missing 1 required positional argument: 'a' >>> >>> f(a=1, b=2, c=3, d=4) 1 () {'d': 4, 'b': 2, 'c': 3} >>> >>> f(1, 2, 3, 4) 1 (2, 3, 4) {} >>> >>> >>> >>> def min(*args): low = args[0] for arg in args[1:]: if arg < low: low = arg return low >>> min(1, 4, 3, 2, 5, 6) 1 >>> min('bb', 'aa', 'dd', 'cc') 'aa' >>> >>> sum([1, 3, 2, 4, 5]) 15 >>> 15 15 >>> >>> >>> >>> >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> sum(sum(row) for row in M) 45 >>> ================================ RESTART ================================ >>> 6 >>> ================================ RESTART ================================ >>> 6 >>> import func1 >>> func1.f(4) 8 >>> 6 6 >>> >>> S = 'spam' >>> S + 'NI' 'spamNI' >>> S.__add__('NI') 'spamNI' >>> >>> fname = 'script1.py' >>> len(open(fname).read()) 86 >>> len(open(fname).readlines()) 6 >>> >>> tot = 0 >>> for line in open(fname): tot += 1 >>> tot 6 >>> >>> import os >>> os.chdir(r'C:\somedir') KeyboardInterrupt >>> >>> >>> import docstrings >>> dir(docstrings) ['MyClass', '__builtins__', '__cached__', '__doc__', '__file__', '__initializing__', '__loader__', '__name__', '__package__', 'spam', 'square'] >>> >>> docstrings.__dict__.keys() dict_keys(['__loader__', '__doc__', '__file__', 'spam', '__builtins__', '__initializing__', '__package__', '__name__', 'square', '__cached__', 'MyClass']) >>> >>> docstrings.__dict__ >>> >>> >>> >>> >>> ================================ RESTART ================================ >>> Bob Smith Sue Jones 100000 >>> name = 'Bob Smith' >>> name.split() ['Bob', 'Smith'] >>> name.split()[-1] 'Smith' >>> ================================ RESTART ================================ >>> Bob Smith Sue Jones 100000 Smith 110000.00000000001 >>> ================================ RESTART ================================ >>> Bob Smith Sue Jones 100000 Smith 110000 >>> ================================ RESTART ================================ >>> Bob Smith Sue Jones 100000 Smith Jones 110000 >>> ================================ RESTART ================================ >>> Bob Smith Sue Jones 100000 Smith Jones 110000 <__main__.Person object at 0x000000000342D390> >>> ================================ RESTART ================================ >>> [Person: Bob Smith, 0.00] [Person: Sue Jones, 100000.00] Smith Jones [Person: Sue Jones, 110000.00] >>> ================================ RESTART ================================ >>> [Person: Bob Smith, 0.00] [Person: Sue Jones, 100000.00] Smith Jones [Person: Sue Jones, 110000.00] [Person: Tom Jones, 50000.00] [Person: Bob Smith, 0.00] [Person: Sue Jones, 121000.00] [Person: Tom Jones, 60000.00] >>> ================================ RESTART ================================ >>> [Person: Bob Smith, 0.00] [Person: Sue Jones, 100000.00] Smith Jones [Person: Sue Jones, 110000.00] [Person: Tom Jones, 50000.00] [Person: Bob Smith, 0.00] [Person: Sue Jones, 121000.00] [Person: Tom Jones, 60000.00] >>> ================================ RESTART ================================ >>> >>> import os >>> os.listdir('.') ['data.bin', 'data.pkl', 'data.txt', 'docstrings.py', 'func1.py', 'interact.py', 'ksc.db.bak', 'ksc.db.dat', 'ksc.db.dir', 'makedb.py', 'output', 'person.py', 'prior', 'scopes.py', 'script1.py', 'stub.py', '__pycache__'] >>> ================================ RESTART ================================ >>> sue => [Person: Sue Jones, 100000.00] bob => [Person: Bob Smith, 0.00] tom => [Person: Tom Jones, 50000.00] Smith None >>> ================================ RESTART ================================ >>> tom => [Person: Tom Jones, 50000.00] sue => [Person: Sue Jones, 110000.00] bob => [Person: Bob Smith, 0.00] Smith None >>> ================================ RESTART ================================ >>> sue => [Person: Sue Jones, 121000.00] tom => [Person: Tom Jones, 50000.00] bob => [Person: Bob Smith, 0.00] Smith None >>> ================================ RESTART ================================ >>> bob => [Person: Bob Smith, 0.00] sue => [Person: Sue Jones, 133100.00] tom => [Person: Tom Jones, 50000.00] Smith None >>> ================================ RESTART ================================ >>> [Person: Bob Smith, 0.00] [Person: Sue Jones, 100000.00] Smith Jones [Person: Sue Jones, 110000.00] [Person: Tom Jones, 50000.00] [Person: Bob Smith, 0.00] [Person: Sue Jones, 121000.00] [Person: Tom Jones, 60000.00] >>> >>> >>> from imp import reload >>> reload(docstrings) Traceback (most recent call last): File "", line 1, in reload(docstrings) NameError: name 'docstrings' is not defined >>> >>> import docstrings >>> >>> from imp import reload >>> reload(docstrings) >>> >>> >>> >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those! >>> >>> >>> import os >>> dir(os) ... >>> >>> len(dir(os)) 145 >>> import sys >>> dir(sys) ... >>>