Python基础学习——函数后面的练习题

函数系统学习

Python编程从入门到实践的习题“动手试一试”
8.1/8.2动手试一试
定义函数及形参、实参应用

def display_message():
    print('I study how to use fuction.')
display_message()

def favorite_book(title):
    print('One of my favorite book is '+ title +' in Wonderland.')
favorite_book('Alice')

8.3/8.4/8.5
传递实参:位置实参、关键字实参、默认值

def make_shirt(size,model_of_written_characters):
    print('It is a '+size+' T-shirt'+ ' says '+model_of_written_characters)
make_shirt('L','Superme')

def make_shirt(size='L',model_of_written_characters='I love Python'):
    print('It is a '+size+' T-shirt'+ ' says '+model_of_written_characters)
make_shirt()
make_shirt('M')
make_shirt(model_of_written_characters='I love my family')
def describe_city(city='Bejing',country='China'):
    print(city+' is in '+country+'.')
describe_city()
describe_city('shanghai')
describe_city('London','England')

动手试一试(返回值)8.6/8.7/8.8
返回值、实参变成可选、返回字典

def city_country(city,country):
    c_country=city+","+country
    return c_country.title()
City_Country_1=city_country('bejing','china')
City_Country_2=city_country('london','england')
City_Country_3=city_country('santiage','chile')
print(City_Country_1)
print(City_Country_2)
print(City_Country_3)
def make_album(singer,album,number='10'):
    s_album={'singer_name':singer,'album_name':album}
    if number:
        s_album['number']=number
    return s_album
Album_1=make_album('zhoujielun','the secret',99)
Album_2=make_album('liudehua','the rain')
Album_3=make_album('wuyuetian','the sky',56)
print(Album_1)
print(Album_2)
print(Album_3)
def make_album(singer,album,number=''):
    s_album={'singer_name':singer,'album_name':album}
    if number:
        s_album['number']=number
    return s_album

while True:
    print("\nPlease input the singer's name and the album.")
    print("(enter 'q' at anytime to quit)")
    Singer=input('Singer_namer:')
    if Singer=='q':
        break
    
    Album=input('Album:')
    if Album=='q':
        break
    
    Album_1=make_album(Singer,Album)
    print(Album_1)

8.9
传递列表

def show_magicians(magicians):
    for magician in magicians:
        print(magician.title())
magicians=['josn','feibi','monika','nosi']
show_magicians(magicians)

在函数中修改列表
/8.10/8.11

**

magicians=['josn','feibi','monika','nosi']
great_magicians=[]
def show_magicians(magicians):
    for magician in magicians:     
        print(magician.title())
     

show_magicians(magicians)

def make_great(magicians,great_magicians):
    while magicians:
        great_magician='the Great '+magicians.pop()
        great_magicians.append(great_magician)
    print(great_magicians)
  
make_great(magicians[:],great_magicians)
print(magicians)

8.12
传递任意数量的实参

def make_sanwich(*toppings):
    print("\nMaking a sanwich with the following toppings.")
    for topping in toppings:
        print('-'+topping)

make_sanwich('mushroom')
make_sanwich('mushroom','sager')
make_sanwich('mushroom','sager','pepper')

8.13
使用任意数量的关键字实参

def bulid_profile(first,last,**user_info):
    profile={}
    profile['first_name']=first
    profile['last_name']=last
    for key,value in user_info.items():
        profile[key]=value
    return profile
user_profile=bulid_profile('harry','porter',location='guangzhou',favorite='travel')
print(user_profile)

8.14

def car_info(brank,model,**other_info):
    profile={}
    profile['Brank']=brank
    profile['Model']=model
    for key,value in other_info.items():
        profile[key]=value
    return profile
user_car=car_info('changcheng','hafoH6',color='black',three_package=True)
print(user_car)   

8.15/8.16
将函数存储在模块中,并导入使用

def print_models(unprinted_designs,completed_models):
    while unprinted_designs:
        current_design = unprinted_designs.pop()
        print("Printing model: "+current_design)
        completed_models.append(current_design)

```python
unprinted_designs=['iphone case','robot pendant','dodecahedron']
completed_models=[]
import printing_functions 

printing_functions.print_models(unprinted_designs,completed_models)


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值