Python Learning Path 3

Source: Internet
Author: User
Tags new set readable shallow copy

1. Dictionary operation

A dictionary of Key-value data types.

Grammar:

1info = {2     'Car01':"Camry",3     'Car02':"Teana",4     'car03':"Accord",5}

Features of the dictionary:

    • Dictionaries are unordered.
    • Key must be a unique

Increase

>>> info["car04"] ="Q50">>>info{'Car01':'Camry','Car02':'Teana','car03':'Accord','car04':'Q50'}

Modify

>>> info["Car01"] ="Camry">>>info{'Car01':'Camry','Car02':'Teana','car03':'Accord','car04':'Q50'}

Delete

>>>info{'Car01':'Camry','Car02':'Teana','car03':'Accord','car04':'Q50'}>>> Info.pop ("Car01")'Camry'>>>info{'Car02':'Teana','car03':'Accord','car04':'Q50'}
>>>info{'Car02':'Teana','car03':'Accord','car04':'Q50'}>>>delinfo["Car02"]>>>info{'car03':'Accord','car04':'Q50'}
#Random Delete>>>info{'car03':'Accord','car04':'Q50'}>>>Info.popitem () ('car04','Q50')>>>info{'car03':'Accord'}

Find

>>>info{'Car01':'Camry','Car02':'Teana','car03':'Accord'}>>>"Car01" inchinfotrue>>> Info.get ("Car01")'Camry'>>> info["Car01"]'Camry'>>> info["car04"]#when there is no key, get get no error display null valueTraceback (most recent): File"<stdin>", Line 1,inch<module>Keyerror:'car04'>>> Info.get ("car04")

Cycle

# Method 1  for inch Info:     Print (Key,info[key]) # Method 2  for inch # will first turn the dict into a list, the data in the big time Mo    Print (K,V)
2. Collection

A collection is an unordered, non-repeating combination of data, and its main functions are as follows:

1, go to the weight, put a list into a set, it automatically go heavy

2, the relationship test, test two sets of data before the intersection, difference sets, and the relationship between the set

s = set ([3,5,9,10])#create a collection of valuesT= Set ("Hello")#Create a collection of unique charactersa= T | S#the set of T and Sb= T & S#intersection of T and SC= T–s#differential Set (items in T, but not in s)D= T ^ s#symmetric difference sets (items in t or S, but not both)Basic operation: T.add ('x')#Add an itemS.update ([10,37,42])#adding multiple items in Suse Remove () to delete an item: T.remove ('H'Len (s) set length xinchs tests if X is a member of S X not inchs tests if X is not a member of S S.issubset (t) s<=T Test if every element in S is in T S.issuperset (t) s>=T Test if every element in T is in S s.union (t) s|T returns a new set containing each element in S and T S.intersection (t) s&T returns a new set containing the common elements in S and T S.difference (t) s-T returns a new set containing an element in s but not in T S.symmetric_difference (t) s^T returns a new set containing a non-repeating element in S and T S.copy () returns a shallow copy of the set "s"

3. File operation

Procedure for file operation

1. Open the file, get the file handle and assign a value to a variable

2. Manipulate the file through a handle

3. Close the file

Basic operations

f = open ('Lyrics')#Open FileFirst_line =F.readline ()Print('First line :', First_line)#read a linePrint('I'm the dividing line .'. Center (50,'-')) Data= F.read ()#Read all the rest of the content, do not use when the file is largePrint(data)#Print Filesf.close ()#Close File

The mode of opening the file is:

    • R, read-only mode (default).
    • W, write-only mode. "unreadable; not exist; create; delete content;"
    • A, append mode. "Readable; not exist" create; "only append content;"

"+" means you can read and write a file at the same time

    • r+, can read and write files. "readable; writable; can be added"
    • w+, write and read
    • A +, with a

"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading

    • RU
    • R+u

"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)

    • Rb
    • Wb
    • Ab

With statement

To avoid forgetting to close a file after opening it, you can manage the context by:

With open ('log','r') as F:         ...

This way, when the with code block finishes executing, the internal automatically shuts down and frees the file resource.

After Python 2.7, with also supports the management of multiple file contexts simultaneously, namely:

With open ('log1') as Obj1, open ('log2') as Obj2:     Pass

Python Learning Path 3

Related Article

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.