Python開發第一篇 基礎篇(二)-------運算子與基本資料型別
阿新 • • 發佈:2018-12-17
對於python而言,一切事物都是物件,物件是基於類建立的,物件繼承了類的屬性,方法等特性
一.int
首先我們來檢視一下int包含了哪些函式
# python3.x
dir(int)
# ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
# python 2.x dir(int) # ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
__abs__ 絕對值
__add__ 加法
__and__ 與&運算
__bool__ 布林值
__divmod__ 除法取整取模
__eq__ ==比較運算子
__float__ 轉換為浮點數
__floordiv__地板除//
__getattribute__獲取物件屬性
__ge__ 比較運算>=
__invert__ 非~運算
__le__ 小於等於
__lshift__左移運算
__lt__小於
__mod__取模運算
__mul__ 乘法運算
__neg__一元運算減法
__ne__ 不等於比較
__or__ 或|運算
__pow__ 冪運算
__rdivmod__ 與divmod返回的結果相反
__sizeof__ 計算資料型別佔用記憶體大小
__str__ int轉換成str
__sub__ 減法運算
__truediv__ 真除
__xor__ 異或^運算
bit_length 顯示資料所佔位長度
conjugate
__format__ 格式化輸出
from_bytes 字元轉換十進位制
to_bytes int轉換為位元組
二.str
1 #python3.5
2 dir(str)
3 #['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] 4 5 #python2.7 6 dir(str) 7 #['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
__add__ 字串拼接
__contains__ 包含判斷
__eq__ 字串==比較
__getattribute__獲取物件屬性
__getitem__獲取對應字元
__getnewargs__轉換成元組
__ge__ 字串比較
__gt__ 字串大於判斷
__hash__ 生成一個臨時的hash值
__iter__ 字串迭代
__len__ 判斷字串長度
__le__小於等於 __lt__ 小於 __mul__ 乘法運算
__ne__ 不等於比較
zfill 以0填充
upper 字母轉換大寫
title 標題
swapcase 大小寫轉換
strip 去除字串兩邊的空格
startswith 字串是否存在該字元
splitlines 以換行符分割字串
split 預設以空格分割字元
rstrip 去除右邊的空格
rpartition 返回字串的一部分
rjust 向右偏移
rindex 查詢下標
rindex 查詢下標
rindex 查詢下標
rindex 查詢下標
rindex 查詢下標
rindex 查詢下標
translate 翻譯
rfind 從左到右查詢
replace 字串替換
partition 擷取字串
maketrans 翻譯表
maketrans 翻譯表
maketrans 翻譯表
maketrans 翻譯表
lstrip 去除左邊的空格
lower 轉換小寫
ljust 右填充
join 生存一個字串
isupper 是否全部大小
istitle 是否是標題
isspace 是否是空格
issprintable 是否可以被列印
isnumeric 是否是數字
islower 是否是小寫
isidentifier
isdigit 是否是數字
isdecimal 是否是數字
isalpha 是否是字母
isalnum 是否為數字或字母
index 通過字元查詢下標
find查詢字串下標
format 格式化輸出字串
expandtabs 製表符長度
endswith 判斷結束字元
encode 編碼
count 統計相同的字元
center 中心顯示
casefold 字母轉換小寫
capitalize 首字母大寫
三.dict
1 #python3.5
2 dir(dict)
3 #['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] 4 5 #python2.x 6 dir(dict) 7 #['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
clear 清空字典
copy 淺拷貝字典
fromkeys 返回一個新字典
get 獲取字典值
items 獲取字典的key,values
keys 以元組形式返回字典鍵
pop 刪除指定的鍵值對
popitem 隨機刪除鍵值對
setdefault 設定預設的鍵值對
update 更新字典
values 以列表形式返回字典的值