一个案例说出python的十余个语法知识点
典型代碼:
1、注釋
2、函數調用
3、帶參數的函數返回值
4、無返回值的return語句
5、字典變量的賦值和使用
6、txt文件讀取
7、文件變量
8、異常處理
9、輸出結果
10、列表排序
11、字符串分割
12、空字符處理
13、判斷
14、循環
這是一個典型案例代碼
def sanitize(time_string): #函數定義及實參傳遞
if '-' in time_string: #判斷一個字符串中是否存在'-'支付;判斷語句
splitter = '-'
elif ':' in time_string: #elif語句
splitter = ':'
else: #else語句
return(time_string) #帶參數的函數返回語句
(mins,secs) = time_string.split(splitter) #多個變量的自動賦值,字符串分割
return(mins+'.'+secs) #參數計算后的函數返回語句
def get_coach_data(filename):
try: #先嘗試
with open(filename) as f: #打開文件
data = f.readline() #按行讀取文件內容并送至變量data中
templ = data.strip().split(',') #取消空字符,字符串分割
return({'name':templ.pop(0), #字典變量的賦值,
'dob':templ.pop(0),
'times':str(sorted(set([sanitize(t) for t in templ]))[0:3])}) #排序、循環、取前3個字符內容
except IOERROR as ioerr:· #出現異常后的異常處理語句
print('File error:'+str(ioerr))
return(None)
james = get_coach_data('james2.txt') #文件變量
print(james['name']+"'s fasttest times are "+james['times']) #輸出
轉載于:https://blog.51cto.com/eagle6899/2146973
總結
以上是生活随笔為你收集整理的一个案例说出python的十余个语法知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Join的加强版CountDownLat
- 下一篇: 移动硬盘格式化了的资料找回方法