Python Basic Learning (iv)

Source: Internet
Author: User
Tags define local variable scope

Python Collection:

set Gu Ming, is a collection, the elements of the collection are unique, unordered. A {} contains elements that make up a set, which can be a variety of data types (but not a list, a collection, a dictionary, a tuple)

It can go back to the repeating elements in the list.

List1 = [1,2,3,23,1,4,2]list1 = set (List1)

Relationship Tests for collections:

A = {1,2,3,4,5}b = {1,3,5,7,9}a.symmetric_difference (b) #对称差集a. Difference (b) #差集a. Intersection (b) #交集a. Union (b) #并集


Python file operations:


Ptython a full list of open files in different modes:

Mode Description
R Open the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
Rb Opens a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+ Open a file for read-write. The file pointer will be placed at the beginning of the file.
rb+ Opens a file in binary format for read-write. The file pointer will be placed at the beginning of the file.
W Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
Wb Open a file in binary format only for writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+ Open a file for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+ Opens a file in binary format for read-write. Overwrite the file if it already exists. If the file does not exist, create a new file.
A Opens a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
Ab Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file to write to.
A + Open a file for read-write. If the file already exists, the file pointer will be placed at the end of the file. The file opens with an append mode. If the file does not exist, create a new file to read and write.
ab+ Opens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file to read and write.


File Object Properties

After a file is opened, you have a files object that you can get various information about the file.

The following is a list of all properties related to the file object:

Properties Description
File.closed Returns true if the file has been closed, otherwise false is returned.
File.mode Returns the access mode of the file being opened.
File.name Returns the name of the file.
File.softspace If the print output must be followed by a space character, false is returned. Otherwise, returns True.


Python Character Set encoding:

    1. The default encoding in PY2 is ASCII

    2. python3.x source files use utf-8 encoding by default, so you can parse Chinese normally without specifying UTF-8 encoding.



Transcoding:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/720333/201608/720333-20160805105601403-2095217232. PNG "alt=" 720333-20160805105601403-2095217232.png "/>


Python functions

functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.

Functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.


Define a function

You can define a function that you want to function, and here are the simple rules:


    • The function code block begins with a def keyword followed by the function identifier name and parentheses ().

    • Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.

    • The first line of the function statement can optionally use the document string-for storing the function description.

    • The function contents begin with a colon and are indented.

    • return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.


Function parameters:

1. Default parameters, as follows: Address is a default parameter, the default is "CN" without passing in the value;

def test (name,age,address= "CN"): Print ("Name:", name) print ("Age:", age) Print ("Address", address) test ("Lilei", 18)

2. Positional parameters, the parameters of the incoming one by one corresponding, can not be more or less;

Test ("Lilei", "US")

3. Keyword parameters can be unordered, can be common with positional parameters, but positional parameters must precede keyword parameters.

Test (age=18,name= "Lilei") test ("Lilei", age=18)

4. Non-fixed parameters, if your function in the definition is not sure how many parameters passed, you can use non-fixed parameters: *args, **kwargs

def test1 (*args): print (args) test1 ("Lilei", "cn") Test1 (*["Lilei", "cn"]) def test2 (**kwargs): Print (Kwargs) Test2 ("Jack", +, "CN", "Python", sex= "Male", province= "Shandong")


Local variables and global variables:

A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of the program is called a global variable.

The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable.

When a global variable has the same name as a local variable:

Local variables work in sub-programs that define local variables, and global variables work elsewhere.

The global variables are defined in the subroutine with the keyword global.


Recursion:

Inside a function, you can call other functions. If a function calls itself internally, the function is a recursive function.

Def calc (n): print (n) if int (N/2) ==0:return n return Calc (int (N/2)) Calc (10)

Recursive properties:

1. There must be a clear end condition

2. Each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion

3. Recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow .


This article is from the "Boundless" blog, please make sure to keep this source http://tofgetu.blog.51cto.com/12856240/1923847

Python Basic Learning (iv)

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.