Python return value

Source: Internet
Author: User

I have a limited level, if there are errors, also hope to point out, thanks!


This chapter describes the return values in detail:

0x 00 Introduction to return Values

0x 01 Specifies the return value and the implied return value

0x return statement position with multiple return statements

0x 03 return value type

0x 04 Function Nesting


0x 00 Introduction to return Values


    • In retrospect, the previous section briefly describes the functions and their various parameters, including a brief description of the difference between print and return, print is simply printed on the console, and return is the return value as the output of the function, you can use the variable to go, Continue to use the return value to do something else.


    • The function needs to be defined and then called, and the result of the return statement in the body of the function is the returned value. If a function does not have a reutrn statement, it actually has an implied return statement, the return value is None, and the type is ' Nonetype '.


    • The function of the return statement:

    • End Function call, return value



0x 01 Specifies the return value and the implied return value


    • The return statement in the body of the function returns the value when it has a specified return value

    • When there is no return statement in the function body, the end of the function will implicitly return a none as the return value, the type is Nonetype, and the return, return none is equivalent to return none.


An example of a return value function is specified:

def showplus (x): print (x) return x + 1 num = Showplus (6) add = num + 2print (add) output result: 69


Implied return None example:

def showplus (x): print (x) num = showplus (6) print (NUM) print (num) output result: 6none<class ' Nonetype ' >



0x return statement position with multiple return statements


    • The Python function returns "return value" using the return statement, which can be assigned to other variables for other purposes.

    • All functions have a return value, and if there is no return statement, return none is called implicitly

    • A function can have multiple return statements, but only one can be executed, and if no REUTRN statement is executed, return none will be called implicitly as the returned value

    • If necessary, you can explicitly call return none explicitly return a none (Null object) as the return value, can be abbreviated to return, but the lazy in Python is a virtue, so generally can not write not write

    • If the function executes the return statement, the function returns immediately, ending the call, and the other statements after the return are not executed.


Example 1:

def showplus (x): print (x) return x + 1 print (x + 1) #该语句会执行么print (Showplus (6)) Output: 67


Example 2:

def showplus (x): Print (x) # 5 return x + 1 # 6 return x + 2 # The statement will not be executed print (Showplus (5)) Output: 56


Example 3:

def guess (x): if x > 3:return "> 3" Else:return "<= 3" print (Guess (2)) Print output: & Gt 3<= 3


Example 4:

# for: else.. Statement (unexpected termination condition)

# indicates that the statement in the else segment will not be executed if the content of the For statement segment loops correctly, and if the For loop is terminated unexpectedly by a break or a return statement during the loop, then the statement in the else segment is not executed.

DEF fn (x): For I in range (x): If i > 4:return i else:print ("{} are not greater than 4". Format (x)) print (FN (3)) print (FN (6)) results returned: 3 is not greater than 4none5



0x 03 return value type


    • Return can only return a single value, regardless of what type is defined, but the value can have more than one element.

    • return [1,3,5] refers to returning a list, which is a list object, 1,3,5 is the element of the list, respectively.

    • Return 1,3,5 appears to return multiple values, implicitly encapsulated by Python as a meta-ancestor returned


Example 1:

DEF fn (): Return 3 #单值时, what type of print (FN ()) print (FN ())) output: 3<class ' int ' > #int integer type



Example 2:

Def showlist (): return [1,3,5] #多元素, what type of print (Type (showlist ())) print (showlist ()) Output: <class ' list ' >[1, 3, 5 ] #列表类型

Example 3:

Def showlist (): Return (2,4,6) #多元素, what type of print (Type (showlist ())) print (showlist ()) Output: <class ' tuple ' > #元祖 Type (2, 4, 6)


Example 4:

Def showlist (): Return 2,4,6 #多值时, not specified type print (Type (showlist ())) print (showlist ()) Output: <class ' tuple ' > #默认封装成元祖 Type

0x 04 Function Nesting


    • The function has a visible scope (both inside and outside the visible relationship), which is the concept of scope.

    • An intrinsic function cannot be called directly from outside, throwing an exception nameerror.


Example 1:

def outer (): Def inner (): #可以理解为内部函数 print ("inner") Print ("outer") outer () Output: outer


At this point, if you call outer (), only print ("outer") is executed, because inner, although within the outer function, is also a function, and if the function is to be called, it must use the ' function name () ' method.


Example 2:

def outer (): Def inner (): Print ("inner") Print ("outer") inner () #外部无法引用内部函数, the intrinsic function only outputs the result in the local scope, throwing an exception: Traceback (most recent): File "c:/python/return_value.py", line 6, in <module> inner () nameerror:name ' inner ' I s not defined


Example 3:

def outer (): Def inner (): Print ("inner") Print ("outer") Inner () outer () output: Outerinner



Summarize:


This section describes the return value of the function, the function of the return value, specifying the difference from when the return value is not specified, the return value type, and the use of the return value when the function is nested.


Python return value

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.