Python第三天笔记——格式化与元组

it2022-05-05  107

print函数查看帮助文件

>>> help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream.

语法格式:

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

等价于

print(value1,value2,...,valuen, sep=' ', end='\n', file=sys.stdout, flush=False)

demo:

>>> *a,b,c,d=1,2,3,4,5,6 >>> print(a,b,c,d) [1, 2, 3] 4 5 6 >>> print(a,b,c,d,sep="+") [1, 2, 3]+4+5+6 >>> for i in range(5): ... print(i,end="#") ... 0#1#2#3#4#>>> >>> x=666 >>> sys.stdout.write(str(x)) 6663

格式化字符串

%号格式化

%格式化字符串,用%匹配参数,注意个数一 一对应。

占位符

格式描述%d有符号的整数%s字符串%c字符和ASCII码%o无符号八进制整数%x/%X无符号十六进制整数%e/%E浮点数,科学计数法%f浮点数 print("%d"% 888) name="CL" age=29 print("Her name is %s,her age is %d"%(name,age)) print("Her name is",name,"her age is",age) print("Her name is "+name+",her age is "+str(age)+".") #输出结果: 888 Her name is CL,her age is 29 Her name is CL her age is 29 Her name is CL,her age is 29. >>> 2.5E4 25000.0 >>> x=100 >>> print("%d%%"%x) 100%

format格式化

demo:

print("{} likes {}".format("jack","ice cream")) print("{name} likes {}".format('ice cream',name='jack')) print("{name} likes {sth}".format(sth='ice cream',name='jack')) #输出结果: jack likes ice cream print("第一个元素是:{0[0]},第二个元素是:{0[1]},第三个元素是:{0[2]},第四个元素是:{1[0]},第五个元素是:{1[1]}".format(('www.','google.','com'),('baidu.','com'))) #输出结果: 第一个元素是:www.,第二个元素是:google.,第三个元素是:com,第四个元素是:baidu.,第五个元素是:com print('1/81={x}'.format(x=1/81)) print('1/81={x:f}'.format(x=1/81)) print('1/81={x:.3f}'.format(x=1/81)) #输出结果: 1/81=0.012345679012345678 1/81=0.012346 1/81=0.012

元组

有序可以重复不可更改

元组的创建

空元组的创建,不能添加元素单元素元组的创建,需要在单元素后面添加逗号多元素元组的创建,包含多种数据类型 tp=() print(type(tp)) tp=("ab") print(type(tp)) tp=("ab",) print(type(tp)) tp=("ab",1,["a","b"]) print(tp,type(tp)) #输出结果: <class 'tuple'> <class 'str'> <class 'tuple'> ('ab', 1, ['a', 'b']) <class 'tuple'>

元组的运算

拼接 tp1=("ab",1,) tp2=(["a","b"],13) print(tp1+tp2) #输出结果: ('ab', 1, ['a', 'b'], 13) 重复 print(tp1*3) #输出结果: ('ab', 1, 'ab', 1, 'ab', 1) 索引,切片 tp=('ab', 1, ['a', 'b'], 13) print(tp[0]) print(tp[:2]) print(tp[::2]) print(tp[::-1]) #输出结果: ab ('ab', 1) ('ab', ['a', 'b']) (13, ['a', 'b'], 1, 'ab') #元组中列表的元素可以修改 tp[2][0]='hh' print(tp) #输出结果: ('ab', 1, ['hh', 'b'], 13)

元组的常见操作

索引查切片查index() tp=('ab', 1, ['a', 'b'], 13,'c','e') print(tp.index("c")) #输出结果: 4

增:拼接(+)

改:不能,元组是不可变类型,不能修改元组的元素。

删:不能单删某个元素,可以全删(del)

元组的遍历

元素遍历 tp=('a','c','e') for i in tp: print(i,end=" ") #输出结果: a c e 索引遍历 for i in range(len(tp)): print(tp[i],end=" ") #输出结果: a c e 枚举遍历 for i in enumerate(tp): print(i) #输出结果: (0, 'a') (1, 'c') (2, 'e')

元组的嵌套

**双层嵌套**: tp=((1,2,3), (4,5,6), (7,8,9)) print(tp[2][1]) print(tp[1][1]) #输出结果: 8 5 **三层嵌套**: tp1=(((1,2,3),(4,5,6)),((11,22,33),(44,55,66))) print(tp1[0][0]) print(tp1[1][1]) #输出结果: (1, 2, 3) (44, 55, 66)

最新回复(0)