Python Learning notes (3)

Source: Internet
Author: User

Tag: value does not repeat copy span mode Val built-in function simple Tom

1. Tuples

The tuple's definition symbol is (), and the element definition is exactly the same as the list. The difference is that the contents of the tuple are immutable.

2. Dictionaries

The contents of the dictionary are unordered.

The element form of a dictionary is Key:value

Key definition rules: key is immutable. and is unique in the dictionary.

Definition rule for value: any type

Operation of the Dictionary:

1 #Enquiry2 Dic[key] Error if there is a null value3Dic.get ('Key'If there is a null value, return none4 5 #Add/Modify6dic['Gender'] ='Famale'7dic['name'] ='Tom'8 The above operation Dictionary does not have this element added to the dictionary. If any, modify the value9 Ten #Delete One deldic['Key']
Dic.pop (' key ')

Built-in functions for dictionaries:

1 #Empty Dictionary2 dic.clear ()3 4 #quickly generate a dictionary based on the parameters given later5 Dic.fromkeys ()6 7 #take the key value. No key returns none8 Dic.get ()9 Ten #generating tuples in k,v form One Dic.items () A  - #Make a list of all the keys - Dic.keys () the  - #Make a list of all the value - dic.values () -  + #deletes the specified key - Dic.pop () +  A #randomly delete a key at Dic.popitems () -  - #add key, when key exists, returns key, does not exist and is added to the dictionary based on subsequent values - Dic.setdefault () -  - #merges two dictionaries. If you have duplicate key. inDic.update ()
The dictionary's depth copy. Shallow copy

When you need to copy a dictionary, a simple copy will only copy the first layer of data (key and value), followed by a direct reference to the old data, through the ID () can be viewed.

Deep copy

You need to import a copy module. Use the Deepcopy method of the Copy module. You can regenerate a completely separate dictionary.

3. Collection

Set holds a set of distinct values.

Method of Set

1Set1&set2#ask for intersection2Set1-set2#Finding the difference set3Set1.difference_update (Set2)#Finding the difference set4Set1|set2#Seek and set5Set1^set2#Seeking symmetry intersection6Set1<=set2#is a subset7Set1>=set2#whether it is a parent set8Set.update#Serialization Updates9Set.add ()#Add updates as a wholeTenSet.pop ()#randomly delete a value OneSet.remove ()#Delete specified value, no value error ASet.discard ()#deletes the specified value without a value of none -Set.clear ()#emptying the collection
4. File processing

Open (' filename ', ' r ') #以只读方式打开一个文件

How to handle files
1 #Common Methods2F.encodeing ()#Print character encoding3F.closed ()#determine if the file is closed4F.fileno ()#returns the file number of the operating system5F.flush ()#forcing data to be flushed to the hard disk6F.isatty ()#determine if the file is a TTY interface file7F.name ()#Print file name8F.seek ()#move the cursor position9F.tell ()#where the print cursor is locatedTenF.truncate ()#truncate One  A #Increase -F=open ('Myfile','W')#Create write mode -F=open ("myfile",'a')#Append write mode theF=open ("myfile",'r+')#read/write mode -F=open ("myfile",'w+')#read-write mode -F=open ("myfile",'A +')#Append read mode -F.close ()#Saving and closing files +  - #Delete/Change + There is currently no way to directly edit a file A write a script to read the file line by row. Save the required content as a new file. at Rename it back again. -  - #Read - F.readline () read a line -F.readlines () read the entire file

Python Learning notes (3)

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.