Continue the basics of Python three

Source: Internet
Author: User

Objective

Always want to write something in front, to record the process of learning, I have to admit that Python does not learn, but, not to learn, I am convinced of this truth. So I'll stick to it.

Specific content last review:

1, list operation can be deleted or modified, Ganso is a read-only list, Ganso and lists are ordered.

2, the string cannot be modified.

For example, turning a string into uppercase is an update of the entire string, which is tantamount to producing a new data. The list is modified in its original location.

3, the list can nest anything.

4, the characteristics of the dictionary: unordered (because by key to find value, do not need to find through the small standard, the bottom through the hash algorithm to find), can nest many layers.

First, the collection

The simplest use is to go to the heavy, relational test.

1, go to reset set

#!/usr/bin/-*-coding:utf-8 -*-= [1,4,5,7,3 ,6,7,9set(list_1) print (List_1,type (list_1))

Output results

{1345679} <class'set  '>

Description

This is a collection, a bit like a dictionary, but it is not, it is unordered.

2. Cross intersection, Intersection

#!/usr/bin/Env python#-*-coding:utf-8-*-#Author: Leon xielist_1= [1,4,5,7,3,6,7,9] #上面有重复数据, I'm going to weigh the list and turn it into a collection list_1=Set(list_1) list_2=Set([2,6,0, the, A,8,4]) #我加了一个list_2, and list_1 have cross-content, this time I want to take their intersection #print (list_1,list_2) print (List_1.intersection (list_2) )

Output results

{46}

3. Union set

#!/usr/bin/Env python#-*-coding:utf-8-*-#Author: Leon xielist_1= [1,4,5,7,3,6,7,9] #上面有重复数据, I'm going to weigh the list and turn it into a collection list_1=Set(list_1) list_2=Set([2,6,0, the, A,8,4]) #并集print (list_1.union (list_2) )

Output results

{01234567the9 8 +}

4, Difference set difference

#!/usr/bin/Env python#-*-coding:utf-8-*-#Author: Leon xielist_1= [1,4,5,7,3,6,7,9] #上面有重复数据, I'm going to weigh the list and turn it into a collection list_1=Set(list_1) list_2=Set([2,6,0, the, A,8,4]) #差集, list_1 inside some list_2 print (List_1.difference (list_2)) output {1,3,5,9,7}

5. Subset Issubset

list_1 = [1,4,5,7,3,6,7,9] #上面有重复数据, I turn the list to heavy, it becomes a set list_1 = Set(list_1) list_2 = set ([2,6,0,66,22,8,4]) 

#子集print (List_1.issubset (list_2))

Output results
False

6. Parent Set Issuperset

List_1 = [1,4,5,7,3,6,7,9] #上面有重复数据, I turn the list to heavy, it becomes a set list_1 = Set (list_1) list_2 = set ([2,6,0,66,22,8,4])
#父集print (List_1.issuperset (list_2))
Output results
False

7. Reverse differential Set

List_1 = [1,4,5,7,3,6,7,9] #上面有重复数据, I turn the list to heavy, it becomes a set list_1 = Set (list_1) list_2 = set ([2,6,0,66,22,8,4])

#反向差集print (List_1.symmetric_difference (list_2))
Print results
{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}
Second, the file operation

Print files

First I create a new file, is a good prose

If you are touched, I would like the wind when love has passed, take office as the rain of the tears of lilac flowers Open the end of the season sacrifice we lost the love of the wind waiting for the time, you are no longer young if love has the providence of love Red Carnation Stop Your World
View Code

I open it and print it and need to be careful to specify what format to use to open it

#!/usr/bin/-*-coding:utf-8 -*-= open ("yesterday", encoding= " Utf-8 " ). Read () print (data)
View Code

Specific steps:

The open file is assigned to the object, and then the object is manipulated.

#!/usr/bin/-*-coding:utf-8 -*-= open ("yesterday", encoding= " Utf-8 "  == f.read () print (data) print ('===data2===='  ) Printing results: If you are touched, I would like the wind when the love has passed, take office as the rain of the Tears Lilacs bloom the end of the season sacrifice we lost the love of the wind waiting for the time, you are no longer young if love has the providence of love Red Carnation Stop Your World ===data2====
View Code

Here I make a small change, as follows:

#!/usr/bin/Env python#-*-coding:utf-8-*-#Author: Leon xie# file handle F= Open ("Yesterday", encoding="Utf-8") Data=f.read () data2=F.read () print (data) print ('===data2====%s==='%data2) Printing results If you are touched, I would like the wind when the love has passed, take office as the rain Tears lilac flowers Open the end of the season sacrifice we lost the love of the wind waiting, you are no longer young if love has the providence of love Red Carnation Stop Your World===data2=======explanation: Why did the last data2 not read it at this time? Because at the beginning of the reading process, it is read from the top down, read the end, the cursor is already at the bottom, this time you call this variable data2, it is equivalent to then from the cursor position read down, so there is no, this time, need to do is to move the cursor up. 
View Code

Need to explain:

W mode allows you to create a new file.

Write files, for example:

#-*-coding:utf-8 -*-= open ("yesterday",'w' , encoding="utf-8") #我往里面写句话f. Write (" I'm a big muscle pa, \ n " f.write (" I love Beijing Cheonan ") output it will wash away the previous text and then generate new content I am a big muscle pa, I love Beijing Tiananmen 
View Code

Summary:

R: Read-only mode

W: Write-only mode, which flushes out the original text and then re-writes

A: Append, can write, but cannot read.

Loop file, line tenth print

f = open ("Yesterday",'R', encoding="Utf-8") forIndex,lineinchEnumerate (F.readlines ()):ifindex = =9: Print ("===fengefu==")        Continueprint (Line.strip ()) Description: Use subscript to determine the first line

There is also a simple way to write, which can be ignored

f = open ("Yesterday",'R', encoding="Utf-8") Count=0 forLineinchF:ifCount = =9: Print ("= = I am the dividing line = =") Count+=1        Continueprint (line) Count+=1
Third, higher order function

Example:

#!/usr/bin/-*-coding:utf-8 -*-#Author: Leon xie# most common function def add (a,b,f):    return F (a) +f (b) #f (ABS), ABS is a function of absolute value, will take this function to deal with these 2 numbers. is 3+= Add (3,-6, ABS) print (RES)

Output results
9

Iv. Summary

1, file processing changes, can only read into memory or create a new file

2, function definition, call, position parameter, key parameter, default parameter, parameter group

3. Ganso and Dictionaries

4. Global variables

5. Character encoding

Continue the basics of Python three

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.