列表
“列表”是一个值,它包含多个字构成的序列。术语“列表值”指的是列表本身(它作为一个值,可以保存在变量中、传递给函数)--:按下标取值、切片、for循环、用于len()以及in not in等
list ['aa','bb','cc','dd']是一个简单的列表
1.用列表下标取值
list =['aa','bb','cc','dd']
list[0]--aa
list[1]--bb
list[-1]--dd
2.切片取值(从列表中取得一个或多个值,返回结果是列表)
list =['aa','bb','cc','dd']
list[:3]--['aa','bb','cc'] #顾头不顾尾
list[:1]--['aa']
list[0:-1]--['aa','bb','cc']
3.用len( )获取列表的长度
list ['aa','bb','cc','dd']
len(list)--4
4.用下标改变列表中的值
list =['aa','bb','cc','dd']
list[0]='AA' -- ['AA','bb','cc','dd']
list[0]=list[1]---- ['bb','bb','cc','dd']
5.列表连接和列表复制
list= ['aa','bb','cc','dd']
num=['11','22','33','44']
list+num--['aa', 'bb', 'cc', 'dd', '11', '22', '33', '44']
list+['AA','BB']---['aa', 'bb', 'cc', 'dd','AA','BB']
list*2--['aa', 'bb', 'cc', 'dd' ,'aa', 'bb', 'cc', 'dd']
6.用del从列表中删除值
list= ['aa','bb','cc','dd']
del list[0]-- ['bb','cc','dd']
7.使用列表
cataNames = []
while True:
print ('enter the name of cat'+str(len(cataNames)) + '(or enter nothing to stop.):' )
name = str(input())
if name =='':
break
cataNames = cataNames+[name]
print ('the cat names are:')
for name in cataNames:
print (''+name)
C:Python27python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
enter the name of cat0(or enter nothing to stop.):
'cat01'
enter the name of cat1(or enter nothing to stop.):
'cat02'
enter the name of cat2(or enter nothing to stop.):
'cat03'
enter the name of cat3(or enter nothing to stop.):
' '
the cat names are:
cat01
cat02
cat03
Process finished with exit code 0
列表用于循环
list = ['cat001','cat002','cat003']
for i in range(len(list)):
print ('index ' + str(i)+ 'in list is:'+ list[i])
C:Python27python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
index 0in list is:cat001
index 1in list is:cat002
index 2in list is:cat003
8.in和not in操作符
list= ['aa','bb','cc','dd']
print 'aa' in list--True
print 'aa1' in list--False
9.对变量增强的赋值操作(+、-、*、/、%)
aa = 10
aa +=2 #aa = aa +2 输出12
aa -=2 #aa = aa-2 输出10
aa *=2 #aa = aa *2 输出20
aa /=2 #aa = aa/2 输出10
aa %=2 #aa = aa % 2 输出0
10.用index()方法在列表中查找值:根据元素值查元素下标
list= ['aa','bb','cc','dd','cc']
print list.index('bb') #1
print list.index('cc') #2:若列表有多个相同元素值,则返回第一个
11.用append()/insert()方法在列表中添加值
list= ['aa','bb','cc','dd']
list.append('ee') #['aa', 'bb', 'cc', 'dd', 'ee']
list.insert(0,'AA') #['AA', 'aa', 'bb', 'cc', 'dd', 'ee']
12.remove()删除列表中的值
list= ['aa','bb','cc','dd']
list.remove('aa') #['bb', 'cc', 'dd'] 直接删元素值
del list[0] #['cc', 'dd'] 通过下标删元素值
13.用sort()方法将列表中的值排序(只能对纯数字、纯字母的列表进行排序)
list= ['dd','cc','bb','aa']
list.sort() #['aa', 'bb', 'cc', 'dd'] 默认升序排列
list.sort(reverse=True) #['dd', 'cc', 'bb', 'aa']
list.sort(reverse=False) #['aa', 'bb', 'cc', 'dd']
list= ['dd','cc','bb','aa','AA','BB','CC','DD']
list.sort() #['AA', 'BB', 'CC', 'DD', 'aa', 'bb', 'cc', 'dd'] 使用‘ASCII字符排序’,因此大写字母会在小写字母之前
字符串
1.列表的很多操作,也可以用于字符串
name = 'python'
print(name[0]) #p
print(name[-1]) #n
print(name[0:3]) #pyt
print(name[::-1]) #nohtyp
print('py' in name) #True
print('PJ' in name) #False
for i in name:
print(i) # p y t h o n
元组
列表数据类型的不可变形式
1.元组的样子
color = ('red','blue','yellow','black')
print(color[0]) #red
print(color[0:2]) #('red', 'blue')
print(color[::-1]) #('black', 'yellow', 'blue', 'red')
2.元组只有一个值,末尾要加上英文逗号
color1=('aa')
color2=('aa',)
print(type(color1)) #<class 'str'>
print(type(color2)) #<class 'tuple'>
3.用list()和tuple函数转换类型
list1=[1,2,3,4]
tuple1=('a','b','c','d')
print(list(tuple1)) #['a', 'b', 'c', 'd']
print(tuple(list1)) #(1, 2, 3, 4)