Python Learning Notes

Source: Internet
Author: User

Recently in the study of Python, the use of the process has a list and dict declaration is very special, under this note

The dictionary is a mutable object, the initialization must not be placed in front of the for loop before the list declaration must be placed at the beginning of the program (the list is also mutable object, but do not know why it must be declared at the beginning) code if names=[] is placed inside the for loop, its names.append () After that, print names always prints only the last value, and the Declaration of dic={} in the following code is initially placed in front of the For loop, which also causes the dictionary table to print for the last time the following code is the subject of the practice Vamei teacher, Returns the name of a person with a score less than 60 in the text Record.txt content as follows:
# name, age, score  19,56,.
I finished my own research and wrote this paragraph:
Def readfiles ():    l=[]    names=[]    f=open ("E:\\record.txt", "R") for line in    F:        dic={}        if line[0 ]== "#":            li0=line[1:-1].split (",")            #print li0        Else:            li1=line[0:-1].split (",")           #print Li1            dic["name"]=li1[0]            dic["age"]=li1[1]            dic["score"]=li1[2]            l.append (DIC)    print L    F.close () for    I in range (len (L)):        #print l[i]["score"]        #names =[]        if l[i]["score"]< "60":            names.append (l[i]["name"])            #print names    print names    

Vamei Teacher's tutorial is good, writing simple and clear, suitable for beginners to get started. Http://www.cnblogs.com/vamei/tag/Python/default.html?page=3

Python Learning Notes

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.