A case study of Python's more than 10 grammatical knowledge points

Source: Internet
Author: User

Typical code:
1. Comments
2. Function call
3. function return value with parameters
4. Return statement with no returned value
5. Assignment and use of dictionary variables
6. txt file read
7. File variables
8. Exception Handling
9. Output results
10. List sorting
11. String segmentation
12, empty character processing
13. Judgment
14. Circulation

This is a typical case code

def sanitize (time_string): #函数定义及实参传递
If '-' in time_string: #判断一个字符串中是否存在 '-' pay; Judgment statement
Splitter = '-'
Elif ': ' in time_string: #elif语句
Splitter = ': '
else: #else语句
Return (time_string) #带参数的函数返回语句
(mins,secs) = Time_string.split (splitter) #多个变量的自动赋值, string split
Return (mins+ '. ') +SECS) #参数计算后的函数返回语句

def get_coach_data (filename):
Try: #先尝试
with open (filename) as F: #打开文件
data = F.readline () #按行读取文件内容并送至变量data中
Templ = Data.strip (). Split (', ') #取消空字符, string split
Return ({' Name ': Templ.pop (0), #字典变量的赋值,
' DOB ': Templ.pop (0),
' Times ': Str (sorted (Set ([Sanitize (T) in Templ]) [0:3])}) #排序, loop, take the first 3 characters of the content
Except IOERROR as Ioerr: #出现异常后的异常处理语句
Print (' File error: ' +str (Ioerr))
Return (None)

James = Get_coach_data (' james2.txt ') #文件变量

Print (james[' name ']+ "s fasttest Times is" +james[' Times ']) #输出

A case study of Python's more than 10 grammatical knowledge points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.