First, the collection
1. What is a collection
Dict's role is to establish a set of keys and a set of value mappings, sometimes we just want to dict key, do not care about the value of key corresponding, then the collection comes in handy. Set set is an unordered, non-repeating combination of data. Its main functions are:
- Deduplication, the elements in set are not duplicates, they are unordered.
- Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set
The way to create a set is to call set () and pass in a list,list element as the set element:
1 list_1=[1,2,3,'oldboy','Alex' 2 list_1=set (list_1)3print(list_1)
Operation Result:
' Alex ' ' Oldboy '}
2. Collection Updates
There are two types of updates to a collection: element additions and deletions
When adding an element, with the Add () method of the set, if the added element already exists in the set, add () will not error, but will not be added:
1 s=set ([oldboy, 'Alex'])2 s.add (' Golden Horn King ')3print(s)
Operation Result:
{'Alex'oldboy ' Golden Horn King '}
Delete element set has the Remove method, if the deleted element does not exist, an error will be:
1 s=set ([oldboy, 'Alex'])2 s.remove ('Alex')3print(s)
Operation Result:
' Oldboy '}
3. Relationship Testing
(1) Take the intersection of two sets
1 s1=set ([oldboy, 'Alex'])2 S2=set ([2,4,6,'Alex',' Golden Horn King ']) 3 Print (S1 & S2)
Operation Result:
{'Alex', 2}
(2) Take a set of two sets
1 s1=set ([oldboy, 'Alex'])2 S2=set ([2,4,6,'Alex',' Golden Horn King ']) 3 Print (S1 | s2)
Operation Result:
' Alex ' ' Oldboy ' ' Golden Horn King '}
(3) Take the difference set of two sets:
1 s1=set ([oldboy, 'Alex'])2 S2=set ([2,4,6,'Alex',' Golden Horn King ']) 3 Print (S1-S2)
Run the results to print elements that belong to S1 but not S2:
{'oldboy', 1, 3}
Ii. Documents
The reading and writing of text files is mainly implemented by the objects constructed by open ().
1. Create a File object
Open a file and use an object to represent the file: F = open (file name, mode), commonly used modes are:
- 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
Example: Read the contents of the original file into a new file, and the "wanton happiness and so I enjoy" this sentence replaced by "wanton happiness, such as Alex to enjoy"
1F=open ('Yesterday.txt','R')2f_new = open ('Yesterday_new.txt','W', encoding='Utf-8')3 forLineinchF:4 if 'I enjoy the wanton pleasures.' inchLine :5line = Line.replace ('I enjoy the wanton pleasures.','The wanton pleasures of Alex to enjoy')6 F_new.write (line)7 f.close ()8F_new.close ()
Third, function
1. Definition : A function is to encapsulate a set of statements by a name (function name), in order to execute the function, simply call the name of its functions. Define a function to use the def statement, write down the function name, parentheses, the arguments in parentheses, and the colon: and then, in the indent block, write the function body, and The return value of the function is returned with a return statement.
1 def Test (): 2 Print ('Hello Golden Horn King ') 3 Test ()
Operation Result:
Hello Gold Horn King
2. function parameter passing
(1) keyword parameter: Pass the parameter according to the keyword, independent of the position of the formal parameter
1 def Test (x, y): 2 Print (x) 3 Print (y) 4 Test (x=' Golden Horn King ', y=' silver horn King ')
Operation Result:
Golden Horn King Silver Horn King
(2) position parameter: According to the position of the formal parameter, do not write the keyword, only write the actual parameters, pass the parameter order must be consistent with the formal parameter order
1 defTest (x, y):2 Print('x=', X)3 Print('y=', y)4Test'Golden Horn King','Silver Horn King')5Test'Silver Horn King','Golden Horn King')
Operation Result:
x= Golden Horn King y= silver Corner King x= silver angle King y= Golden Horn King
(3) Default value parameter: The function's default parameter is to simplify the call by simply passing in the necessary parameters. However, when needed, additional parameters can be passed in to override the default parameter values.
1 def Test (x,y=0):2 print('x=', x) 3 Print ('y=', y) 4 Test (1)# pass the first parameter only, the second parameter takes the default value of 5 test (8,9)# after assigning a value to a parameter, overriding default parameters
Operation Result:
x= 1y= 0x= 8y= 9
(4) Variable parameter: The name of the variable parameter is preceded by an * number, we can pass in 0, one or more parameters to the variable parameter, the Python interpreter will assemble a set of parameters into a tuple passed to the variable parameter, therefore, inside the function, directly to the parameter as a Tuple has:
1 def Test (*args):2 print(args)3 Test (All-in-all) 4 Test (1,'hello')
Operation Result:
(1, 2, 3) ('hello')
There is also a multivalued parameter, preceded by a * * (double star), all other key parameters other than the normal parameter will be placed in a dictionary passed to the function, such as:
1 def Test (* *args):2 print(args)3 Test (name=' Alex', age=30,ginder='M')
Operation Result:
{'ginder' 'M'name ' Alex ' ' Age ': 30}
Combination parameter: Keyword parameter cannot be written in front of position parameter
1 defTest (name, age=18, *args, * *Kargs):2 Print(name)3 Print(age)4 Print(args)5 Print(Kargs)6 7Test'Jim', 15, 16,'Hello', sex='M', hooby='Car')
Operation Result:
Jim('hello') {'sex' M'hooby'car}
Python Learning--day3