Python:常用函數(shù)封裝:
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了沾化免費(fèi)建站歡迎大家使用!
def is_chinese(uchar):
"""判斷一個(gè)unicode是否是漢字"""
if uchar = u'\u4e00' and uchar=u'\u9fa5':
return True
else:
return False
def is_number(uchar):
"""判斷一個(gè)unicode是否是數(shù)字"""
if uchar = u'\u0030' and uchar=u'\u0039':
return True
else:
return False
def is_alphabet(uchar):
"""判斷一個(gè)unicode是否是英文字母"""
if (uchar = u'\u0041' and uchar=u'\u005a') or (uchar = u'\u0061' and uchar=u'\u007a'):
return True
else:
return False
def is_other(uchar):
"""判斷是否非漢字,數(shù)字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False
def B2Q(uchar):
"""半角轉(zhuǎn)全角"""
inside_code=ord(uchar)
if inside_code0x0020 or inside_code0x7e: #不是半角字符就返回原來(lái)的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式為:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)
def Q2B(uchar):
"""全角轉(zhuǎn)半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code0x0020 or inside_code0x7e: #轉(zhuǎn)完之后不是半角字符返回原來(lái)的字符
return uchar
return unichr(inside_code)
def stringQ2B(ustring):
"""把字符串全角轉(zhuǎn)半角"""
return "".join([Q2B(uchar) for uchar in ustring])
def uniform(ustring):
"""格式化字符串,完成全角轉(zhuǎn)半角,大寫轉(zhuǎn)小寫的工作"""
return stringQ2B(ustring).lower()
def string2List(ustring):
"""將ustring按照中文,字母,數(shù)字分開(kāi)"""
retList=[]
utmp=[]
for uchar in ustring:
if is_other(uchar):
if len(utmp)==0:
continue
else:
retList.append("".join(utmp))
utmp=[]
else:
utmp.append(uchar)
if len(utmp)!=0:
retList.append("".join(utmp))
return retList
分兩步:定義函數(shù)和調(diào)用函數(shù)。
1.定義函數(shù)用def關(guān)鍵字,然后定義函數(shù)名和入?yún)?,以及函?shù)執(zhí)行語(yǔ)句。
2.通過(guò)函數(shù)名調(diào)用函數(shù)即可,需要傳入?yún)?shù)的話需要加上參數(shù)值
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
a = "2/9-3"
print eval(a)
-3
b = "2.0/9-3"
print eval(b)
-2.77777777778
看看a和b的區(qū)別,能解決你的問(wèn)題嗎
def multi(n):
f=1
for i in range(1,n+1):
f = f*i
print(f)
num = int(input("Input number:"))
multi(num )
honey@DESKTOP-H6QG9QG:~% python3 mypy.py
Input number:5
120