Python Basics (File manipulation review, functions, function return values, function parameters)

Source: Internet
Author: User
Tags function definition

Review of file operations

Reading: read: One-time reading, ReadLines: One-time read, ReadLine: One line of reading, the disadvantage: do not know at that end

Video, image with RB belongs to bytes type, is read in bytes for loop (best)

Write: Write

Cursor: Is the file pointer seek: Specifies that the cursor is moved to a location tell: Gets the current position of the cursor truncate: intercepting files

Closing files: Close

Modify file: The file cannot be modified

Delete File is: Os.remove ()

Renaming files: Os.rename ()

Function

def: Defines the keyword format for a function: Def variable name ():

Once defined, it can be called wherever it is needed

# function Definition def Mylen ():     """ calculate the length of a S1 """   ' Helloworld'    = 0    for      inch S1:         = length+1    print(length)# function call   Mylen ()
return value of the function

Return is the keyword of the returned value

Return is a keyword, and in pycharm you will see it turn blue. You have to memorize the word without any bad words.

This word translates to "return", so the value that we write after return is called "return value".

The return value has the following conditions: No return value, return value, return multiple values

1, no return value (default returns None)

If you do not write a return, the default is to return a none

#function DefinitiondefMylen ():"""calculate the length of a S1"""S1="Hello World"length=0 forIinchS1:length= Length+1Print(length)#function CallStr_len =Mylen ()#because there is no return value, the Str_len is none at this timePrint('Str_len:%s'%str_len)

Write only return and end the entire function once the return is encountered

def Ret_demo ():     Print (111)     return    Print (222= Ret_demo ()print(ret)

Return None: As in the above two cases, we generally do not write like this.

def Ret_demo ():     Print (111)     return None     Print (222= Ret_demo ()print(ret)

2, returns a value

1) returnand return values should have a space between them, and the value of any data type can be returned.

2) You can receive it as soon as you return.

3) If in a program, there are multiple return read only to the first return

#function DefinitiondefMylen ():"""calculate the length of a S1"""S1="Hello World"length=0 forIinchS1:length= Length+1returnlength#function CallStr_len =Mylen ()Print('Str_len:%s'%str_len)

3, returns multiple values

1) Multiple return values are received with multiple variables, and the number of return values is received in the number of variables

defRet_demo2 ():return1,['a','b'],3,4#returns multiple values, received with a variableRet2 =Ret_demo2 ()Print(Ret2)#returns multiple values, received with multiple variablesA,b,c,d =Ret_demo2 ()Print(a,b,c,d)#receive return values with multiple values: Returns several values, receives them with several variablesA,b,c,d =Ret_demo2 ()Print(a,b,c,d)

2) A meta-ancestor is received with a variable

Parameters of the function

1, no parameters

No content is written in parentheses when defining functions and calling functions

2, there is a parameter

Pass what is what

3, with multiple parameters

By location parameter

At the angle of the argument:

Pass the parameter by location

def Mymax (x, y    ): # at this time x=10,y=20    if Else y     return  = Mymax (10,20)print(MA)

Follow the key word parameters

def Mymax (x, y    ): # at this point x = 20,y = Ten    Print (x,    y) if Else y     return  = Mymax (y = 10,x =)print(MA)

mixed with can, but must line according to the position parameter, in according to the key word to pass the parameter

def Mymax (x, y    ): # at this point x = 10,y =    Print (x,    y) if Else y     return  = Mymax (10,y =)print(MA)

Multiple values cannot be passed to multiple variables

Standing on the angle of the formal parameter:

Positional parameters: Must pass and have several parameters to pass several values

Default parameters: Can not pass the parameters, if not pass the default parameters, if the transmission of the

Only when the function is called

Pass by location: The value of the direct write parameter

by keyword: keyword = value

When defining a function

Positional parameters: Defining parameters directly

Default parameter: keyword argument: parameter name = "Default Value"

Trap: The default parameter is a mutable data type

def defult_param (a,l = []):    l.append (a)    print(l) defult_param ('  Alex') defult_param ('Egon')

Dynamic parameters: Can receive any number of parameters, the parameter preceded by the * number, the customary parameter name of args

def mysum (*args):    = 0    for in  args:        the_sum+ =  I    return= MySum (1,2,3,4)print(the_sum)

**kwargs the value of the keyword parameter to form a dictionary

*args receives the value of the location-pass parameter, forming a meta-ancestor

Args must precede the Kwargs

Order: Positional parameters, *args, default parameters, **kwargs

Another way to pass parameters to a dynamic parameter:

At the angle of the argument, add * to a sequence, which breaks the sequence in order

Standing at the angle of the parameter, adding * to the variable is the combination of all the transmitted values.

Python Basics (File manipulation review, functions, function return values, function parameters)

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.