python快速上手之实战篇(列表、字典、字符串)
列表
用下标、切片取值,len()取长度,列表复制和连接,改变列表中的值,列表删除,列表循环,in用于循环,index(),append(),insert(),remove(),sort()
a=['hello','huanglianghuang','myname','name','abc']
print(a[1])
print(a[-1])
print(a[0:4:2])
print(len(a))
a[1]='huang'
print(a)
b=['liang']
print(a+b)
print(a+b*3)
del a[-1]
print(a)
for i in a:
print(i)
print(a.index('hello'))
a.append('liang')
print(a)
a.insert(1,'list')
print(a)
a.remove('liang')
print(a)
a.sort()
print(a)
tuple列表转换元组
a=['hello','huang','myname','name','list']
print(tuple(a))
copy模块的copy()
import copy
a=['hello','huang','myname','name','list']
b=copy.copy(a)
print(b)
字典
字典中的keys()方法、values()方法,get()方法
a={1:'hello',2:'huang',3:'myname',4:'name',5:'list'}
print(type(a))
for i in a.keys():
print(i)
for i in a.values():
print(i)
print(a.get(1,2)+','+'huang')
字符串
字符串下标、upper()方法、lower()方法、join()方法,pyperclip模块
import pyperclip
a="I'm a huang"
print(a)
print(a[0])
print(a[0:len(a):2])
print(a.upper())
print(a.lower())
print(','.join(a))
print(pyperclip.copy(a))
print(pyperclip.paste())
|