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) >> a,b,c,*d=1,23,4,5,6 >>> a 1 >>> b 23 >>> c 4 >>> d [5, 6] >>> >>> print(a,b,c,d) 1 23 4 [5, 6] >>> print(a,b,c,d,sep'-') File "<stdin>", line 1 print(a,b,c,d,sep'-') ^ SyntaxError: invalid syntax >>> print(a,b,c,d,sep='-') 1-23-4-[5, 6] >>> x=666 >>> import sys >>> sys.stdout(x) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '_io.TextIOWrapper' object is not callable >>> sys.stdout.write(x) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: write() argument must be str, not int >>> sys.stdout.write(str(x)) 6663输出百分数
a=100 print('%d%%'%a)#100% name='Tom' age=18 print('his name is %s,his age is %d'%(name,age)) print('his name is',name,'his age is',age,'.') print('his name is'+ name +',his age is'+ str(age)+'.') print('{address},name:{},age:{}'.format('Tom',18,address='hangzhou')) print('第一个元素是{0[0]},第二个元素是{0[1]},第三个元素是{0[2]},\ 第四个元素是{1[0]}'.format(('www','google','com'),('baidu.','j')))his name is Tom,his age is 18 his name is Tom his age is 18 . his name isTom,his age is18. hangzhou,name:Tom,age:18 第一个元素是www,第二个元素是google,第三个元素是com,第四个元素是baidu.
数组 特点:是一个有序的 、可重复的、可变类型
li1=[1,23,4] li2=['s','d'] print(li1+li2) #拼接 print(li1*3) #重复 ls=['w','w','wew','r',12] print(len(ls)) #长度 print((ls[1],ls[4])) #索引提取 print(ls[2][1])[1, 23, 4, ‘s’, ‘d’] [1, 23, 4, 1, 23, 4, 1, 23, 4] 5 (‘w’, 12) e
[22478, 24066, 23398, 38498] [22478, 24066, 23398, 38498, ‘sad’, ‘你好’] [22478, 24066, [‘sad’, ‘你好’], 23398, 38498, ‘sad’, ‘你好’]
遍历
#遍历 #元素遍历 li=['23','23',4] for i in li: print(i) #索引遍历 li=['23','23',4] for i in range(len(li)): print(li[i]) #枚举遍历 #enumerate(),对于一个可迭代的/可遍历的对象 # 将其组成索引序列,;利用它,同时可以获得索引和值 li=['23','23',4] for i in enumerate(li,6): print(i) li=['23','23',4] for index,value in enumerate(li[:3],3): print(index,value) print([x for x in range(10) if x%2==0])#[0, 2, 4, 6, 8](6, ‘23’) (7, ‘23’) (8, 4) 3 23 4 23 5 4
排序
li=['we','waa','a','asdsd'] li.reverse()#反转 li.sort()#按照ascii码排序 li.sort(reverse=True) print(li)[‘we’, ‘waa’, ‘asdsd’, ‘a’]
1.input()函数实现往空列表中添加元素,当输入"q"的时候,结束输入,并返回列表。
# list1=[] # while True: # a=input('添加元素 \t') # if a=='q': # break # else: # list1.append(a) # print(list1)2.随机生成30个0-10的随机整数,并统计每个数出现的次数, 返回的结果形式为[[0, 3], [1, 4], … , [10, 5]]
# s=[0]*11 # d=[] # import random # for i in range(30): # a=random.randint(0,10) # for j in range(11): # if a==j: # s[j]+=1 # for i in range(11): # d.append([i,s[i]]) # print(d) #进阶版 # rand_int=[] # result_int=[] # for i in range(30): # rand_int.append(random.randint(0,10)) # for i in range(11): # result_int.append([i,rand_int.count(i)]) # print(result_int)[‘s’, ‘wq’, 1, 23, 45, 67, [44, 56]] 19671040 [‘s’, ‘wq’, 1, 23, 45, 67, [44, 56]] 19670200 [‘s’, ‘wq’, 1, 23, 45, 67, [44, 56]] 48590600 [‘s’, ‘wq’, 1, 23, 45, 67, [44, 34]] [‘s’, ‘wq’, 1, 23, 45, 67, [44, 34]] [‘s’, ‘wq’, 1, 23, 45, 67, [44, 56]]
#三目运算符 y=10 x=y+1 if y>10 else y-1 print(x,y) #带过滤功能格式 '''python [exp for iter_val in iter if if_condtion] ''' name=['Tom','Jack','Lucy'] subject=['python','java','c'] print([[i,j]for i in name for j in subject])9 10 [[‘Tom’, ‘python’], [‘Tom’, ‘java’], [‘Tom’, ‘c’], [‘Jack’, ‘python’], [‘Jack’, ‘java’], [‘Jack’, ‘c’], [‘Lucy’, ‘python’], [‘Lucy’, ‘java’], [‘Lucy’, ‘c’]]
(‘a’, ‘b’, ‘c’, 1, 2, 3, 4, [5, 6]) (1, 2, 3, 4, [5, 6], 1, 2, 3, 4, [5, 6], 1, 2, 3, 4, [5, 6]) (2, 3)
增,不能
删,删除某个元素,不能;但可以全部删除
重新调用不改变id 地址
