记忆关键点:
一、定义函数
def greet_user():
"""显示简单的问候语"""
print("hello!")
greet_user()
def greet_user():
"""显示简单的问候语"""
print("hello!")
greet_user()
def greet_user(username):
"""显示简单的问候语"""
print("hello!",username)
greet_user("bob")
注意实参,形参,函数开头用define,冒号的使用
二、传递实参
1、位置实参
要求实参的位置与形参相同
def show_mypets(name,varition):
print(f"my pet's name is {name},it is a {varition}")
show_mypets("xinxin","my_wife")
实参按对应顺序传递给形参后,在函数体内执行
def show_mypets(name,varition):
print(f"my pet's name is {name},it is a {varition}")
show_mypets("xinxin","my_wife")
show_mypets("dd",'dog')#多次调用函数
2、关键字实参
无需考虑实参顺序,清楚地指明了函数调用中各个值得用途
def show_mypets(name,varition):
print(f"my pet's name is {name},it is a {varition}")
show_mypets("xinxin","my_wife")
show_mypets(varition='dog',name="dd")
3、默认值
实参或者形参有默认值了,那么只需要给没有默认值得形参进行实参传递
def show_mypets(name="ggg",varition):
print(f"my pet's name is {name},it is a {varition}")
show_mypets(varition='dog')#默认为位置实参,发生错误
def show_mypets(name,varition="dog"):
print(f"my pet's name is {name},it is a {varition}")
show_mypets(name="dsds")
def show_mypets(name,varition="dog"):
print(f"my pet's name服务器托管网 is {name},it is a {varition}")
show_mypets(name="dsds",varition="cat")#覆盖形参中得原来的赋值
4、等效的函数调用
就是综合位置实参和关键字实参的应用
def show_mypets(name,varition="dog"):
print(f"my pet's name is {name},it is a {varition}")
show_mypets(name="dsds",varition="cat")#关键字实参
show_mypets("dsds",varition="cat")#位置实参
show_mypets(varition="cat",name="dsds")#关键字实参
show_mypets(name="dsds")#位置实参
show_mypets("dsds")#位置实参
show_mypets("dsds","dog")#位置实参
三、返回值
1、返回简单的值
def full_name(first_name,last_name):
full_name=f"{first_name}{last_name}"
return full_name
message=full_name("lai","xiangtiai")
print(message)
2、让实参变成可选的
def full_name(first_name,last_name,middle_name=""):
#是否有middle——name,有就打印,否则就不打印
if(middle_name):
full_name=f"{first_name} {middle_name} {last_name} "
else:
full_name=f"{first_name} {last_name}"
return full_name.title()
message1=full_name("lai","li")
print(message1)
message2=full_name("lai",'hhh',"li")
print(message2)
3、返回字典
函数可以返回任何类型的值,包括列表字典等较为复杂的数据结构
def my_name(first_name,last_name):#返回一个人的信息的字典
name={"first":first_name,"last":last_name}
return name
myname=my_name("bob","son")
print(myname)
def my_name_age(first_name,last_name,age):#返回一个人的信息的字典
name={"first":first_name,"last":last_name}
if age:
name["age"]=age#追加到后面
return name
myname_age=my_name_age("bob","son",27)
print(myname_age)
4、结合使用函数和while循环
使用用户名和姓给用户无限循环地打招呼
def my_name(first_name,last_name):#返回一个人的信息的字典
full_name=f"{first_name} {last_name}"
return full_name
while True:
print("tell me your name?")
f_name=input("first name:")
l_name=input("last name:")
full_name=my_name(f_name,l_name)
print(f"hello,{full_name}")
用break设置退出条件
def my_name(first_name,last_name):#返回一个人的信息的字典
full_name=f"{first_name} {last_name}"
return full_name
while True:
print("tell me your name?")
print("enter 'q' at any time to quit")
f_name=input("first name:")
if f_name=="q":
break
l_name=input("last name:")
if l_name=='q':
break
full_name=my_name(f_name,l_name)
print(f"hello,{full_name}")
5、传递列表
def greet_users(names):
for name in names:
print(f"hello,{name.tit服务器托管网le()}")
names=["zhang",'bob','david']#给列表中的每个人打招呼
greet_users(names)
- 在函数中修改列表
#首先创建一个列表,其中包含一些要打印的数据
unprinted_designs=['phone case','robot','data']#未打印的设计
completed_models=[]#完成的设计
#模拟打印所有设计
#打印设计后,移到列表completed_models中
while unprinted_designs:
current_design=unprinted_designs.pop()#每次从列表中最后一个设计打印
print(f"printing model:{current_design}")
completed_models.append(current_design)#添加至已完成中
#显示已打印的模型
print("nthe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
等同于
def print_models(unprinted_designs,completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop() # 每次从列表中最后一个设计打印
print(f"printing model:{current_design}")
completed_models.append(current_design) # 添加至已完成中
def show_completed_models(completed_models):
"""显示打印好的额所有模型"""
print("nthe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
#首先创建一个列表,其中包含一些要打印的数据
unprinted_designs=['phone case','robot','data']#未打印的设计
completed_models=[]#完成的设计
print_models(unprinted_designs,comple
有函数的版本程序更容易拓展和维护,将复杂的任务分解成一系列步骤
- 禁止函数修改列表
传递列表的副本,让原列表保留
def print_models(unprinted_designs,completed_models):
while unprinted_designs:
current_design = unprinted_designs.pop() # 每次从列表中最后一个设计打印
print(f"printing model:{current_design}")
completed_models.append(current_design) # 添加至已完成中
def show_completed_models(completed_models):
"""显示打印好的额所有模型"""
print("nthe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
#首先创建一个列表,其中包含一些要打印的数据
unprinted_designs=['phone case','robot','data']#未打印的设计
completed_models=[]#完成的设计
print_models(unprinted_designs[:],completed_models)#unprinted_designs[:]复制列表
show_completed_models(completed_models)
print(unprinted_designs)
6、任意传递参数的实参
def make_pizza(*toppings):
"""打印顾客所点的所有配料"""
print(toppings)
make_pizza("gousi")
make_pizza('sssxs')
make_pizza('ss','sb','sd','sl')
kk=["jo",'dd']
make_pizza(kk)
ll={'ss','xas'}
make_pizza(ll)
def make_pizza(*toppings):
"""打印顾客所点的所有配料"""
for jj in toppings:
print(jj)
make_pizza('ss','sb','sd','sl')
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
新装的php可能没有php.ini 1、执行php –ini 可以看到路径 2、可以在php的源码目录中找到php.ini-developmen服务器托管网t ,改个名字拷贝到上面的目录下 3、修改php.ini 加入extension=swoole.so …