Python full stack development from getting started to giving up function basics

Source: Internet
Author: User

1, why to use the function
#1. Avoid code reuse
#2. Improve the readability of your code

2. Definition of function
def function name (parameter 1, parameter 2):
"' Function comment '
Print ("function body")
Return "returned value"

3. Function call
return value = function name (parameter 1, parameter 2)

4, function definition--return value
#关键字: Return
#return的作用:
#1. Ending the execution of a function
#2. Returns the value to return
#返回值的两种情况:
#返回值为None
#返回值不为None
#1. Returns a value
#2. Returning Multiple values

5. Function call--Receive return value
#返回值为None do not receive
#返回值不为None
# 1. Return a value
#用一个变量接收
# 2. Return multiple values
#用一个变量接收, the received result is a tuple
#有多少个返回值就用多少个变量接收

#参数--standing on the point of defining the function
#1. Position parameters,
#2. Default parameters
#3. Dynamic Parameters
#*args
#**kwargs
#顺序 positional parameter *args default parameter **kwargs

#参数--standing on the point of calling the function
#1. Pass the parameter by location
#2. by keyword
#3. Dynamic parameter *tup **dic

1. The first return value can be any data type

1 def func (): 2     a = 1113     b = [1,2,3]4     return b5 6 ret = func ()   

2. function can not return value

1 def func1 (): 2     A = 1113     b = [4]ret = func1 ()print (ret)6  # function can have no return value 7# when no return is written, the default return value of a function is ' None '

3.

1 def Func2 (): 2     A = 1113     b = [5]4     return#  ret = FUNC2 ()  6#  print (ret)7# When you write only one return, the return value of the function is None
None

4.

def func3 ():     = 111    = [+=    ]return  None#  ret = func3 ()# thereturn value of the function is none (almost unused) when print (ret)#return is None

5.

Python full stack development from getting started to giving up function basics

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.