#1. #A:赋值的形式 a, b, c = "szn" #序列赋值运算 a = 's' b = 'z' c = 'n' [value0, value1] = [1, 2] #列表赋值运算 value0 = 1 value1 = 2 value0, value1 = 'a', 'b' #元组赋值运算 value0 = 'a' value1 = 'b' value0, *value1 = "string" #扩展的序列解包 value0 = 's' value1 = ['t', 'r', 'i', 'n', 'g'] value0, *value1, value2 = "string" #value0 = 's' value1 = ['t', 'r', 'i', 'n'] value2 = 'g' 不管带星号的名称出现在哪里,包含该位置的每个未赋值名称的一个列表都将赋给它 value0, *value1 = "12" #value0 = '1' value1 = ['2'] 带星号的名称可能只匹配单个的项,但是,总会向其赋值一个列表 value0, *value1 = "1" #value0 = '1' value1 = [] #*value0 = "ss" #运行出错 如果有多个带星号的名称,或者值少了而没有带星号的名称,以及如果带星号的名称自身没有编写到一个列表中,都将引发错误 *value0, = "ss" #value0 = ['s', 's'] s0 = s1 = [1, 2] #多目标赋值运算 s0 = [1, 2] s1 = [1, 2] s0.append('s') #s1 = [1, 2, 's'] a, b, c = range(3) #a = 0 b = 1 c = 2 #2. #A:序列赋值语句实际上支持任何可迭代的对象,而不是局限于任何序列 #B:python不支持++ -- 运算符,因为python没有像对数字那样不可变对象进行在原处修改的概念 [a, b, c] = (1, 2, 3) #a = 1 b = 2 c = 3 (d, e, f) = "abc" #d = 'a' e = 'b' f = 'c' #(d, e, f) = "abcd" #运行出错 虽然可以在等号两侧混合相匹配的序列类型,但是右边元素的数目还是要和左边的变量数目相同的,否则会产生错误 string = "abcd" a, b, c = list(string[:2]) + [string[2:]] #a = 'a' b = 'b' c = 'cd' ((a, b), c) = ("ab", "cd") #a = 'a' b = 'b' c = 'cd' for ((a, b), c) in [((1, 2), 3), (('a', 'b'), 'c')]: print(str(a) + " " + str(b) + " " + str(c), end = ' ') #1 2 3 a b c #3. #A:+总是生成新对象,而+=是在原处进行修改,其他比如-= *= 均类似 list0 = [1] list1 = list0 list0 = list0 + [2] #list0 = [1, 2] list1 = [1] list0 = [1] list1 = list0 list0 *= 2 #list0 = [1, 1] list1 = [1, 1] #4. #A:print([object, ...][, sep = ' '][, end = ' '][, file = sys.stdout][, flush = False]) #B:printf第一个参数会接收多个待打印值,第二个参数为文本之间的插入字符串,默认为一个空格,第三个参数为换行符,默认为 ,第四个参数是文本将要发送的地方,第五个参数代表是否需要刷新缓冲 print() import sys print("s0", 's1', 's2', sep = 'v', end = '', file = sys.stdout, flush = True) #s0vs1vs2v MyFile = open('1.txt', 'w') print(1 + 2, [1, 2, {3}], sep = 'sep', end = 'end', file = MyFile) #文件1.txt中写入3sep[1, 2, {3}]end