字符串是不允许修改值的 数组可以
word = 'Python'
word[0] = 'J'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
cubes = [1, 8, 27, 65, 125]
## 4 ** 3 64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
索引还支持负数,用负数索引时,从右边开始计数:
>>> word[-1] # last character 'n' # 除了索引,字符串还支持 切片。索引可以提取单个字符,切片 则提取子字符串: >>> word[0:2] 'Py' >>> word[2:5] 'tho' >>> word[:2] + word[2:] 'Python' 要生成不同的字符串,应新建一个字符串: >>> 'J' + word[1:] 'Jython' >>> word[:2] + 'py' 'Pypy'
内置函数
len()
返回字符串的长度:>>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34
列表
>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
>>> squares + [36, 49, 64, 81, 100] # squares 列表不变 只是输出的值变化
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> squares
[1, 4, 9, 16, 25]
#列表是mutable类型 可以变换
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here
>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
# append() 方法 可以在列表结尾添加新元素
>>> cubes.append(216) # add the cube of 6
>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
# 切片赋值
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4
end关键字
取消输出后面的换行, 或用另一个字符串结尾:
>>> a, b = 0, 1 >>> while a < 1000: print(a, end=',') a, b = b, a+b 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
if and elif 语句
x = int(input("Please enter an integer: ")) if(x < 0): x=0 print('Negative changed to zero') elif x==0: print('Zero') elif x ==1: print('Single') else : print('More')
for语句
>>> # Measure some strings: ... words = ['cat', 'window', 'defenestrate'] >>> for w in words: ... print(w, len(w)) ... cat 3 window 6 defenestrate 12 遍历集合时修改集合的内容,会很容易生成错误的结果。因此不能直接进行循环,而是应遍历该集合的副本或创建新的集合:
Python——字典的 clear()、copy()、items()、popitem()
#clear d = {} a = d d['name'] = '李欣' d ['age'] = 24 print(a) d = {} #会发现 d 不会等于 {} print(a) #所以 需要 clear() a.clear() print(a)
结果:
{‘name’: ‘李欣’, ‘age’: 24}
{‘name’: ‘李欣’, ‘age’: 24}
{}
#2.copy()方法(浅复制) 返回一个具有相同键-值对的新字典 x = {'name': '李欣', 'age': 24,'like':{'base','eat','read'}} y = x.copy() print("y",y) y['name'] ='周易' y['like'].remove('eat') print("y修改后的:",y) print("x",x)
# items()方法是字典以列表形式返回,但没有特殊的返回顺序; # popitem()方法 是随机删除字典元素 z = {'name': '王者', '书本号':'NO.20181314111' ,'like':{'baseball','reading'}} c = z.items() print(c) # popitem()方法 是随机删除字典元素 p = {'name': '王者', '书本号':'NO.20180000001' ,'like':{'baseball','reading'},'price':45.11} for i in range(3): q = p.popitem() print("popitem:",q)
range() 函数
# 内置函数 range() 常用于遍历数字序列,该函数可以生成算术级数: >>> for i in range(5): print(i) #生成的序列不包含给定的终止数值;range(10) 生成 10 个值,这是一个长度为 10 的序列,其中的元素索引都是合法的。range 可以不从 0 开始,还可以按指定幅度递增 >>> list(range(5, 10)) [5, 6, 7, 8, 9] >>> list(range(0, 10, 3)) [0, 3, 6, 9] >>> list(range(-10, -100, -30)) [-10, -40, -70] #range() 和 len() 组合在一起,可以按索引迭代序列: a = ['Mary', 'had', 'a', 'little', 'lamb'] for i in range(len(a)): print(i, a[i]) >>> range(10) range(0, 10) >>> sum(range(4)) # 0 + 1 + 2 + 3 6
pass 语句
#pass 语句不执行任何操作。语法上需要一个语句,但程序不实际执行任何动作时,可以使用该语句。 >>> while True: pass #创建了最小的类 class MyEmptyClass: ... pass #pass 还可以用作函数或条件子句的占位符,让开发者聚焦更抽象的层次。此时,程序直接忽略 pass: def initlog(*args): pass # Remember to implement this!
match
def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the internet" #使用 | (“ or ”)在一个模式中可以组合多个字面值: case 401 | 403 | 404: return "Not allowed"