Python3中int、float、str、list、dict、tuple類的內建方法
-->the start
養成好習慣,每次上課的內容都要寫好筆記。
第二天內容主要是熟悉int、long、float、str、list、dict、tuple這幾個類的內建方法。
對於Python來說,一切事物都是對象,對象是基於類創建的。
一、整型
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Q1mi" 4 """ 5 int類的幾個內建方法 6 """ 7 8 # 返回商和餘數的元祖 9 all_pages = 95 10 per_page = 10 11 pages = all_pages.__divmod__(per_page) 12 print(pages) 13 14 # __ceil__ ?? 15 num = 19 16 print(num.__ceil__()) 17 18 # 絕對值 19 num1 = -19 20 print(abs(num1)) 21 print(num1.__abs__()) 22 23 # 相加 24 num2 = 5 25 print(num1+num2) 26 print(num2.__add__(num1)) 27 28 # 布爾值,非零返回真, 29 num3 = 0 30 print(num2.__bool__()) 31 print(num3.__bool__()) 32 33 # 判斷是是否相等 34 print(num3.__eq__(num2)) 35 36 # 返回浮點 37 print(num2.__float__()) 38 39 # 地板除 19//5 40 print(num.__floordiv__(num2)) 41 42 # 大於等於 43 print(num.__ge__(num2)) 44 45 # 大於 46 print(num.__gt__(num2)) 47 48 # 哈希 49 print(num.__hash__()) 50 51 # __index__ :索引 用於切片,數字無意義 52 # x[y: z] <==> x[y.__index__(): z.__index__()] 53 54 # __init__ :構造方法 55 56 # 轉換為整數 57 # x.__int__() <==> int(x) 58 59 # __invert__ :取反 x.__invert__() <==> ~x 60 print(num.__invert__()) 61 62 # 小於等於 63 print(num.__le__(num2)) 64 65 # 小於 66 print(num.__lt__(num2)) 67 68 # __lshift__ :左移位 69 int_temp1 = 1 70 int_temp2 = 4 71 print(int_temp1.__lshift__(int_temp2)) 72 73 # 求模 x.__mod__(y) <==> x%y 74 print(num.__mod__(num2)) 75 76 # 相乘 x.__mul__(y) <==> x*y 77 print(num.__mul__(num2)) 78 79 # 取反 80 print(num.__neg__()) 81 82 # 不等於 83 print(num.__ne__(num2)) 84 85 # 取正數 ??? 86 print(num1.__pos__()) 87 88 # 乘方 89 print(num.__pow__(2)) 90 91 # 右加(以下首碼為r的都是右;首碼為l的都是左) 92 print(num.__radd__(num1)) 93 94 # 右或 95 print(int_temp1.__rand__(int_temp2)) 96 97 # 右除以左,返回商和餘數 98 print(per_page.__rdivmod__(all_pages)) 99 100 # 轉換為解釋器可讀取的形式 101 print(num.__repr__()) 102 103 # 轉換為字元串 104 print(num.__str__()) 105 print(type(num.__str__())) 106 107 # 求差 x.__sub__(y) <==> x-y 108 print(num.__sub__(num2)) 109 110 # x.__truediv__(y) <==> x/y 111 print(num.__truediv__(num2)) 112 113 # 異或 :按位不一樣的為真,一樣的為假。x.__xor__(y) <==> x^y 114 115 # 返回->表示該數字時占用的最少位數 116 print(bin(37)) 117 print((37).bit_length()) 118 119 # 返回->共軛數 120 int_temp1 = 37 121 print(int_temp1.conjugate()) 122 123 '''這倆方法暫時沒搞懂。。。 124 def from_bytes(cls, bytes, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 125 """ 126 int.from_bytes(bytes, byteorder, *, signed=False) -> int 127 128 Return the integer represented by the given array of bytes. 129 130 The bytes argument must be a bytes-like object (e.g. bytes or bytearray). 131 132 The byteorder argument determines the byte order used to represent the 133 integer. If byteorder is 'big', the most significant byte is at the 134 beginning of the byte array. If byteorder is 'little', the most 135 significant byte is at the end of the byte array. To request the native 136 byte order of the host system, use `sys.byteorder' as the byte order value. 137 138 The signed keyword-only argument indicates whether two's complement is 139 used to represent the integer. 140 """ 141 pass 142 143 def to_bytes(self, length, byteorder, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 144 """ 145 int.to_bytes(length, byteorder, *, signed=False) -> bytes 146 147 Return an array of bytes representing an integer. 148 149 The integer is represented using length bytes. An OverflowError is 150 raised if the integer is not representable with the given number of 151 bytes. 152 153 The byteorder argument determines the byte order used to represent the 154 integer. If byteorder is 'big', the most significant byte is at the 155 beginning of the byte array. If byteorder is 'little', the most 156 significant byte is at the end of the byte array. To request the native 157 byte order of the host system, use `sys.byteorder' as the byte order value. 158 159 The signed keyword-only argument determines whether two's complement is 160 used to represent the integer. If signed is False and a negative integer 161 is given, an OverflowError is raised. 162 """ 163 pass 164 '''int
二、長整型
三、浮點型
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Q1mi" 4 5 """ 6 float的內建方法 7 """ 8 9 # 返回分子分母數構成的元祖 10 f1 = 10.0 11 f2 = 0.0 12 f3 = -0.25 13 14 print(f1.as_integer_ratio()) 15 print(f2.as_integer_ratio()) 16 print(f3.as_integer_ratio()) 17 print(f3.as_integer_ratio()) 18 19 # 返回共軛複數 conjugate(self, *args, ***kwargs) 20 21 # 將十六進位數轉換為浮點數 22 print(float.fromhex('0x1.ffffp10')) 23 24 # 將浮點數轉換為十六進位數 25 print(2047.984375.hex()) 26 27 # 判斷浮點數是不是整數 28 f1 = 10.2 29 f2 = 10.0 30 print(f1.is_integer()) 31 print(f2.is_integer())float
四、字元串
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Q1mi" 4 5 """ 6 str類的內建方法練習 7 8 """ 9 name1 = 'alex' 10 name2 = str('ERIC') # 調用字元串類的__init__()方法 11 print(type(name1)) 12 print(dir(name1)) # 列印出類的成員 13 14 # 包含 15 result = name1.__contains__('ex') 16 print(result) 17 result = 'ex' in name1 18 print(result) 19 20 # 等於 21 print(name1.__eq__(name2)) 22 23 # 字元串按照format_spec格式化 24 """ 25 format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type] 26 27 fill ::= <any character> 28 29 align ::= "<" | ">" | "=" | "^" 30 31 sign ::= "+" | "-" | " " 32 33 width ::= integer 34 35 precision ::= integer 36 37 type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" 38 39 fill是表示可以填寫任何字元。 40 41 align是對齊方式,<是左對齊, >是右對齊,^是居中對齊。 42 43 sign是符號, +表示正號, -表示負號。 44 45 width是數字寬度,表示總共輸出多少位數字。 46 47 precision是小數保留位數。 48 49 type是輸出數字值是的表示方式,比如b是二進位表示;比如E是指數表示;比如X是十六進位表示。 50 """ 51 # 共20個字元,name1右對齊 52 print(name1.__format__('>20')) 53 54 # 返回小寫 55 result = name2.casefold() 56 print(result) 57 58 # 居中 59 # print('*' * 8 %s '*' * 8 % name1) 60 result = name1.center(20, '*') 61 print(result) 62 63 # 獲取指定標簽屬性的值 64 print(name1.__getattribute__('index')) 65 66 # x.__getitem__(y) <==> x[y](返回str[key]) 67 print(name1.__getitem__(1)) 68 69 # 大於等於 70 print(name1.__ge__(name2)) 71 72 # 大於 73 print(name1.__gt__(name2)) 74 75 # 哈希 76 print(name1.__hash__()) 77 78 # 返回一個字元串的迭代 79 for i in name1.__iter__(): 80 print(i) 81 82 # 返回字元串的長度 83 print(name1.__len__()) 84 85 # 首字母大寫 86 print(name1.capitalize()) 87 88 # 返回將小寫字元串轉換為大寫,大寫轉換為小寫 89 name3 = "AlEx" 90 print(name1.swapcase()) 91 print(name2.swapcase()) 92 print(name3.swapcase()) 93 94 # 返回str的小寫 95 print(name2.casefold()) 96 97 # 將字元串轉換為標題:每個單詞的首字母大寫 98 str_temp = "eric is humor!" 99 print(str_temp.title()) 100 101 # 居中 下例為:20個字元的寬度以'*'號填充,name1居中 102 print(name1.center(20, '*')) 103 104 # count 計數 105 str1 = 'adasafafdsfqadasddfa' 106 result = str1.count('a') 107 print(result) 108 109 # encode 編碼 110 result = name1.encode('gbk') 111 print(result) 112 113 # endswith 以'xxx'結束,後面可跟索引 114 result = str1.endswith('a') 115 print(result) 116 result = str1.endswith('s', 2, 4) 117 print(result) 118 119 # 替換tabs tabsize預設為8 120 name_temp = 'a\nlex' 121 print(name_temp.expandtabs()) 122 print(len(name_temp.expandtabs())) 123 124 # 查找 返回索引值 125 print(name1.find('e')) 126 127 # 格式化 128 str_temp = '{} is humor!' 129 print(str_temp.format(name2)) 130 131 # 按映射格式化 132 # format_map(self, mapping) 133 134 # 返回索引值 index(self, sub, start=None, end=None) 沒有返回0 135 print(name1.index('l')) 136 137 str_temp = 'a1b2c3' 138 # 判斷字元串是否是字母或數字 139 print(str_temp.isalnum()) 140 141 # 判斷字元串是否只是字母 142 print(str_temp.isalpha()) 143 144 # 判斷字元串中是否只包含十進位字元 145 str_temp = '0.3a0.4b' 146 str_temp1 = '0.3' 147 print(str_temp.isdecimal()) 148 print(str_temp1.isdecimal()) 149 150 # 判斷字元串中是否只有數字 151 str_temp = b'12345' 152 str_temp1 = '12345' 153 str_temp2 = '四' 154 print(str_temp.isdigit()) 155 print(str_temp1.isdigit()) 156 print(str_temp2.isdigit()) 157 158 print("line:148".center(20, "=")) 159 160 # 判斷是否是標識符 161 str_temp = 'class' 162 print(str_temp.isidentifier()) 163 164 # 判斷字元串中是否都是小寫 165 str_temp = 'eric' 166 str_temp1 = 'Eric' 167 print(str_temp.islower()) 168 print(str_temp1.islower()) 169 170 # 判斷字元串中是否都是數值型字元串 171 # 'bytes' object has no attribute 'isnumeric' 172 str_temp = '12345' 173 str_temp1 = '12345' 174 str_temp2 = '四' 175 str_temp3 = '壹佰貳拾叄' 176 print(str_temp.isnumeric()) 177 print(str_temp1.isnumeric()) 178 print(str_temp2.isnumeric()) 179 print(str_temp3.isnumeric()) 180 181 # 判斷是否可被列印 182 str_temp = 'abc' 183 print(str_temp.isprintable()) 184 185 # 是否為空格 186 str_temp = ' ' 187 print(str_temp.isspace()) 188 189 # 判斷是否為標題 (所有首字母大寫) 190 str_temp = 'eric is humor!' 191 str_temp1 = 'Eric Is Humor!' 192 print(str_temp.istitle()) 193 print(str_temp1.istitle()) 194 195 print('line:185'.center(20, '=')) 196 197 # 判斷字元串是否全為大寫 198 str_temp = 'eric is humor!' 199 str_temp1 = 'Eric Is Humor!' 200 str_temp2 = 'ERIC IS HUMOR!' 201 print(str_temp.isupper()) 202 print(str_temp1.isupper()) 203 print(str_temp2.isupper()) 204 205 # 字元串的拼接 join(self, iterable) 206 str_temp = "ERIC" 207 print(':'.join(str_temp)) 208 209 # 左對齊 210 str_temp = "eric" 211 print(str_temp.ljust(20, '*')) 212 # 右對齊 213 print(str_temp.rjust(20, '*')) 214 215 # 將字元串轉換為小寫 216 str_temp = "ERIC" 217 print(str_temp.lower()) 218 # 將字元串轉換為大寫 219 str_temp1 = "eric" 220 print(str_temp1.upper()) 221 222 # strip()移除字元串前後的指定字元;lstrip()移除左邊指定字元 預設為移除空格;rstrip()移除右邊指定字元 223 str_temp = " eric is humor ! " 224 print(str_temp.strip()) 225 print(str_temp.lstrip()) 226 print(str_temp.rstrip()) 227 str_temp1 = "eric is humor!" 228 print(str_temp1.lstrip('eric')) 229 print(str_temp1.rstrip('mor!')) 230 231 # maketrans(self, *args, **kwargs)和translate() 232 str_temp = '654321123789' 233 map_temp = str.maketrans('123', 'abc') 234 map_temp1 = str.maketrans('123', 'abc', '789') 235 print(str_temp.translate(map_temp)) 236 print(str_temp.translate(map_temp1)) 237 238 # 按照指定分隔符將字元串分割,返回元祖 239 # 如果字元串包含指定的分隔符,則返回一個三元的元組,第一個為分隔符左邊的子串,第二個為分隔符本身,第三個為分隔符右邊的子串。 240 # 如果字元串不包含指定的分隔符,則返回一個三元的元祖,第一個為字元串本身,第二個、第三個為空字元串。 241 str_temp = "www.google.com" 242 print(str_temp.partition('google')) 243 print(str_temp.partition('www')) 244 print(str_temp.partition('wxw')) 245 246 # 右側開始分隔 247 print(str_temp.partition('o')) 248 print(str_temp.rpartition('o')) 249 250 # Python replace() 方法把字元串中的 old(舊字元串) 替換成 new(新字元串),如果指定第三個參數max,則替換不超過 max 次。 251 str_temp = "www.google.com" 252 print(str_temp.replace('.', '-', 1)) 253 254 # 右查找 255 str_temp.rfind('.') 256 257 # 右索引 258 str_temp.rindex('.') 259 260 # 分割 261 str_temp = "www.google.com" 262 print(str_temp.split('o', 2)) 263 # 右分隔 264 print(str_temp.rsplit('o', 2)) 265 266 print('line:252'.center(20, '=')) 267 268 # 行分割 269 str_temp = """ 270 eric 271 is 272 humor 273 ! 274 """ 275 print(str_temp.splitlines()) 276 277 # 以什麼開始 278 str_temp = "eric is humor!" 279 print(str_temp.startswith('eric')) 280 281 # 返回指定長度的字元串,原字元串右對齊,前面補0 282 str_temp = "23" 283 print(str_temp.zfill(20))str
五、列表
1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # __author__ = "Q1mi" 4 5 """ 6