greeting = 'hello' #字符串也能够理解为序列,由于对元素进行了编号等处理 print greeting[3] print 'hello'[3] fourth = raw_input('year: ')[3] print fourth #输出 l l year:
greeting = 'hello' #字符串也能够理解为序列,由于对元素进行了编号等处理
print greeting[3]
print 'hello'[3]
fourth = raw_input('year: ')[3]
print fourth
#输出
l
l
year:
numbers = [1,2,3,4,5,6,7,8] print numbers[3:6] #注意,只获取包含索引3,不包含索引6 print numbers[3:-2] #索引从3开始到倒二的位置 print numbers[:7] #头取到尾 print numbers[:-1] #从最开始数取到导数第一,不包含倒数第一个 print numbers[-1:] #取最后一个 print numbers[-3:0] #输出空列表 print numbers[:] #复制整个序列 print numbers[:len()] #取出整个序列中的元素 #输出结果 [4, 5, 6] [4, 5, 6] [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] [8] []
numbers = [1,2,3,4,5,6,7,8]
print numbers[3:6] #注意,只获取包含索引3,不包含索引6
print numbers[3:-2] #索引从3开始到倒二的位置
print numbers[:7] #头取到尾
print numbers[:-1] #从最开始数取到导数第一,不包含倒数第一个
print numbers[-1:] #取最后一个
print numbers[-3:0] #输出空列表
print numbers[:] #复制整个序列
print numbers[:len()] #取出整个序列中的元素
#输出结果
[4, 5, 6]
[4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[8]
[]
numbers = [1,2,3,4,5,6,7,8,9,10] print numbers[0:10:1] print numbers[0:10:2] print numbers[::4] #输出[1, 5, 9],将步长设置为4,从开始位置间隔3个取出一个 print numbers[8:3:-1] #输出[9, 8, 7, 6, 5],步长不能为0,负数表示从右到左取元素,从第8个位置开始取到第3个位置,间隔为0 print numbers[10:0:2] #输出[10,8,6,4,2] print numbers[5::-2] #输出[6,4,2] print numbers[:5:-2] #输出[10.8]
numbers = [1,2,3,4,5,6,7,8,9,10]
print numbers[0:10:1]
print numbers[0:10:2]
print numbers[::4] #输出[1, 5, 9],将步长设置为4,从开始位置间隔3个取出一个
print numbers[8:3:-1] #输出[9, 8, 7, 6, 5],步长不能为0,负数表示从右到左取元素,从第8个位置开始取到第3个位置,间隔为0
print numbers[10:0:2] #输出[10,8,6,4,2]
print numbers[5::-2] #输出[6,4,2]
print numbers[:5:-2] #输出[10.8]
print [1,2,3,4]+[5,6] print [1,2,3]+'hello wolrd' #TypeError: can only concatenate list (not "str") to list ,序列类型不一样没法相加
print [1,2,3,4]+[5,6]
print [1,2,3]+'hello wolrd' #TypeError: can only concatenate list (not "str") to list ,序列类型不一样没法相加
s =[1,2,3,4]+[5,6] print s print s *3 #乘法表示生成一个新的序列中将原序列重复的3次 print [None] *10 #None是Python的内键值,表示空值 #输出 [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] [None, None, None, None, None, None, None, None, None, None]
x
s =[1,2,3,4]+[5,6]
print s
print s *3 #乘法表示生成一个新的序列中将原序列重复的3次
print [None] *10 #None是Python的内键值,表示空值
#输出
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
[None, None, None, None, None, None, None, None, None, None]
in | 若是在指定的序列中找到值返回True,否者返回False |
not in | 若是在指定的序列中没有找到值返回True,不然返回False |
database = [['a','1'],['b','2'],['c','3']] userame = raw_input('your name: ' ) pin = raw_input('pin码: ' ) if([userame,pin] in database): #if过好内为 TRUE则输出 Access granted print 'Access granted' else: print "wrong"
database = [['a','1'],['b','2'],['c','3']]
userame = raw_input('your name: ' )
pin = raw_input('pin码: ' )
if([userame,pin] in database): #if过好内为 TRUE则输出 Access granted
print 'Access granted'
else:
print "wrong"
database = [['a','1'],['b','2'],['c','3']] print len(database) print max(database) print min(database)
database = [['a','1'],['b','2'],['c','3']]
print len(database)
print max(database)
print min(database)