1. 类型 1.1 基础类型 Python中基础类型包含整数、浮点数、布尔值、字符串、元组。
1 2 3 4 5 6 a = 10 b = 10000000000 c = 1.5 flag = True s = "abc" print (s)
Python中的整数不同于C/Java,Python的整数没有溢出,底层被视为动态长度的 C 语言结构体。
Python使用变量时无需声明类型,但其也是强类型语言,进行跨类型操作时,如果不显式转换,会提示TypeError。
1.2 列表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 nums = [1 ,2 ,3 ] nums = [i for i in range (10 )] nums = [0 ] * 10 nums[0 ]len (nums) nums.append(4 ) nums.pop(0 ) nums.remove(0 )if n in nums: pass
1.3 for循环 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 for i in range (n): print (i)for n in nums: print (n)for i, n in enumerate (nums): print (i,num)for a, b in zip (nums1, nums2): print (a ,b)
1.4 字符串 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 s="abc" s[0 ]len (s)for c in s: s1 = [] s1.append("b" ) s1.append("a" ) s2 = "," .join(s1)print (s2)if 'a' in s:
1.5 字典 1 2 3 4 5 6 7 8 9 10 11 12 13 14 map ={}map [1 ]=100 map [1 ]1 in map c = s.setdefault(1 ,2 )
1.6 集合set 1 2 3 4 5 6 7 8 9 10 11 set =set ()set .add(1 )1 in set set .remove(1 )
1.7 栈stack 1 2 3 4 5 6 7 stack=[] stack.append(1 ) stack.pop() stack[-1 ]
1.8 双向队列deque 1 2 3 4 5 6 7 8 9 10 11 12 13 from collections import deque q=deque() q.append(1 ) q.popleft() q.appendleft(2 ) q.pop()print (q)
1.9 堆(优先队列)heap 默认小根堆
1 2 3 4 5 6 7 import heapq heap=[] heapq.heappush(heap,3 ) heapq.heappop(heap)
使用大根堆
1.10 类和函数 Python 使用 def 定义函数,使用 class 定义类。self 代表实例本身,必须显式写在参数列表中。
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 def add (a, b ): return a + bdef greet (name, msg="Hello" ): return f"{msg} , {name} " class Person : species = "Human" def __init__ (self, name, age ): self .name = name self .age = age def introduce (self ): return f"I am {self.name} , {self.age} years old." def __repr__ (self ): return f"Person(name={self.name!r} , age={self.age!r} )" def __str__ (self ): return f"{self.name} , {self.age} " @staticmethod def is_adult (age ): return age >= 18 @classmethod def from_birth_year (cls, name, birth_year ): return cls(name, 2026 - birth_year)class Student (Person ): def __init__ (self, name, age, school ): super ().__init__(name, age) self .school = school def introduce (self ): return f"{super ().introduce()} I study at {self.school} ." p = Person("Alice" , 25 )print (p.introduce()) print (p) print (repr (p)) print (Person.is_adult(20 ))
1.11 二维数组 二维数组通常用嵌套列表表示。注意初始化时的浅拷贝陷阱 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 m, n = 3 , 4 matrix = [[0 ] * n for _ in range (m)] matrix[0 ][1 ] = 5 for i in range (len (matrix)): for j in range (len (matrix[0 ])): print (matrix[i][j], end=" " ) print ()for row in matrix: for val in row: print (val, end=" " )
1.12 排序 list.sort() 原地排序,sorted() 返回新列表。使用 key 参数指定排序依据,reverse=True 降序。
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 nums = [3 , 1 , 4 , 1 , 5 , 9 ] nums.sort()print (nums) sorted_nums = sorted (nums, reverse=True )print (sorted_nums) pairs = [(1 , 3 ), (2 , 1 ), (3 , 2 )] pairs.sort(key=lambda x: x[1 ]) print (pairs) pairs.sort(key=lambda x: (x[0 ], x[1 ])) words = ["apple" , "kiwi" , "banana" , "pear" ] words.sort(key=len )print (words) from functools import cmp_to_keydef compare (a, b ): return (a > b) - (a < b) nums.sort(key=cmp_to_key(compare))
1.13 Lambda Lambda 是匿名函数,语法为 lambda 参数: 表达式。常用于 map、filter、sorted 等需要简短回调的场景。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 add = lambda a, b: a + bprint (add(1 , 2 )) nums = [1 , 2 , 3 , 4 , 5 ] squared = list (map (lambda x: x ** 2 , nums))print (squared) evens = list (filter (lambda x: x % 2 == 0 , nums))print (evens) pairs = [(1 , 3 ), (2 , 1 ), (3 , 2 )] pairs.sort(key=lambda x: x[1 ])
1.14 切片 Slice 切片语法 [start:stop:step],start 默认 0,stop 默认末尾,step 默认 1。切片返回新对象 (浅拷贝)。
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 nums = [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]print (nums[2 :5 ]) print (nums[:5 ]) print (nums[5 :]) print (nums[:]) print (nums[::2 ]) print (nums[1 ::2 ]) print (nums[::-1 ]) print (nums[::-2 ]) s = "Hello, World!" print (s[::-1 ]) take_every_two = slice (None , None , 2 )print (nums[take_every_two])
1.15 列表推导式 [表达式 for 变量 in 可迭代对象 if 条件]。比 for 循环更简洁高效。
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 squares = [i ** 2 for i in range (10 )]print (squares) evens = [i for i in range (20 ) if i % 2 == 0 ]print (evens) labels = ["even" if x % 2 == 0 else "odd" for x in range (6 )]print (labels) pairs = [(i, j) for i in range (3 ) for j in range (2 )]print (pairs) matrix = [[i + j for j in range (3 )] for i in range (3 )]print (matrix) flat = [x for row in matrix for x in row]print (flat) gen = (i ** 2 for i in range (10 ** 6 )) print (next (gen))
1.16 字典推导式 {key: value for 变量 in 可迭代对象 if 条件}。快速构建或转换字典。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 square_map = {i: i ** 2 for i in range (6 )}print (square_map) even_squares = {i: i ** 2 for i in range (10 ) if i % 2 == 0 }print (even_squares) d = {"a" : 1 , "b" : 2 , "c" : 3 } reversed_d = {v: k for k, v in d.items()}print (reversed_d) keys = ["name" , "age" , "city" ] values = ["Bob" , 30 , "NYC" ] d = {k: v for k, v in zip (keys, values)}print (d) s = "hello world" freq = {c: s.count(c) for c in set (s) if c != ' ' }print (freq)
1.17 元组 元组是不可变序列。创建后不能修改元素,但可以包含可变对象。
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 t = (1 , 2 , 3 ) t = 1 , 2 , 3 t = tuple ([1 , 2 , 3 ]) single = (1 ,) not_a_tuple = (1 ) empty = () empty = tuple () t = (1 , 2 , 3 , 4 , 5 )print (t[0 ]) print (t[1 :3 ]) t = ([1 , 2 ], 3 ) t[0 ].append(3 )print (t) from collections import namedtuple Point = namedtuple("Point" , ["x" , "y" ]) p = Point(1 , 2 )print (p.x, p.y) print (p[0 ], p[1 ])
1.18 多变量交换 利用元组打包/解包机制,一行交换两个变量的值,无需临时变量。
1 2 3 4 5 6 7 8 9 10 11 a, b = 1 , 2 a, b = b, aprint (a, b) x, y, z = 1 , 2 , 3 x, y, z = z, y, xprint (x, y, z)
1.19 解包 将可迭代对象的元素分别赋值给多个变量。* 可以收集剩余元素。
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 a, b, c = [1 , 2 , 3 ]print (a, b, c) first, *rest = [1 , 2 , 3 , 4 , 5 ]print (first) print (rest) *beginning, last = [1 , 2 , 3 , 4 , 5 ]print (last) print (beginning) first, *middle, last = [1 , 2 , 3 , 4 , 5 ]print (middle) _, _, third, *rest = [1 , 2 , 3 , 4 , 5 ]print (third) def func (a, b, c ): return a + b + c args = [1 , 2 , 3 ]print (func(*args)) kwargs = {"a" : 1 , "b" : 2 , "c" : 3 }print (func(**kwargs)) def log (*args, **kwargs ): print ("args:" , args) print ("kwargs:" , kwargs) log(1 , 2 , name="alice" , age=25 )
1.20 any/all any(iterable) 任一元素为真则返回 True。all(iterable) 所有元素为真才返回 True。都支持短路求值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 print (any ([False , False , True ])) print (any ([False , False , False ])) print (any ([])) print (all ([True , True , False ])) print (all ([True , True , True ])) print (all ([])) nums = [2 , 4 , 6 , 8 ]print (all (n % 2 == 0 for n in nums)) nums = [1 , 3 , 5 , 7 , 8 ]print (any (n % 2 == 0 for n in nums))
1.21 字符串高级操作 Python 字符串提供了丰富的内置方法,常用于文本处理和算法题中。
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 s = " Hello, World! " print (s.strip()) print (s.lstrip()) print (s.rstrip()) s = "a,b,c,d" parts = s.split("," ) print ("," .join(parts)) s = "one two three" print (s.split()) s = "Hello, World!" print (s.find("World" )) print (s.index("World" )) print (s.rfind("o" )) print (s.count("l" )) print (s.startswith("Hello" )) print (s.endswith("!" )) s = "apple, apple, orange" print (s.replace("apple" , "banana" )) print (s.replace("apple" , "banana" , 1 )) s = "Hello World" print (s.lower()) print (s.upper()) print (s.title()) print (s.swapcase()) print ("123" .isdigit()) print ("abc" .isalpha()) print ("abc123" .isalnum()) print (" " .isspace()) name, age = "Alice" , 25 print (f"My name is {name} , I am {age} years old." )print (f"Next year I will be {age + 1 } ." )print (f"PI is approximately {3.14159 :.2 f} " ) print (f"{255 :#x} " ) print (f"{1000 :,} " ) print ("{} + {} = {}" .format (1 , 2 , 3 )) print ("{1} + {0} = {2}" .format (1 , 2 , 3 )) print ("{a} + {b} = {c}" .format (a=1 , b=2 , c=3 ))
1.22 Counter统计 collections.Counter 是 dict 的子类,用于统计可哈希对象的出现次数。
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 from collections import Counter cnt = Counter("abracadabra" )print (cnt) cnt = Counter([1 , 2 , 1 , 3 , 2 , 1 ])print (cnt) cnt = Counter(a=3 , b=1 , c=2 )print (cnt) print (cnt.most_common(2 )) a = Counter(a=3 , b=1 ) b = Counter(a=1 , b=2 , c=1 )print (a + b) print (a - b) print (a & b) print (a | b) cnt.update("aaa" )print (cnt["a" ]) print (dict (cnt))
1.23 defaultdict collections.defaultdict 在访问不存在的 key 时自动生成默认值,避免 KeyError。
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 from collections import defaultdict cnt = defaultdict(int ) words = ["apple" , "banana" , "apple" , "cherry" ]for w in words: cnt[w] += 1 print (dict (cnt)) groups = defaultdict(list ) items = [("a" , 1 ), ("b" , 2 ), ("a" , 3 ), ("b" , 4 ), ("a" , 5 )]for key, val in items: groups[key].append(val)print (dict (groups)) seen = defaultdict(set ) pairs = [(1 , "a" ), (1 , "b" ), (1 , "a" ), (2 , "c" )]for k, v in pairs: seen[k].add(v)print (dict (seen)) tree = defaultdict(lambda : None )
1.24 双端队列(进阶) 在 1.8 节基础上,deque 还支持限制长度和旋转操作。
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 from collections import deque dq = deque(maxlen=3 ) dq.append(1 ) dq.append(2 ) dq.append(3 ) dq.append(4 ) print (dq) dq = deque([1 , 2 , 3 , 4 , 5 ]) dq.rotate(1 ) print (dq) dq.rotate(-2 ) print (dq) dq = deque([1 , 2 ]) dq.extend([3 , 4 ])print (dq) dq.extendleft([0 , -1 ]) print (dq)print (dq[0 ], dq[-1 ]) dq = deque([1 , 2 , 1 , 3 , 1 ])print (dq.count(1 ))
1.25 链表定义 算法题中链表的常用定义和操作模板。
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 class ListNode : def __init__ (self, val=0 , next =None ): self .val = val self .next = next def build_list (values ): dummy = ListNode() cur = dummy for v in values: cur.next = ListNode(v) cur = cur.next return dummy.next head = build_list([1 , 2 , 3 , 4 , 5 ])def traverse (head ): cur = head while cur: print (cur.val, end=" -> " ) cur = cur.next print ("None" )def reverse_list (head ): prev = None cur = head while cur: nxt = cur.next cur.next = prev prev = cur cur = nxt return prevdef reverse_list_recursive (head ): if not head or not head.next : return head new_head = reverse_list_recursive(head.next ) head.next .next = head head.next = None return new_headdef find_middle (head ): slow = fast = head while fast and fast.next : slow = slow.next fast = fast.next .next return slowdef has_cycle (head ): slow = fast = head while fast and fast.next : slow = slow.next fast = fast.next .next if slow == fast: return True return False
1.26 二叉树定义 算法题中二叉树的常用定义和遍历模板。
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 class TreeNode : def __init__ (self, val=0 , left=None , right=None ): self .val = val self .left = left self .right = rightdef preorder (root ): if not root: return [] return [root.val] + preorder(root.left) + preorder(root.right)def inorder (root ): if not root: return [] return inorder(root.left) + [root.val] + inorder(root.right)def postorder (root ): if not root: return [] return postorder(root.left) + postorder(root.right) + [root.val]def inorder_iter (root ): res, stack = [], [] cur = root while cur or stack: while cur: stack.append(cur) cur = cur.left cur = stack.pop() res.append(cur.val) cur = cur.right return resfrom collections import dequedef level_order (root ): if not root: return [] res, q = [], deque([root]) while q: level = [] for _ in range (len (q)): node = q.popleft() level.append(node.val) if node.left: q.append(node.left) if node.right: q.append(node.right) res.append(level) return res
1.27 图结构 图通常用邻接表(dict + list)或邻接矩阵表示。
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 from collections import deque, defaultdict graph = { 0 : [1 , 2 ], 1 : [0 , 2 , 3 ], 2 : [0 , 1 ], 3 : [1 ] } edges = [(0 , 1 ), (0 , 2 ), (1 , 2 ), (1 , 3 )] graph = defaultdict(list )for u, v in edges: graph[u].append(v) graph[v].append(u) def dfs (graph, node, visited=None ): if visited is None : visited = set () visited.add(node) print (node, end=" " ) for neighbor in graph[node]: if neighbor not in visited: dfs(graph, neighbor, visited)def bfs (graph, start ): visited = set ([start]) q = deque([start]) while q: node = q.popleft() print (node, end=" " ) for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) q.append(neighbor)def topological_sort (n, edges ): graph = defaultdict(list ) indegree = [0 ] * n for u, v in edges: graph[u].append(v) indegree[v] += 1 q = deque([i for i in range (n) if indegree[i] == 0 ]) res = [] while q: u = q.popleft() res.append(u) for v in graph[u]: indegree[v] -= 1 if indegree[v] == 0 : q.append(v) return res if len (res) == n else []
1.28 位运算 Python 位运算符:&(与)、|(或)、^(异或)、~(取反)、<<(左移)、>>(右移)。
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 a, b = 5 , 3 print (a & b) print (a | b) print (a ^ b) print (~a) print (a << 1 ) print (a >> 1 ) n = 42 print (n & 1 ) print (n & -n) print (n & (n - 1 )) def is_power_of_two (n ): return n > 0 and (n & (n - 1 )) == 0 def count_ones (n ): cnt = 0 while n: n &= n - 1 cnt += 1 return cnt x, y = 1 , 2 x ^= y y ^= x x ^= yprint (x, y) mask = 0b1101 sub = maskwhile sub: print (bin (sub)) sub = (sub - 1 ) & mask
1.29 math库 Python 的 math 模块提供了常用的数学函数和常量。
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 import mathprint (math.pi) print (math.e) print (math.inf) print (-math.inf) print (math.nan) print (math.ceil(3.2 )) print (math.floor(3.8 )) print (math.trunc(-3.8 )) print (math.pow (2 , 10 )) print (math.sqrt(16 )) print (math.isqrt(10 )) print (math.gcd(12 , 18 )) print (math.lcm(12 , 18 )) print (math.comb(5 , 2 )) print (math.perm(5 , 2 )) print (math.factorial(5 )) print (math.log(8 , 2 )) print (math.log2(8 )) print (math.log10(100 )) print (math.sin(math.pi / 2 )) print (math.cos(math.pi)) print (math.degrees(math.pi)) print (math.radians(180 )) print (math.isclose(0.1 + 0.2 , 0.3 )) print (math.isinf(math.inf)) print (math.isnan(math.nan))
1.30 输入输出 算法竞赛和脚本中常用的输入输出方式。
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 import sys name = input ("Enter your name: " ) print ("Hello," , name) print ("No" , "newline" , end="" ) a, b, c = map (int , input ().split()) nums = list (map (int , input ().split()))print (nums) n = int (input ()) arr = [int (input ()) for _ in range (n)] n = int (input ()) arr = list (map (int , input ().split())) data = sys.stdin.read().split()for line in sys.stdin: line = line.strip() if not line: break import sys n = int (sys.stdin.readline()) arr = list (map (int , sys.stdin.readline().split()))print (*arr) sys.stdout.write(" " .join(map (str , arr)) + "\n" )
1.31 深拷贝 copy.copy 做浅拷贝(只复制一层),copy.deepcopy 做深拷贝(递归复制所有层)。
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 import copy a = [1 , 2 , 3 ] b = copy.copy(a) c = copy.deepcopy(a) b[0 ] = 99 print (a) a = [[1 , 2 ], [3 , 4 ]] b = copy.copy(a) b[0 ][0 ] = 99 print (a) a = [[1 , 2 ], [3 , 4 ]] c = copy.deepcopy(a) c[0 ][0 ] = 99 print (a) b = a[:] b = a.copy() b = list (a)
1.32 常见坑 Python 中几个容易踩的陷阱。
列表乘法(浅拷贝问题) 1 2 3 4 5 6 7 8 9 grid = [[0 ] * 3 ] * 3 grid[0 ][0 ] = 1 print (grid) grid = [[0 ] * 3 for _ in range (3 )] grid[0 ][0 ] = 1 print (grid)
默认参数(可变对象陷阱) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def append_to (element, lst=[] ): lst.append(element) return lstprint (append_to(1 )) print (append_to(2 )) def append_to (element, lst=None ): if lst is None : lst = [] lst.append(element) return lst
列表内遍历删除 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 nums = [1 , 2 , 3 , 4 , 5 ]for n in nums: if n % 2 == 0 : nums.remove(n)print (nums) nums = [1 , 2 , 3 , 4 , 5 ] nums = [n for n in nums if n % 2 != 0 ] nums = [1 , 2 , 3 , 4 , 5 ]for i in range (len (nums) - 1 , -1 , -1 ): if nums[i] % 2 == 0 : nums.pop(i)
2. 对象类型 2.1 不可变对象 不可变对象(int、float、str、tuple、frozenset、bool)创建后值不能修改 。任何”修改”操作实际上是创建了新对象。
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 a = 10 print (id (a)) a += 1 print (id (a)) s = "hello" print (id (s)) s += " world" print (id (s)) a = 256 b = 256 print (a == b) print (a is b) a = 257 b = 257 print (a == b) print (a is b) s1 = "hello_world" s2 = "hello_world" print (s1 is s2) t = (1 , 2 , [3 , 4 ]) t[2 ].append(5 ) print (t) a = 1 print (id (a)) a += 1 print (id (a))
2.2 可变对象 可变对象(list、dict、set、bytearray)可以在原地修改值,id() 保持不变。
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 nums = [1 , 2 , 3 ]print (id (nums)) nums.append(4 ) nums[0 ] = 99 print (id (nums)) nums = [1 , 2 , 3 ]print (id (nums)) nums += [4 , 5 ] print (id (nums)) def modify_list (lst ): lst.append(4 ) def reassign_list (lst ): lst = lst + [4 ] def modify_int (n ): n += 1 a = [1 , 2 , 3 ] modify_list(a)print (a) b = [1 , 2 , 3 ] reassign_list(b)print (b) c = 10 modify_int(c)print (c) def bad_append (item, target=[] ): target.append(item) return targetprint (bad_append(1 )) print (bad_append(2 )) s = {(1 , 2 ), (3 , 4 )} d = {(1 , 2 ): "point" }