python自动化2021/03/26 函数参数2 函数的返回值

#位置传参 ***

# def add(x,y):
# print("x",x)
# print("y", y)
# print(x+y)
#
#
# add(3,5)

# def add(x,y,z): #add() missing 1 required positional argument: 'z'
# print("x",x)
# print("y", y)
# print(x+y)
#
#
# add(3,5,4)

# def print_stu(name,age,height,weight,id):
# print("name",name)
# print("age", age)
# print("height", height)
# print("weight", weight)
# print("id", id)
#
# print_stu("yuan",23,180,140,23)



#关键字传参**
# def print_stu(name,age,height,weight,id):
# print("name",name)
# print("age", age)
# print("height", height)
# print("weight", weight)
# print("id", id)
#
# print_stu("yuan",height=180,id=23,weight=140,age=23) #混合传参时,关键字一定要在所有的位置参数之后.

#默认参数*

# def print_stu(n,age,genter="male"): #一个班大部分人都是男的,可以提前传参中定义genter,实参不是male的时候再单独传就行了.
# print("name",n)
# print("age", age)
# print("genter", genter)
#
# print_stu("阳春岭",29)
#
# print_stu("王俊",34)
#
# print_stu("雪薇",23,"female")


#不定长/可变参数 *args(未知参数) **kwargs(关键字参数)

# def add(x,y):
# print("x",x)
# print("y", y)
# print(x+y)
#
# add(3,5,4)


# def add(*args):
# # print(args)
# # print(type(args))
# ret = 0
# for i in args:
# ret += i
# print(ret)
#
#
# add(3,5,4) #(3, 5, 4) 元组

# def print_stu(**kwargs):
# # print(kwargs)
# # print(type(kwargs))
# for key,val in kwargs.items():
# print(key,val)
#
#
# print_stu(name = "alvin",age = 23,gender = "male")



# def foo(name,*args,**kwargs):
# print("name",name)
# print("args",args)
# # print(type(args)) #<class 'tuple'>
# print("kwargs", kwargs)
# # print(type(kwargs)) #<class 'dict'>
#
# foo("yuan",23,"male",height=180,weight=140)


#值传递和引用传递

# def foo(x):
# x.append(4)
# print(x)
# a = [1,2,3]
# foo(a)
# print(a)


# def foo(x):
# x = 100
#
# x = 10
# foo(x)
# print(x)

def foo(x): # x-----> |[0]| ---> [1] #可变类型和不可变类型的区别foo下的作用域和全局的作用域
x.append(4) # / |[1]| ---> [2]
print("foo的x",x) # / |[2]| ---> [3]
# / |[3]| ---> [4]
x = [1,2,3] # /
foo(x) # x
print("全局的x",x)


#函数的返回值
posted @ 2021-03-27 14:05  lpaxq  阅读(67)  评论(0编辑  收藏  举报