程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Arguments and formal parameters of Python functions

編輯:Python

About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .

Catalog

      • One 、 background
      • Two 、 Arguments and formal arguments
        • 1. Pass value
        • 2. The reference
      • 3、 ... and 、 Reference resources
      • Four 、 summary


One 、 background

In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function arguments and formal parameters of .


Two 、 Arguments and formal arguments

When defining a function , In parentheses is a comma separated list of formal parameters ( It's called formal parameter for short ), Pass arguments to the function when it is called , According to different parameter types , Pass the value or reference of the argument to the formal parameter .

1. Pass value

When the parameter type is fixed data type ( Such as integers 、 Floating point numbers 、 character string 、 Tuples etc. ) when , Modifying the value of a parameter directly inside a function does not affect the actual parameter .

example : Read the following procedure , Analyze the output .

def ChangeInt(a):
a = 10
a = 2
ChangeInt(a)
print('a =',a)

give the result as follows .

2. The reference

But when the parameter type is variable data type ( As listing 、 Dictionaries 、 Collection etc. ) when , Add to a function by subscript or other means 、 When deleting an element or modifying an element value , The modified result can be reflected outside the function , That is, the arguments will be modified accordingly .
example : Read the following procedure , Analyze the output .

def changeme(mylist):
mylist.append([1,2,3,4])
print(" Value in function : ", mylist)
return
mylist = [10,20,30]
changeme(mylist)
print (" Value outside the function : ",mylist)

give the result as follows .


3、 ... and 、 Reference resources

1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial


Four 、 summary

The above is about Python Function arguments and formal parameters , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved