The way of Python ——— chapter II: Functions

Source: Internet
Author: User

---restore content starts---

First, the function one, the definition function

Def f1 (): #定义函数

Asdf

1.def #关键字, creating functions

2.F1 #函数名

3. ()

4.asdf.. #函数体 (Code executed)

5. Return value

F1 () #执行函数

The definition of a function has the following main points:

    • def: A keyword that represents a function
    • Function name: the names of functions, which are called later by function name
    • function Body: The function of a series of logical calculations, such as: Send mail, etc...
    • Parameters: Providing data for the function body
    • Return value: Once the function has finished executing, it can return data to the caller.
Second, the parameters

1. General parameters: The actual parameters are assigned to formal parameters in strict order

2. Default parameters: Must be placed at the end of the parameter list

3. Specify parameters: give the actual parameter assignment to the prescribed formal parameters

4. Dynamic Parameters:

*: The default will be stored in the parameters, all placed in the tuple, F1 (*[11,22,33,44])

* *: Default to pass in the parameters, all placed in the dictionary F1 (**{"K1": "V1", "K2": "V2"})

5. Universal Parameters: *args,**kargs

6. The value of the parameter is transmitted by reference

Third, global variables

1. All scopes are readable

2. Re-assignment of global variables requires global

3. If the variable is a list, dictionary, you can modify it, but you cannot reassign the value

4. Define global variables to all capitals (unspoken rules)

Two or three-dollar operation (Trinocular operation)

#对if_else的简写

      #如果 1==1 established
#name = "You"
#否则
#name = "SB"
Name = = "You" if 1 = = 1 else "SB"

Third, lambda expression

#对函数的简写, can only be done on one line

    F2 = Lambda a1:a1 +

    Name of #f2 function

#a1 parameters

#默认返回执行之后的结果

iv. operation of the file

Open function: This function is used to process the file

First, open the file

The mode of opening the file is:

R, read-only mode "default"

W, write-only mode "unreadable; not exist" created; empty content;

X, write-only mode "unreadable; not present, create, present error"

A, append mode "readable; not present, create; append content only;"

"+" means you can read and write a file at the same time

r+, read and write "readable, writable"

w+, write "readable, writable"

x+, write "readable, writable"

A +, write "readable, writable"

"B" means to operate in bytes

RB or R+b

WB or W+b

XB or W+b

AB or A+b

Note: When opened in B, the content read is byte type, and the byte type is also required for writing

Second, the operation

Read () #无参数, read all; Parameters: (b: by Byte; no B: by character)
Tell () #获取当前指针位置 (bytes)
Seek () #指针跳转到指定位置 (bytes)
Write () #写数据, B: by Byte; no B: by character
Fileno () #文件描述符
Flush () #强刷到硬盘
ReadLine () #仅读取一行
Truncate () #截断, empty everything behind the pointer
For loop file object for Line in f:
print (line)

 

---restore content ends---

The way of Python ——— chapter II: 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.