Python的第十五天
一、return返回值
作用:1、返回执行结果(若没有return,则返回值是none)
2、程序执行,一遇到return,就代表着函数的结束
练习:
def stu_register(name,age,course,country):
print("-----------学生注册信息----------")
print("name:",name)
print("age:",age)
print("course:",course)
print("country:",country)
if age > 22:
return False
else:
return True 或 return True,name,age,course,country
a = stu_register("alex",20,"python","china")
print(a)
if a:
print("注册成功。。")
else:
print("sorry,,,")
-----------学生注册信息----------
name: alex
age: 20
course: python
country: china
True 或 (True,'alex','20','python','china')
注册成功。。
二、局部变量 and 全局变量
1、变量的查找顺序是 局部变量>全局变量
2、在函数里不能直接修改全局变量
name = "alex"
def change_name():
name = "jack"
print(name)
change_name()
print(name)
jack
alex
name = "alex"
def change_name():
# name = "jack"
print(name)
change_name()
print(name)
alex
alex
三、给函数传list、dict
dict = {"name":"alex","age":"22","hobbie":"大保健"}
list = ["liang","wang","li"]
def data(info,people):
info["hobbie"] = "学习"
people.append("zhang")
data(dict,list)
print(dict,list)
{'name': 'alex', 'age': '22', 'hobbie': '学习'} ['liang', 'wang', 'li', 'zhang']