3rd Python functions and file operations

Source: Internet
Author: User


Function

Defined

1. Basic structure

Def func_name ():

Func_code


2. Three different forms

No parameters: The scenario simply performs some action, such as interacting with the user, printing

Parameter: It is necessary to perform the corresponding logic, such as statistical length, to calculate the maximum minimum value, according to the parameters passed in externally.

Empty function: Design code structure

3. Function only detects syntax in the definition phase, does not execute code




Call

Func_name ()

List parameter call/keyword argument call (keyword argument behind)


Attention:

1. function is defined first and then called

2 The definition of a function is similar to the definition of a variable, without defining the variable beforehand, and directly referring to the variable, the error does not define the function beforehand, and the direct call is equivalent to referencing a nonexistent variable name



Parameters:

#形参: When defining a function, the parameters inside the parentheses become formal parameters

#特点: A parameter is a variable name

# def foo (x, y): #x =1,y=2

# Print (x)

# Print (y)


#实参: When a function is called, the arguments inside the parentheses become arguments

#特点: argument is a variable value

# foo (ON)


#在调用阶段实参 (variable value) to bind formal parameter (variable name)

#调用结束后, Unbind


Return value: Return value does not have a type limit

1. No return: Return none, equivalent to return none

2. Return a value: Returns the value

3. Return Val1,val2,val3: Return (VAL1,VAL2,VAL3)



1. Function name + function body

2. Input (parameter)/output

3. Parameter defaults (default parameters must be behind)


def func_name (param1, param2, Param3, ..., param_m=default_m, ..., param_n=default_n):

Func_code1

Func_code2

...

Func_coden

Return Return_value


Func_name (value1, value2, Value3, ..., value_m-1, [Value_m, ..., Value_n])



LGB Principles

L = local (locally)

G = Global (overall)

B = Builtin (built-in)

In accessing a variable is Python looking for the order is l->g->b






Variable parameters

List/Dictionary

def func_name1 (*args, **kwargs):

Print args, Kwargs


def func_name2 (*args):

Print args


def func_name3 (**kwargs):

Print Kwargs


function is also a variable


A list of the squares of all elements in range (0, 10)

RT = []

For I in range (10):

Rt.append (i * i)


[I * I for I in range (10)]

A list of the squares of all even-numbered elements in range (0, 10)

RT = []

For I in range (10):

If I% 2 = = 0:

Rt.append (i * i)


3rd Python functions and file operations

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.