>>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium' >>> 'Py''thon' 'Python' >>> text = ('Put several strings within parentheses ' ... 'to have them joined together.') >>> text 'Put several strings within parentheses to have them joined together.' >>> word = 'Python' >>> word[42] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> word[4:42] 'on' >>> word[0] = 'J' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str'object does not support item assignment >>> 'J' + word[1:] 'Jython' >>> len(word) 6
>>> a = ['Mary', 'had', 'a', 'little', 'lamb'] >>> for i inrange(len(a)): ... print(i, a[i]) ... 0 Mary 1 had 2 a 3 little 4 lamb
for 和 while 循环可以有对应的 else 子句,如果循环在未执行 break 的情况下结束,则会执行该 else 子句。pass 语句不执行任何操作,当语法上需要一个语句,而程序不需要执行任何操作时,可以使用该语句。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
>>> for n inrange(2, 10): ... for x inrange(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... # loop fell through without finding a factor ... print(n, 'is a prime number') ... 2is a prime number 3is a prime number 4 equals 2 * 2 5is a prime number 6 equals 2 * 3 7is a prime number 8 equals 2 * 4 9 equals 3 * 3
1 2 3
>>> whileTrue: ... pass# Busy-wait for keyboard interrupt (Ctrl+C) ...
match 语句用于模式匹配,使用 _ 通配符匹配剩余情况,使用 | 将多个字面值组合到一个模式中。模式之后可以使用 if 子句,如果判断结果为假,则 match 会继续尝试匹配下一个 case 块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classPoint: __match_args__ = ('x', 'y') def__init__(self, x, y): self.x = x self.y = y
match points: case []: print("No points") case [Point(0, 0)]: print("The origin") case [Point(x, y)]: print(f"Single point {x}, {y}") case [Point(0, y1), Point(0, y2)]: print(f"Two on the Y axis at {y1}, {y2}") case _: print("Something else")
1 2 3 4 5
match point: case Point(x, y) if x == y: print(f"Y=X at {x}") case Point(x, y): print(f"Not on the diagonal")
>>> deffib(n): # write Fibonacci series less than n ... """Print a Fibonacci series less than n.""" ... a, b = 0, 1 ... while a < n: ... print(a, end=' ') ... a, b = b, a+b ... print() ... ... # Now call the function we just defined: ... fib(2000) ... 011235813213455891442333776109871597
>>> deff(a, L=None): ... if L isNone: ... L = [] ... L.append(a) ... return L ... ... print(f(1)) ... print(f(2)) ... print(f(3)) ... [1] [2] [3]
可以使用 *args 表示可变参数,该参数是元组类型。可以使用 * 和 ** 执行解包。
1 2 3 4 5
>>> list(range(3, 6)) # normal call with separate arguments [3, 4, 5] >>> args = [3, 6] >>> list(range(*args)) # call with arguments unpacked from a list [3, 4, 5]
1 2 3 4 5 6 7 8 9
>>> defparrot(voltage, state='a stiff', action='voom'): ... print("-- This parrot wouldn't", action, end=' ') ... print("if you put", voltage, "volts through it.", end=' ') ... print("E's", state, "!") ... ... d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} ... parrot(**d) ... -- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
列表推导式的方括号 [] 中包含以下内容:一个表达式,一个 for 子句,之后是零个或多个 for/if 子句。使用 zip() 函数在多个迭代器上并行迭代,每个迭代器返回一个元素组成元组。使用 del 语句删除列表元素。
1 2 3 4 5 6 7 8 9
>>> combs = [] ... for x in [1,2,3]: ... for y in [3,1,4]: ... if x != y: ... combs.append((x, y)) ... ... combs ... [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
1 2
>>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} >>> print(basket) # show that duplicates have been removed {'orange', 'banana', 'pear', 'apple'} >>> 'orange'in basket # fast membership testing True >>> 'crabgrass'in basket False
>>> # Demonstrate set operations on unique letters from two words >>> >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a {'a', 'r', 'b', 'c', 'd'} >>> a - b # letters in a but not in b {'r', 'd', 'b'} >>> a | b # letters in a or b or both {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} >>> a & b # letters in both a and b {'a', 'c'} >>> a ^ b # letters in a or b but not both {'r', 'd', 'b', 'm', 'z', 'l'}
字典的键必须是不可变类型,使用 {} 创建空字典,获取不存在键的值会报错。使用 list(d) 获取键列表,使用 sorted(d) 获取排序之后的键列表,使用 in 或 not in 判断键是否存在,使用 dict() 函数创建字典。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
>>> tel = {'jack': 4098, 'sape': 4139} >>> tel['guido'] = 4127 >>> tel {'jack': 4098, 'sape': 4139, 'guido': 4127} >>> tel['jack'] 4098 >>> del tel['sape'] >>> tel['irv'] = 4127 >>> tel {'jack': 4098, 'guido': 4127, 'irv': 4127} >>> list(tel) ['jack', 'guido', 'irv'] >>> sorted(tel) ['guido', 'irv', 'jack'] >>> 'guido'in tel True >>> 'jack'notin tel False
>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} ... for k, v in knights.items(): ... print(k, v) ... gallahad the pure robin the brave
1 2 3 4 5 6
>>> for i, v inenumerate(['tic', 'tac', 'toe']): ... print(i, v) ... 0 tic 1 tac 2 toe
1 2 3 4 5 6 7 8
>>> questions = ['name', 'quest', 'favorite color'] ... answers = ['lancelot', 'the holy grail', 'blue'] ... for q, a inzip(questions, answers): ... print('What is your {0}? It is {1}.'.format(q, a)) ... What is your name? It is lancelot. What is your quest? It is the holy grail. What is your favorite color? It is blue.
使用 is 或 is not 比较两个对象是否是同一个对象。比较运算支持链式操作,a < b == c 和 a < b and b == c 等价。and、or 和 not 表示与或非,相当于 Java 中的 &&、|| 和 !。布尔值有 True 和 False,使用大写字母开头。在表达式内部赋值需要使用 :=,避免误用 =。布尔运算中使用普通值而不是布尔值时,短路运算的返回值是最后一个已求值的参数。
1 2 3 4
>>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' >>> non_null = string1 or string2 or string3 >>> non_null 'Trondheim'
kind = 'canine'# class variable shared by all instances
def__init__(self, name): self.name = name # instance variable unique to each instance
>>> d = Dog('Fido') >>> e = Dog('Buddy') >>> d.kind # shared by all dogs 'canine' >>> e.kind # shared by all dogs 'canine' >>> d.name # unique to d 'Fido' >>> e.name # unique to e 'Buddy'
defupdate(self, iterable): for item in iterable: self.items_list.append(item)
__update = update # private copy of original update() method
classMappingSubclass(Mapping):
defupdate(self, keys, values): # provides new signature for update() # but does not break __init__() for item inzip(keys, values): self.items_list.append(item)