li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
v = li[3][3][0][1]
print(v)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True]
print(li[3])
print(li[2:4])
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
for item in li:
print(item)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
li[1] = [11, 22, 33, 44]
print(li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
li[1:3] = [20, 30]
print(li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
del li[1]
print(li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
del li[0:4]
print(li)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py'], 23], True, 'cyx']
v = 20 in li
print(v)
s = 'qwertyuioopasdfghjkl'
v = list(s)
print(v)
s = ''
li =[11, 22, 33, '123', 'alex']
for i in li:
s = s + str(i)
print(s)
s = ''
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
for i in li:
s = s + str(i)
print(s)
li = [1, 2, 3, ['yu', 'gh', 'jk', ['py', 16, 'op'], 23], True, 'cyx']
v = str(li)
print(v)
li =['123', 'alex']
s = ''.join(li)
print(s)
li = [11, 22, 33, 44]
li.append(5)
print(li)
li = [11, 22, 33, 44]
li.clear()
print(li)
li = [11, 22, 33, 44]
v = li.copy()
print(v)
li = [11, 22, 33, 44, 22]
v = li.count(22)
print(v)
li = [11, 22, 33, 44, 22]
li.append([5656, 'cyx'])
print(li)
li = [11, 22, 33, 44, 22]
li.extend([5656, 'cyx'])
print(li)
li = [11, 22, 33, 44, 22]
for i in [5656, 'cyx']:
li.append(i)
print(li)
li = [11, 22, 33, 44, 22]
li.extend('集合吧')
print(li)
li = [11, 22, 33, 44, 22]
v = li.index(22)
print(v)
li = [11, 22, 33, 44, 22]
li.insert(2, 'cyx')
print(li)
li = [11, 22, 33, 44, 22]
v = li.pop()
print(li)
print(v)
li = [11, 22, 33, 44, 22]
v = li.pop(3)
print(li)
print(v)
li = [11, 22, 33, 44, 22]
li.remove(22)
print(li)
注: pop remove del[0] del[0:3] clear都表示刪除python
li = [11, 22, 33, 44, 22]
li.reverse()
print(li)
li = [11, 22, 33, 44, 22]
li.sort()
print(li)
li = [11, 22, 33, 44, 22]
li.sort(reverse = True)
print(li)
通常寫元組的時候,在最後一位加,,不會報錯,爲了將元組和參數區分開來app
tu = (11, 22, 33, 44, 22)
v = tu[0]
print(v)
tu = (11, 22, 33, [(33,44)], 44, 22,)
v = tu[3][0][0]
print(v)
tu = (11, 22, 33, 44, 22)
v = tu[0]
print(v)
tu = (11, 22, 33, 44, 22)
for item in tu:
print(item)
li = [11, 22, 33, 44, 22]
v = tuple(li)
print(v)
s = 'sgjdkhie'
v = tuple(s)
print(v)
元組轉換爲列表oop
tu = (11, 22, 33, 44, 22)
v = list(tu)
print(v)
元組轉換爲字符串測試
1)元素都是字符串時ui
tu = ('hui', 'qwe')
v = ''.join(tu)
print(v)
2)元素爲字符串和數字組合時,需寫for 循環spa
s = ''
tu = (123, 'qwe')
for i in tu:
s = s + str(i)
print(s)
tu = (11, 22, 33, [(33,44)], 44, 22,)
tu[3][0] = 567
print(tu)
[(33,44)]中的33不可修改,由於它屬於(33,44)的一級元素
建立字典:orm
info = {'k1': 'v1', 'k2': 'v2'}
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
v = info['k1']
print(v)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
v = info['k3'][5]['kk3'][0]
print(v)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
del info['k1']
print(info)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
del info['k3'][5]['kk1']
print(info)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info:
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info.keys():
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for item in info.values():
print(item)
info = {'k1': 18, 'k2': True, 'k3': [11, [], (), 22, 33,{'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
for k, v in info.items():
print(k, v)
info ={ 1: 'asdf', "k1": 'asdf','k1': "123",(11,22): 123,}
print(info)
info ={ 1: 'asdf', "k1": 'asdf', True: "123",(11,22): 123,}
print(info)
info ={ 7: 'asdf', "k1": 'asdf', True: "123",(11,22): 123,}
print(info)
dic ={ 'k1': 'v1', "k2": 'v2'}
v = dict.fromkeys(['qw', 123, 'uio'], 123)
print(v)
dic ={ 'k1': 'v1', "k2": 'v2'}
v = dict.fromkeys('dffhkji', 123)
print(v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.get('k333')
print(v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.get('k333', 1111)
print(v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.pop('k1')
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.pop('k1111', 'lop')
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.popitem()
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
k, v = dic.popitem()
print(dic, k, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.setdefault('k1', 123)
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
v = dic.setdefault('k1111', 123)
print(dic, v)
dic = {'k1': 'v1', "k2": 'v2'}
dic.update({'k1': 123, 'k3': 'uiop'})
print(dic)
dic = {'k1': 'v1', "k2": 'v2'}
dic.update(k1=123, k2=456, k3='klj')
print(dic)
dic = {
"k1": 'v1'
}
v = "k1" in dic
print(v)
v = "v1" in dic.values()
print(v)
list1 = ["這", "是", "一個", "測試"]對象
for index, item in enumerate(list1): 排序
print index, item 索引
>>>
0 這
1 是
2 一個
3 測試
list1 = ["這", "是", "一個", "測試"]
for index, item in enumerate(list1, 1):
print index, item
>>>
1 這
2 是
3 一個
4 測試
從新賦值後,用id(變量名)查看,id地址改變,說明其爲不可變,修改其值,id地址不變,說明其爲可變
可變:列表 字典
不可變:字符串 數字 元組
按索引順序訪問:元組字符串 列表
key value映射方式訪問:字典
直接訪問:數字
容器類型:列表 元組 字典
原子類型:數字 字符串
字典查詢速度比列表快,但字典佔得內存高
必需要背的:
1、數字
int(..)
2、字符串
replace/find/join/strip/startswith/split/upper/lower/format
tempalte = "i am {name}, age : {age}"
# v = tempalte.format(name='alex',age=19)
v = tempalte.format(**{"name": 'alex','age': 19})
print(v)
3、列表
append、extend、insert
索引、切片、循環
4、元組
忽略
索引、切片、循環 一級元素不能被修改
5、字典
get/update/keys/values/items
for,索引
6、布爾值
0 1
bool(...)
None "" () [] {} 0 ==> False
dic = { '植物': {'草本植物': ['牽牛花', '瓜葉菊', '葫蘆', '翠菊', '冬小麥', '甜菜'], '木本植物': ['喬木', '灌木', '半灌木', '如鬆', '樟'], '水生植物': ['荷花', '千屈菜', '菖蒲', '黃菖蒲', '水蔥', '再力花', '梭魚草']}, '動物': {'兩棲動物': ['山龜', '山鱉', '石蛙', '娃娃魚', '蟾蜍', '龜', '鱷魚', '蜥蜴', '蛇'], '禽類': ['雉雞', '原雞', '長鳴雞', '昌國雞', '鬥雞', '長尾雞', '烏骨雞'], '哺乳類動物': ['虎', '狼', '鼠', '鹿', '貂', '猴', '梭魚草', '貔', '樹獺', '斑馬', '狗']}}