python基础:7、函数

定义函数

#定义函数
def greet_user():
    """显示简单的问候语"""
    print("Hello!")
    
greet_user()
#向函数传递信息
def greet_user(username):
    """显示简单的问候语"""
    print("Hello,"+username.title()+"!")
    
greet_user("jesse")
#实参和形参
#username是形参 jesse是实参
#414
def display_message(neirong):
    """显示本章所学"""
    print("zhishi:"+neirong.title())
display_message("jsj")

传递实参

#传递实参
#位置实参:基于实参的顺序关联形参
def describe_pet(animal_type,pet_name):
    """显示宠物的信息"""
    print("\nI have a "+animal_type+".")
    print("My "+animal_type+"’s name is "+pet_name.title()+".")
describe_pet("hamster","harry")
#1 调用函数多次
describe_pet("dog","willie")
describe_pet("cat","hx")
#2 位置实参的顺序很重要
#关键字实参:在实参中将名称和值关联起来了
def describe_pet(animal_type,pet_name):
    """显示宠物的信息"""
    print("\nI have a "+animal_type+".")
    print("My "+animal_type+"’s name is "+pet_name.title()+".")
describe_pet(animal_type="hamster",pet_name="harry")
describe_pet(pet_name="yyy",animal_type="cat")
#默认值
def describe_pet(pet_name,animal_type="dog"):
    """显示宠物的信息"""
    print("\nI have a "+animal_type+".")
    print("My "+animal_type+"’s name is "+pet_name.title()+".")
describe_pet(pet_name="harry")
describe_pet("harry")
#等效的函数调用
def describe_pet(pet_name,animal_type="dog"):
     print("\nI have a "+animal_type+".")
     print("My "+animal_type+"’s name is "+pet_name.title()+".")
describe_pet(pet_name="harry")
describe_pet("harry")
describe_pet("harry","hamster")

返回值

#返回值
def get_formatted_name(first_name,last_name):
    """返回整洁的姓名"""
    full_name=first_name+" "+last_name
    return full_name.title()
musician=get_formatted_name("jimi","hendrix")
print(musician)
#让实参变成可选的
def get_formatted_name(first_name,last_name,middle_name=""):
    """返回整洁的姓名"""
    if middle_name:
        full_name=first_name+" "+middle_name+" "+last_name
    else:
        full_name=first_name+" "+last_name
    return full_name.title()
musician=get_formatted_name("jimi","hendrix")
print(musician)
musician=get_formatted_name("john","hooker","lee")
print(musician)
#返回字典
def build_person(first_name,last_name):
    """返回一个字典,其中包含有关一个人的信息"""
    person={"first":first_name,"last":last_name}
    return person
musician=build_person("Chen","Rong")
print(musician)
def build_person(first_name,last_name,age=""):
    """返回一个字典,其中包含有关一个人的信息"""
    person={"first":first_name,"last":last_name}
    if age:
        person["age"]=age
    return person
musician=build_person("Chen","Rong",age=27)
print(musician)
#结合使用函数和while循环
def get_formatted_name(first_name,last_name):
    """返回整洁的姓名"""
    full_name=first_name+" "+last_name
    return full_name.title()
#这是一个无限循环
while True:
    print("\nPlease 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
    formatted_name=get_formatted_name(f_name,l_name)
    print("\nHello,"+formatted_name+"!")

传递列表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值