Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ #List operations >>> l = [1,2,3, "a"] >>> l[3] 'a' >>> l[-1] 'a' >>> l[-2] 3 >>> l[4] Traceback (most recent call last): File "", line 1, in l[4] IndexError: list index out of range >>> l[1:2] [2] >>> l[1:3] [2, 3] >>> l2 = [-2,5] >>> l + l2 [1, 2, 3, 'a', -2, 5] >>> l [1, 2, 3, 'a'] >>> l = l+l2 >>> l [1, 2, 3, 'a', -2, 5] >>> l = l + [["b"]] >>> l [1, 2, 3, 'a', -2, 5, ['b']] >>> type('a') >>> type("a") >>> l[-1][0] 'b' >>> l[-1] ['b'] >>> l[-1][0][0] 'b' >>> l[-1][0][1] Traceback (most recent call last): File "", line 1, in l[-1][0][1] IndexError: string index out of range >>> l = [1,2,3] >>> sum(l) 6 >>> l = [1,2,3, 'a'] >>> sum(l) Traceback (most recent call last): File "", line 1, in sum(l) TypeError: unsupported operand type(s) for +: 'int' and 'str' >>> len(l) 4 >>> l = [3,2,5,4] >>> l.sort() >>> l [2, 3, 4, 5] >>> l = [3,2,5,4] >>> sorted(l) [2, 3, 4, 5] >>> l [3, 2, 5, 4] >>> l = [1,2,3, 'a'] >>> sorted(l) Traceback (most recent call last): File "", line 1, in sorted(l) TypeError: unorderable types: str() < int() >>> l = ['b', 'a'] >>> sorted(l) ['a', 'b'] >>> l ['b', 'a'] >>> l.append('c') >>> l ['b', 'a', 'c'] >>> l.extend(['c']) >>> l ['b', 'a', 'c', 'c'] >>> l.append(['c']) >>> l ['b', 'a', 'c', 'c', ['c']] >>> l.extend('c') >>> l ['b', 'a', 'c', 'c', ['c'], 'c'] >>> l.extend(1) Traceback (most recent call last): File "", line 1, in l.extend(1) TypeError: 'int' object is not iterable >>> l.extend('c', 2) Traceback (most recent call last): File "", line 1, in l.extend('c', 2) TypeError: extend() takes exactly one argument (2 given) >>> >>> ================================ RESTART ================================ #Change lists >>> >>> lst = [1,2,3,4] >>> for i in range(len(lst)): lst[i] = 5 >>> lst [5, 5, 5, 5] >>> >>> lst = [1,2,3,4] >>> for e in lst: e = 5 >>> lst [1, 2, 3, 4] >>> ================================ RESTART ================================ #List comprehensions >>> >>> l = [1,2,3,4,5] >>> l2 = [2*x for x in l if x > 3] >>> l2 [8, 10] >>> l2 = [True for x in l if x > 3] >>> l2 [True, True] >>> l2 = [4 for x in l if x > 3] >>> l2 [4, 4] >>> ================================ RESTART ================================ #Short circuit evaluation >>> >>> True and 3//0 Traceback (most recent call last): File "", line 1, in True and 3//0 ZeroDivisionError: integer division or modulo by zero >>> False and 3//0 False >>>