python 学习day 6
2019-09-09 19:14 叫我永康就好啦 阅读(144) 评论(0) 收藏 举报1、代码学习部分
def display_message(context):
"""本章学习的内容"""
print("I learned "+context+" in this chapter.")
display_message("for")
def favorite_book(title):
"""最喜欢的书的名字"""
print("One of my favorite books is "+title+".")
favorite_book("Robinson crusoe")
#
def make_shirt(size,symbol):
print("The size of T-shirt is "+size+" and the word in it is "+symbol+".")
make_shirt("31","hello world")
#默认字样
def make_shirt(size,symbol="I love python"):
print("The size of T-shirt is "+size+" and the word in it is "+symbol+".")
make_shirt("big-sized")
def make_shirt(size,symbol="I love python"):
print("The size of T-shirt is "+size+" and the word in it is "+symbol+".")
make_shirt("middle-sized")
def describe_city(name,nation="China"):
print(name+" belongs to China.")
describe_city("shenyang")
describe_city("nanjing")
describe_city("london")
#城市名
def city_country(city,country):
print(city+", "+country)
city_country("shenyang","China")
#专辑
def make_album(name,album):
info={"singer":name,'albums':album}
print(info)
make_album("xuezhiqian","du")
make_album("chenxixun","hongmeigui")
make_album("maobuyi","juxing")
#可选形参:
def make_album(name,album,number=""): #将可选形参放置到最后,并设置成形参=""
if number:
info={"singer":name,'albums':album ,'numbers':number}
print(info)
else:
info = {"singer": name, 'albums': album}
print(info)
make_album("xuezhiqian","du","1")
make_album("chenxixun","us","1")
make_album("maobuyi","feel myself a big star")
#用户的专辑/while
def make_album(name,album,number=""): #将可选形参放置到最后,并设置成形参=""
if number:
info={"singer":name,'albums':album ,'numbers':number}
return info
else:
info = {"singer": name, 'albums': album}
return info
while True:
print("\nPlease input singer name and his album:")
print("(enter 'q' at any time to break out.)")
singer=input("singer name:")
if singer== 'q':
break
album=input("album name:")
if album== 'q':
break
album_maked=make_album(singer,album)
print(album_maked)
#魔术师 向函数传递列表
def show_magicians(names):
for name in names:
print(name)
magic_names=["yuqian","david kebofeier"]
show_magicians(magic_names)
#了不起的魔术师 向函数传递列表
def make_great(names):
for name in names:
name="The great "+name
print(name)
magic_names=["yuqian","david kebofeier"]
make_great(magic_names)
#不变的魔术师 禁止函数修改列表
def make_great(names):
for name in names:
print("The Great "+name)
magic_names=["yuqian","david kebofeier"]
make_great(magic_names[:])
print(magic_names)
def sandwich(*toppings):
print("The customer order these:")
for topping in toppings:
print("\n"+topping)
sandwich("mushrooms","green peppers","extra cheese")
#users_profile.py
def build_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=build_profile('albert','einstein',location='princeton',field='physics')
print(user_profile)
user_profile=build_profile('zhang','yong kang',location='shen yang',field='electrical engineering ',food_like='doule cooked pork slices')
print(user_profile)
#汽车
def build_car(manufacturer,xinghao,**car_info):
car={}
car['zhizaoshang']=manufacturer
car['xinghao']=xinghao
for key,value in car_info.items():
car[key]=value
return car
car_bought=build_car('aodi','31',color='red',parts='tire')
print(car_bought)
#模块调用
#import module name
import car
print(car.build_car('aodi','31',color='red',parts='tire'))
#from module_name import function_name
from car import build_car
print(build_car('aodi','31',color='red',parts='tire'))
#from module_name import function_name as fn
from car import build_car as bc
print(bc('aodi','31',color='red',parts='tire'))
#import module_name as mn
import car as cr
print(cr.build_car('aodi','31',color='red',parts='tire'))
#from module_name import *
from car import *
print(build_car('aodi','31',color='red',parts='tire'))
2、学习疑问与心得
2.1、三引号是什么作用
2.2、形参前加两个*星号,即创建一个空字典
2.3、编写函数时,应给函数指定描述性名称,且只在其中使用小写字母和下划线
2.4、每个函数都包含简要的阐述其功能的注释,该注释应紧跟在函数定义后面,并采用文档字符串格式
2.5、给形参指定默认值时,等号两边不要有空格
浙公网安备 33010602011771号