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)