取值app
name = "alexdfg" print(name[3:5])
取出 exspa
name = "alexdfg" print(name[3])
取出ecode
list 類型 列表orm
li = [1,12 ,19 ,"age" ,["alex","lalala"],188,True]對象
能夠用 while 和 for 循環blog
可是循環的元素是每個 段排序
li = [1, 3, "alex", "bbb", 332] s = " " for i in li : s = s + str(i) print(s)
打出 13alexbbb332索引
雖說 把一個list 變成一個 str list 裏面能夠用這種方法 但沒看懂ip
li = [11, 22, 33, 22, 44]
# 參數
# 1.append 原來值最後追加
# 對象.方法(..) # li對象調用append方法
# li.append(5)
# li.append("alex")
# li.append([1234,2323])
# print(li)rem
# 2 clear() 清空列表
# li.clear()
# print(li)
# 3 copy() 拷貝,淺拷貝
# v = li.copy()
# print(v)
# 4. 計算元素出現的次數
# v = li.count(22)
# print(v)
# 5. .extend 擴展原列表 把新的列表加入舊的列表裏,參數:可迭代對象
# li = [11, 22, 33, 22, 44]
# li.append([9898,"不得了"])
# [11, 22, 33, 22, 44, [9898, '不得了']]
# li.extend([9898,"不得了"])
# for i in [9898,"不得了"]:
# li.append(i)
# [11, 22, 33, 22, 44, 9898, '不得了']
#
# li.extend("不得了")
# print(li)
# 6.index 根據值獲取當前值索引位置(左邊優先)
# li = [11, 22, 33, 22, 44]
# v= li.index(22)
# print(v)
# 7.insert 在指定索引位置插入元素
# li = [11, 22, 33, 22, 44]
# li.insert(0,99)
# print(li)
# 八、pop 刪除某個值(1.指定索引;2. 默認最後一個),並獲取刪除的值
# li = [11, 22, 33, 22, 44]
# v = li.pop()
# print(li)
# print(v)
# li = [11, 22, 33, 22, 44]
# v = li.pop(1)
# print(li)
# print(v)
# 9.remove 刪除列表中的指定值,左邊優先
# li = [11, 22, 33, 22, 44]
# li.remove(22)
# print(li)
# PS: 對於刪除列表的功能有 1 pop 2 remove 3 del li[0] del li[7:9] 4 clear
# 10 reverse 將當前列表進行翻轉
# li = [11, 22, 33, 22, 44]
# li.reverse()
# print(li)
# 11 列表的排序
# li = [11,44, 22, 33, 22]
# li.sort() 從小到大排列
# li.sort(reverse=True) 從大到小排列
# print(li)
### 欠
# cmp
# key
# sorted
# 6.元組的一級元素不可修改/刪除/增長
# tu = (111,"alex",(11,22),[(33,44)],True,33,44,)
# # 元組,有序。
# # v = tu[3][0][0]
# # print(v)
# # v=tu[3]
# # print(v)
# tu[3][0] = 567
# print(tu)
# 字典
# dict
# dict
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# 1 根據序列,建立字典,並指定統一的值
# v = dict.fromkeys(["k1",123,"999"],123)
# print(v)
# 2 根據Key獲取值,key不存在時,能夠指定默認值(None)
# v = dic['k11111']
# print(v)
# v = dic.get('k1',111111)
# print(v)
# 3 刪除並獲取值
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# v = dic.pop('k1',90)
# print(dic,v)
# k,v = dic.popitem()
# print(dic,k,v)
# 4 設置值,
# 已存在,不設置,獲取當前key對應的值
# 不存在,設置,獲取當前key對應的值
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# v = dic.setdefault('k1111','123')
# print(dic,v)
# 5 更新
# dic = {
# "k1": 'v1',
# "k2": 'v2'
# }
# dic.update({'k1': '111111','k3': 123})
# print(dic)
# dic.update(k1=123,k3=345,k5="asdf")
# print(dic)
# 6 keys() 7 values() 8 items() get update
##########
# 一、基本機構
# info = {
# "k1": "v1", # 鍵值對
# "k2": "v2"
# }
#### 2 字典的value能夠是任何值
# info = {
# "k1": 18,
# "k2": True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# print(info)
#### 3 布爾值(1,0)True爲1 False爲0 、列表、字典不能做爲字典的key
# info ={
# 1: 'asdf',
# "k1": 'asdf',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {'k1':'v1'}: 123
#
# }
# print(info)
# 4 字典無序
# info = {
# "k1": 18,
# "k2": True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# print(info)
# 五、索引方式找到指定元素
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# # v = info['k1']
# # print(v)
# # v = info[2]
# # print(v)
# v = info['k3'][5]['kk3'][0]
# print(v)
# 6 字典支持 del 刪除
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# del info['k1']
#
# del info['k3'][5]['kk1']
# print(info)
# 7 for循環
# dict
# info = {
# "k1": 18,
# 2: True,
# "k3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# 'kk1': 'vv1',
# 'kk2': 'vv2',
# 'kk3': (11,22),
# }
# ],
# "k4": (11,22,33,44)
# }
# for item in info:
# print(item)
#
# for item in info.keys():
# print(item)
# for item in info.values():
# print(item)
# for item in info.keys():
# print(item,info[item])
# for k,v in info.items():
# print(k,v)
# True 1 False 0
# info ={
# "k1": 'asdf',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {'k1':' v1'}: 123
#
# }
# print(info)
# 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,索引
# dic = {
# "k1": 'v1'
# }
# v = "k1" in dic
# print(v)
# v = "v1" in dic.values()
# print(v)
# 6、布爾值
# 0 1
# bool(...)
# None "" () [] {} 0 ==> False
#判斷方法 bool({}) bool([]) bool("")
都會出現 False