Python Automation Development 03

Source: Internet
Author: User
Tags readable

Collection

1, go to Heavy

List_1 = Set ([1,2,3,4,0,7,4,8])

List2 = Set ([99,34,6,8,3])

LIST3 = Set ([0,4,8])

LIST4 = Set ([84,45,49])

Print (list_1) <<< {0, 1, 2, 3, 4, 7, 8}

2. Intersection

List_1 & List2

Print (List_1.intersection (list2)) <<< {8, 3}

3. Co-set (collection)

list_1 | List2

Print (List_1.union (list2)) <<< {0, 1, 2, 3, 4, 99, 34, 7, 8, 6}

4. Subsets

Print (List_1.issubset (list2)) <<< False

5. Parent Set

Print (List_1.issuperset (LIST3)) <<< True

6. Difference Set

List_1-list2

Print (List_1.difference (list2)) <<< {0, 1, 2, 4, 7}

7. Complement set

list_1 ^ List2

Print (List_1.symmetric_difference (list2)) <<< {0, 1, 34, 99, 2, 6, 4, 7}

8. Determine if there is a intersection (not true)

Print (List_1.isdisjoint (LIST4)) <<< True

9, small knowledge point supplement

List_1.add # Add

List_1.update ([10,37,42]) # add multiple items

List_1.remove (8) # Delete specified item, no error will be found

List_1.discard (in) # Delete the specified item, no error is found

List_1.pop () # Random Delete

file Operations

f = open ("file.txt", "R", encoding= "Utf-8")

F.read ()----> disposable Read all

F.readline ()-----> read one line at a time

F.readlines ()----> Read all the lines of the file, generate a list, one element per behavior (only suitable for reading small files)

F.tell ()----> pointer to the current file, counted by the number of characters

F.seek (0)-----> pointer back to the start position

f.encoding-----> file character encoding

F.seekable-----> Determine if pointer position can be moved (true/false)

F.flush ()-------> Writing to a file is not necessarily written to the hard disk in a timely manner, may also be in the cache,flush can be used for flushing to write to the hard disk (the cache writes to a certain value to write to the hard disk)

f.truncate------> truncate from the beginning, no matter where the pointer is

##### Example ####### ( only one row is saved in memory, can read large files, and a row is overwritten )

For line in F:

Print (line)

########## Printing progress bar ##########

Import Sys,time

For I in range (50):

Sys.stdout.write ("#")

Sys.stdout.flush ()

Time.sleep (0.1)

R Read only ; r+ can be read and written ; neither is created

W New Write only , cannot read;

w+ New Read and write, both will clear the contents of the file 0

w+ and r+ difference:r+: Readable writable , if the file does not exist, error;w+: Readable and writable , if the file does not exist, create

########### r+ with the A + difference  ############

FD = open ("1.txt", ' w+ ')

Fd.write (' 123 ')

FD = open ("1.txt", ' r+ ')

Fd.write (' 456 ')

FD = open ("1.txt", ' A + ')

Fd.write (' 789 ')

results:456789

description r+ the overwrite write.

Open the file in a a,a+ way, attach it

(a: Additional write mode open, unreadable;a+: Additional read and write mode open)

Open the file with the ' U ' sign , all the line separators through the Python Input Method ( example # such as read* ()), the return will be replaced with a newline character \ n. (' RU ' mode also supports ' RB ' Options ) .

R and U require that the file must exist

non-readable open mode:w and a

If no new file is created open:a,A +,w,w+

function number of first knowledge

1. Function advantages

(1) Reduce duplication of code

(2) Code uniformity

(3) Scalability

2, non-fixed parameters

def test1 (x, *args):

Print (x)

Print (args)

def test2 (X,**kwargs)

Print (x)

Print (Kwargs)

*args----> receive location parameters become tuples (including dictionaries, tuples)

**kwargs-----> receive keyword parameters into a dictionary

Note: keyword parameter cannot be written in front of position parameter

3. formal parameters and arguments

4. Local variables and global variables

Local variables take effect only in functions and do not change the values of global variables

School = "Oldboy"

def change_name (name):

Global School # defines the globals variable can be changed school

School = "Mage"

Print ("Before Change", Name,school)

Name = "Alex Li"

Print ("After Change", name)

Change_name (' Alex ')

Print (school)

>>> before change Alex Mage

>>> after change Alex Li

>>> Mage

strings and integers cannot be changed to global variables in functions, lists, dictionaries, and collections can be changed in functions.

Note: Global variables should not be changed in the function

5. Recursive characteristics

(1) must have a definite end condition

(2) Each time you enter a deep layer of recursion, the problem scale should be reduced by the last recursion

(3) Recursive efficiency is not high, too many recursive levels will lead to stack overflow

6. function and function programming

Object-oriented ---> classes ----> class

process-oriented ----> process ----> Def

functional Programming ----> functions ----> Def

Functional programming: As long as the input is deterministic, the output is also deterministic

Python Automation Development 03

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.