python小函数
1、sum()
sum() 方法對(duì)系列進(jìn)行求和計(jì)算。
以下是 sum() 方法的語法:
sum(iterable[, start])(iterable[, start])- iterable -- 可迭代對(duì)象,如列表。
- start -- 指定相加的參數(shù),如果沒有設(shè)置這個(gè)值,默認(rèn)為0。
返回計(jì)算結(jié)果。
以下展示了使用 sum 函數(shù)的實(shí)例:
>>>sum([0,1,2]) 3 >>> sum((2, 3, 4), 1) # 元組計(jì)算總和后再加1 10 >>> sum([0,1,2,3,4], 2) # 列表計(jì)算總和后再加2 122、strip()
描述
Python strip() 方法用于移除字符串頭尾指定的字符(默認(rèn)為空格)。
語法
strip()方法語法: str.strip([chars]);
參數(shù):chars -- 移除字符串頭尾指定的字符。
返回值:返回移除字符串頭尾指定的字符生成的新字符串。
3、enumerate()
Python內(nèi)置的enumerate函數(shù)可以把一個(gè)list變成索引-元素對(duì),這樣就可以在for循環(huán)中同時(shí)迭代索引和元素本身
>>> for i, value in enumerate(['A', 'B', 'C']): ... print(i, value) ... 0 A 1 B 2 C
4、isinstance函數(shù)可以判斷一個(gè)變量是不是Iterable對(duì)象
5、count()
count() 方法用于統(tǒng)計(jì)字符串里某個(gè)字符出現(xiàn)的次數(shù)。可選參數(shù)為在字符串搜索的開始與結(jié)束位置。
count()方法語法:
str.count(sub, start= 0,end=len(string))- sub -- 搜索的子字符串
- start -- 字符串開始搜索的位置。默認(rèn)為第一個(gè)字符,第一個(gè)字符索引值為0。
- end -- 字符串中結(jié)束搜索的位置。字符中第一個(gè)字符的索引為 0。默認(rèn)為字符串的最后一個(gè)位置。
該方法返回子字符串在字符串中出現(xiàn)的次數(shù)。
以下實(shí)例展示了count()方法的實(shí)例:
#!/usr/bin/python3str="www.runoob.com" sub='o' print ("str.count('o') : ", str.count(sub)) sub='run' print ("str.count('run', 0, 10) : ", str.count(sub,0,10))?
以上實(shí)例輸出結(jié)果如下:
str.count('o') : 3 str.count('run', 0, 10) : 16、replace()
Python replace() 方法把字符串中的 old(舊字符串) 替換成 new(新字符串),如果指定第三個(gè)參數(shù)max,則替換不超過 max 次。
replace()方法語法:
str.replace(old, new[, max]).replace(old, new[, max])- old -- 將被替換的子字符串。
- new -- 新字符串,用于替換old子字符串。
- max -- 可選字符串, 替換不超過 max 次
返回字符串中的 old(舊字符串) 替換成 new(新字符串)后生成的新字符串,如果指定第三個(gè)參數(shù)max,則替換不超過 max 次。
以下實(shí)例展示了replace()函數(shù)的使用方法:
#!/usr/bin/pythonstr = "this is string example....wow!!! this is really string"; print str.replace("is", "was"); print str.replace("is", "was", 3);#以上實(shí)例輸出結(jié)果如下: thwas was string example....wow!!! thwas was really string thwas was string example....wow!!! thwas is really string7、python十進(jìn)制數(shù)轉(zhuǎn)其他進(jìn)制
以下代碼用于十進(jìn)制轉(zhuǎn)二進(jìn)制,八進(jìn)制,十六進(jìn)制
# -*- coding: UTF-8 -*-# Filename : test.py # author by : www.runoob.com# 獲取用戶輸入十進(jìn)制數(shù) dec = int(input("輸入數(shù)字:"))print("十進(jìn)制數(shù)為:", dec) print("轉(zhuǎn)換為二進(jìn)制為:", bin(dec)) print("轉(zhuǎn)換為八進(jìn)制為:", oct(dec)) print("轉(zhuǎn)換為十六進(jìn)制為:", hex(dec)) 執(zhí)行以上代碼輸出結(jié)果為:#python3 test.py #輸入數(shù)字:5 #十進(jìn)制數(shù)為:5 #轉(zhuǎn)換為二進(jìn)制為: 0b101 #轉(zhuǎn)換為八進(jìn)制為: 0o5 #轉(zhuǎn)換為十六進(jìn)制為: 0x5負(fù)數(shù)的補(bǔ)碼表示,例如用二進(jìn)制補(bǔ)碼表示 -n
b = bin(2**32+n)其他進(jìn)制轉(zhuǎn)十進(jìn)制:
a? = int('11110',base = 2)? //二進(jìn)制轉(zhuǎn)十進(jìn)制
a? = int('7810',base = 8)? // 八進(jìn)制轉(zhuǎn)十進(jìn)制
a? = int('56AE0',base = 16)? // 十六進(jìn)制轉(zhuǎn)十進(jìn)制
8.pyhon? isalpha() 方法
Python isalpha() 方法檢測(cè)字符串是否只由字母組成。
用法:str.isalpha()
參數(shù):無
返回值:如果字符串至少有一個(gè)字符并且所有字符都是字母則返回 True,否則返回 False
#!/usr/bin/pythonstr = "this"; # No space & digit in this string print str.isalpha();str = "this is string example....wow!!!"; print str.isalpha();########### True False ###########?
總結(jié)
- 上一篇: 银行利息2022年最新存款利率表,各大银
- 下一篇: Python切片各种情况详解