第四篇:python基本數據類型的經常使用方法(函數)與內建函數

python內置了不少內置函數、類方法屬性及各類模塊。當咱們想要當咱們想要了解某種類型有哪些屬性方法以及每種方法該怎麼使用時,咱們鼓勵使用dir()函數、help()函數或網上搜索來研究各種的方法。dir() 函數不帶參數時,返回當前範圍內的變量、方法和定義的類型列表;帶參數時,返回參數的屬性、方法列表。若是參數包含方法__dir__(),該方法將被調用。若是參數不包含__dir__(),該方法將最大限度地收集參數信息。help()函數幫助咱們瞭解模塊、類型、對象、方法、屬性的詳細信息。html

print(dir()) #不帶參數,得到當前模塊的屬性列表

'''
運行結果:
    ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'num', 'srt1']
    
    注意:有一些屬性或模塊是python自動加載進去的。
'''
print(dir(list)) #若帶參數,則返回參數的屬性與方法
print(help(print)) #查看print()函數的文檔信息
print(help('keywords')) #查看python的關鍵字

 numberpython

python官方詳細文檔:https://docs.python.org/3.6/library/stdtypes.html#numeric-types-int-float-complexapp

字符串的經常使用函數

字符串相關的官方文檔:https://docs.python.org/3.6/library/stdtypes.html#text-sequence-type-stride

列表的經常使用函數

列表相關的官方文檔:https://docs.python.org/3.6/library/stdtypes.html#sequence-types-list-tuple-range函數

    添加 ——》append() , insert()ui

fruits = ['蘋果','梨子','香蕉','橙子','葡萄']
fruits.append('李子') #末尾追加
print(fruits)
fruits.insert(2,'榴蓮') #指定的下標位置插入
print(fruits)
fruits.insert(9,'荔枝') #當指定的下標大於列表的長度時,效果與append()同樣
print(fruits)

    刪除 ——》pop() , remove()spa

fruits = ['蘋果','梨子','香蕉','橙子','葡萄']
fruits.pop(1) #經過索引刪除元素
print(fruits)
fruits.remove('葡萄') #知道元素的時候,可使用remove()
print(fruits)

#經過del語句刪除
del fruits[2]
print(fruits)

     更多列表的方法,使用print(dir(list))來查找3d

集合的經常使用方法

    建立:set()、frozense()code

#一、建立,將會自動去重,其元素爲不可變數據類型,即數字、字符串、元組
test01 ={"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True}
#或者
test02 =set({"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True})

#二、不可變集合的建立 -->frozenset()
test =frozenset({"zhangsan","lisi","wangwu","lisi",666,("hello","world",),True})
示例

    增:   add()、update()htm

#更新單個值 --->add
names ={"zhangsan","lisi","wangwu"}
names.add("james") #其參數必須爲hashable類型
print(names)

#更新多個值 -->update
names ={"zhangsan","lisi","wangwu"}
names.update({"alex","james"})#其參數必須爲集合
print(names)
示例

    刪除:pop()、remove()、discard()

#隨機刪除 -->pop
names ={"zhangsan","lisi","wangwu","alex","james"}
names.pop()
print(names)

#指定刪除,若要刪除的元素不存在,則報錯 -->remove
names ={"zhangsan","lisi","wangwu","alex","james"}
names.remove("lisi")
print(names)

#指定刪除,若要刪除的元素不存在,無視該方法 -->discard
names ={"zhangsan","lisi","wangwu","alex","james"}
names.discard("hello")
print(names
示例

   關係運算:交集 & 、並集 | 、差集 - 、交差補集 ^ 、 issubset() 、isupperset()

english_c ={"ZhangSan","LiSi","James","Alex"}
math_c ={"WangWu","LiuDeHua","James","Alex"}

#一、交集-->  in a and in b
#統計既報了英語班又報了數學班的同窗
print(english_c & math_c)
print(english_c.intersection(math_c))
#輸出爲:{'Alex', 'James'}

#二、並集--> in a or in b
#統計報名了兩個班的全部同窗
print(english_c | math_c)
print(english_c.union(math_c))
#輸出爲:{'James', 'ZhangSan', 'LiuDeHua', 'LiSi', 'Alex', 'WangWu'}

#三、差集--> in a not in b
#統計只報名英語班的同窗
print(english_c - math_c)
print(english_c.difference(math_c)) 
#輸出爲:{'LiSi', 'ZhangSan'}

4、交差補集 
#統計只報名一個班的同窗
print(english_c ^ math_c)
#輸出爲:{'LiuDeHua', 'ZhangSan', 'WangWu', 'LiSi'}

#五、issubset-->n 是否爲 m 的子集
#   issuperset --> n 是否爲 m 的父集
n ={1,2,4,6,8,10}
m ={2,4,6}
l ={1,2,11}

print(n >= m)
#print(n.issuperset(m)) #n 是否爲 m的父集
#print(n.issuperset(l))
print(m <=n)    
#print(m.issubset(n))    #m 是否爲 n的子集
示例

字典的經常使用方法

字典相關的官方文檔:https://docs.python.org/3.6/library/stdtypes.html#mapping-types-dict

字典的增、刪、改、查

fruits = {'蘋果':2,'梨子':5,'香蕉':6,'橙子':8,'葡萄':9}

#增:只要添加的鍵在字典不存在,就會新添加這個元素。
fruits['榴蓮'] = 10
print(fruits)

#若鍵鍵在字典存在,就會修改這個元素。
fruits['蘋果'] = 15
print(fruits)

#
del fruits['榴蓮'] # del語句能夠刪除整個字典和字典的某個元素
print(fruits)
fruits.clear()  #清空字典全部元素,變成空字典
print(fruits)

 

更多字典的方法,使用print(dir(dict))來查找

python的內置函數

python的內置函數主要來自「__builtins__」模塊,__builtins__   模塊是python的內置模塊,啓動python便會自動導入,因此可使用print(dir(__builtins__))查看‘「__builtins__」模塊全部內置函數。具體使用方法參考:1.中文文檔。    2.python官方文檔 

相關文章
相關標籤/搜索