defmain():if __name__ =='__main__':print('This program is run directly')elif __name__ =='text3':print('this program is run as a module')
main()# main() 是考点
import re
defcheckModify(s):return re.sub(r'\b(\w)(\w+)(\w)\b',lambda x : x.group(1)+ x.group(2).lower()+ x.group(3),s)
s ='aBc HJSH H eee fEEfff'print(checkModify(s))
[‘111’, ‘22’, ‘3’, ‘’] //[‘111’, ‘22’, ‘3’, ‘’]
6 //111223
对
返回字符的最后出现
deflastAppear(s):
result =[]for ch in s :if ch in result :
result.remove(ch)
result.append(ch)return result
from math import pi as PI
defCircleArea(r):# 确保数值正确ifisinstance(r,(int,float)):return PI*r*r
else:print('You must enter an integer or float as radius')
import random
data =[random.randrange (50)for i inrange(20)]print(data)
data[:10]=sorted(data[:10])
data[11:]=sorted(data[10:],reverse=True)# reverse = True 表示降序排序print(data)