python-函数模块基础语法复习

# test1
def greet_user():
    print("hello")
greet_user()

# test2
def greet_user(username):#形参
    print(f"hello,{username.title()}")
greet_user("name")#实参

# test3
def display(text):
    print(f"我在本章学的是{text}")
display("函数")

# test4
def favorite_book(book):
    print(f"one of my favorite book is {book}")
favorite_book("alice in wonderland")

# test5传递多个位置实参
def discribe_name(type,name):
    print(f"type is {type}")
    print(f"name is {name}")
discribe_name("dog","pet")

# test6传递多个关键字实参,指定各个实参对应的形参
def discribe_name(type,name):
    print(f"type is {type}")
    print(f"name is {name}")
discribe_name(type="dog",name="pet")

# test7:给每个形参指定默认值,必须先给出没有默认值的形参
def discribe_name(name,type='dog'):
    print(f"type is {type}")
    print(f"name is {name}")
discribe_name(name="pet")

#test8
def make_t(size,text):
    print(f"size is {size},name is {text}")
make_t("m","fish")

# test9
def make_lt(size,text = "I love T"):
    print(f"size is {size},text is {text}")
make_lt("s")
make_lt("m")
make_lt("l")

# test10
def get_formated_name(first_name,last_name):
    full_name = f"{first_name}{last_name}"
    return full_name.title()#首字母大写格式
musican = get_formated_name('jimi','hendrix')
print(musican)

# test11,让参数变成可选的,处理中间名为空的情况
def get_formated_name(first_name,middle_name,last_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()#首字母大写格式
musican = get_formated_name('jimi','hendrix','end_name')
print(musican)

musican = get_formated_name('jimi','end_name')
print(musican)

# test12 返回字典
def build_person(first_name,last_name):
    person  = {'first':first_name,'last':last_name}
    return person
musican1 = build_person('yu','fish')
print(musican1)

test13 结合函数使用while循环,设置截至输入的条件
def get_formated_name(first_name,last_name):
    full_name = f"{first_name}{last_name}"
    return full_name.title()#首字母大写格式
while True:
    print("\nplace tell me you 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
    formated_name = get_formated_name(f_name,l_name)
    print(f"hello,{formated_name}")

# test14
def city_country(country,city):
    city_name = f"{country}{city}"
    return city_name
name1 = city_country("cn","kunming")
print(name1)
name2 = city_country("UN","CO")
print(name2)

# test15,数量是可选的,因此把设置成空字符串,放在最末尾
def make_album(name1,name2,num = ''):
    if num :#判断是否有值是这么写的
        album = f"song name is {name1},album name is {name2},mumber is {num}"
    else:
        album = f"song name is {name1},album name is {name2}"
    return album
album1 = make_album(name1='周杰伦',name2 = '烟花易冷')
print(album1)

# test16  注意字符串一定要用引号
def make_album(name1,name2):
    album = f"song name is {name1},album name is {name2}"
    return album
while True:
    print("input q at any time to quit")
    song_name = input("请输歌手名:")
    if song_name == 'q':
        break
    album_name = input("请输入歌曲名:")
    if album_name== 'q':
        break
    name = make_album(song_name,album_name)
    print({name})

# test17 传递列表
def greet_user(names):
    for user in names:
        print(f"hello,{user}")
names = ['张三','李四','王五']
greet_user(names)

# test18在函数中修改列表
unprinted_designs = ['phone_case','robot pendant','dodecachedron']
completed_models =[]

while unprinted_designs:
    curent_design = unprinted_designs.pop()
    print(f"printing model:{curent_design}")
    completed_models.append(curent_design)
print("\nthe following modul have been printed:")
for complated_model in completed_models:
    print(complated_model)

# test19传递任意数量的实参,让topping创建一个名为toppings的空元组
# 并将收到的值都封装到这个元组中
def make_pizza(*toppings):
    """
    打印客户点的所有配料
    :param toppings:
    :return:
    """
    for top in toppings:
        print(top)
make_pizza('pep')
make_pizza('mushrooms','green pep','extra chees')

# test20 结合使用位置实参和任意数量的实参
# python先匹配位置实参和关键字实参,再将余下的实参都收集到一个形参中
def make_pizza(size,*topping):
    print(f"making a {size}-inch pizza with the following toppings:")
    for top in topping:
        print(f"{top}")
make_pizza(16,'pop')
make_pizza(20,'pep','pop')

# test21,使用任意数量的关键字形参,*是元组,**是字典
# 输出:{'loc': 'c', 'age': 'd', 'first_name': 'a', 'last_name': 'b'}
def biuld_profile(first,last,**user_info):
    user_info['first_name'] = first
    user_info['last_name'] = last
    return user_info
user_profile = biuld_profile('a','b',loc = 'c',age = 'd')
print(user_profile)

# test22
def make_san(*san):
    for s in san:
        print(s)
make_san('草莓味','香蕉味','榴莲味')
make_san('橘子味')


#test23
# 输出:{'loc': 'dali', 'money': '1000000000', 'name': 'fish', 'age': '15'}
def my_info(name,age,**my_info):
    my_info['name'] = name
    my_info['age'] = age
    return my_info
my = my_info('fish','15',loc = 'dali',money = '1000000000')
print(my)

# test24
# 输出:{'money': '20w', 'price': '100w', 'maker': 'CN', 'size': '20W'}
def car_info(maker,size,**car):
    car['maker']  = maker
    car['size'] = size
    return car
my_car = car_info('CN','20W',money = '20w',price = '100w')
print(my_car)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值