Python Learning Note 3: Collections, file operations, functions

Source: Internet
Author: User

A. Collection

1.

>>> S=set ([1,2,3,4,5,6,6,6,])
>>> s
{1, 2, 3, 4, 5, 6}

A collection can be understood as a dictionary with keys that have no value, and the keys are de-weighed and unordered.

2. Set Operation:

>>> s1={1,2,3,4,5,6,7}
>>> s2={6,7,8,9,10,11}
>>> S1&S2
{6, 7}//intersection
>>> S1|S2
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}//
>>> S1^S2
{1, 2, 3, 4, 5, 8, 9, 10, 11}//difference set

3. Adding and removing elements

>>> S=set ([1,2,3,4,5,5,5,8])
>>> s
{1, 2, 3, 4, 5, 8}
>>> s.add (' Hello ')
>>> s
{1, 2, 3, 4, 5, ' Hello ', 8}
>>> S.remove (5)
>>> s
{1, 2, 3, 4, ' Hello ', 8}

Two. File operation

1.open function Open File mode:

(1) r, read-only mode

(2) W, write-only mode: unreadable, nonexistent, create, delete content

(3) A, append mode: Readable, non-existent then created, only append content exists

+ identity can write a file at the same time

2. Close the file:

(1) Close function

(2) with:

>>> with open ("/root/1.txt", "R") as F:
... f.readlines ()
...
[' This was a txt file line1\n ', ' This is a txt file line2\n ']

3. File iterators

1. How to use:

>>> with open ("/root/1.txt", "R") as F:
... f.readlines ()
...
[' This was a txt file line1\n ', ' This is a txt file line2\n ']
>>> with open ("/root/1.txt", "R") as F:
... for line in F:
.. print (line)
...
This is a TXT file line1

This is a TXT file line2

Benefit: Avoid the ReadLines method to read the file into memory at once

Three. Functions

1. Definition: A function refers to the collection of a set of statements by a name to encapsulate, to execute this function, simply call its function name to

2. Features:

Reduce duplication of code, make programs extensible, and make programs easier to maintain

3. Scope: A variable within a function does not affect the value of a variable defined by a function sibling, and needs to be changed to use the global declaration within a function

4. Position parameters and keyword parameters

A. Keyword parameter cannot precede position parameter

B.*args:

>>> def Fun (*args):
.. print (*args)
...
>>> Fun ("Hello World", +)
Hello World 1 2

Accept arbitrary multi-position parameters

C.**args:

>>> def Fun (X,y,z=1,*args,**kwargs):
... print (x, y, z)
.. print (args)
.. print (Kwargs)
...
>>> Fun (1,2,3,4,5,6,foo= "Hello python!")
1 2 3
(4, 5, 6)
{' foo ': ' Hello python! '}
>>> Fun (+)
1 2 1
()
{}

Python Learning Note 3: Collections, file operations, functions

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.