Python path-function return value returns

Source: Internet
Author: User

return keyword

  No return value

Returns a value

Return multiple values

1. No return value

----does not write return, it returns a none by default: The following function does not write return, which is a case where there is no return value.

#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, not write anything else, will return none, since there is no value to return, can not write return, why do you want to write a return? Here we have to talk about the other uses of return, that is, once the return is encountered, the entire function is terminated

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

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

2. Returns a value

Just write what you want to return after 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)
returns a value

3. Return multiple values

--- can return any number of values of any data type, with a value that receives

deffunc1 ():return1,2,3,4defFunc2 ():return1, (+), [3,4],{'name':'Nancy'}L1=func1 () L2=Func2 ()Print(L1)#(1, 2, 3, 4)Print(L2)#(1, (1, 2), [3, 4], {' name ': ' Nancy '})
return multiple values

--- returns any number of values of any data type, received with multiple values

deffunc1 ():return1,2,3,4defFunc2 ():return1, (+), [3,4],{'name':'Nancy'}L1=func1 () a,b,c,d=Func2 ()Print(L1)#(1, 2, 3, 4)Print(a,b,c,d)#1 (1, 2) [3, 4] {' name ': ' Nancy '}
receive with multiple values

----Cause Analysis

>>>#in Python, multiple values separated by commas are considered to be a tuple. (1, 2)>>> 1,2,3,4(1, 2, 3, 4)>>> (1,2,3,4)(1, 2, 3, 4)#Sequence Decompression One>>> a,b,c,d = (1,2,3,4)>>>a1>>>b2>>>C3>>>D4#Sequence Decompression Two>>> a,_,_,d= (1,2,3,4)>>>a1>>>D4>>> a,*_= (1,2,3,4)>>> *_,d= (1,2,3,4)>>>a1>>>D4#also applies to strings, lists, dictionaries, collections>>> A, B = {'name':'Eva',' Age': 18} >>>a'name'>>>b' Age'
View Code

Python path-function return value returns

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.