RT,小白学习py,个人理解,如有不对希望指正,不胜感激
参考书籍:《Python编程从入门到实践》 Eric Matthes
ps:最近两天有点摸鱼了,希望本周能够日更
今天学习函数
1.注意函数体内第一行那个注释!!!!!!!!!!!!!!!!!!!!!!!
貌似是专门用在函数体内首行,描述函数的function的
def greet_user():
"""显示简单地问候语"""
#上面是, 文档字符串的注释:描述函数是做什么的。文档字符串用三引号括起来
#PY使用它们来生成有关程序中函数的文档
print("Hello! ")
greet_user()
2.最简单的传参
def greet_user(username):
"""显示简单地问候语"""
#上面是, 文档字符串的注释:描述函数是做什么的。文档字符串用三引号括起来
#PY使用它们来生成有关程序中函数的文档
print("Hello! "+username.title()+" !")
greet_user('jesses')
#实参 jesses 传到了函数体内的形参username中,并存储
3.传递实参.位置
#传递实参
#要求顺序对应,可以用关键字实参;可以用列表,字典作为参??
#1 位置形参 总之就是对应!
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('dog','wxx')
4.传递实参.关键字实参 即 指定关键字对应的实参值,这样就可以不在乎顺序了
#关键字实参
describe_pet(pet_name='zxw',animal_type='dog')
5.!!!!!!!!!!!注意 第一种定义,也就是把有默认值的不放在最后,是不对的????有默认值的要往后排
#默认值,给默认值,如果不给实参就用默认值,相当于重载??????
def describe_pet(animal_type='dog',pet_name):
""""显示宠物的信息"""
print("\nI have a "+animal_type+".")
print("My "+animal_type+"'s name is "+pet_name.title()+".")
describe_pet(pet_name='willie')
#默认值,给默认值,如果不给实参就用默认值,相当于重载??????
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='willie')
6.返回值 不用void int 啥的指定 直接返回
#返回值
def get_formatted_name(first_name,last_name):
"""返回整洁的姓名"""
full_name=first_name+' '+last_name
return full_name.title()
#musician=get_formatted_name('jimi','hendeson')
print(get_formatted_name('jimi','hendeson'))
7.通过指定默认值,然后结合函数体内的判断,来让实参变成可选的
#可选实参
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()
print(get_formatted_name('jimi','case','hendeson'))
8.返回值是一个字典,可以在函数体中添加各种属性来储存信息
#返回字典
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('jimi','Green','27')
print(musician)
9.#结合使用函数和while
#结合使用函数和while
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.
#传递列表
def greet_user(names):
"""对每个人都显示简单地问候语"""
for name in names:
msg="Hello! "+name.title()+"!"
print(msg)
usernames=['hannah','ty','msw']
greet_user(usernames)
11.在函数中修改列表,简化代码,封装
#在函数中修改列表
# #首先创建一个列表,其中包含一些要打印的设计
# unprinted_designs=['iphone case','robot pendant','dodecahedron']
# completed_models=[]
# #模拟打印每个设计,直到没有未打印的设计为止
# #打印每个设计后,都completed
# while unprinted_designs:
# current_design=unprinted_designs.pop()
# print("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("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=['iphone case','robot pendant','dodecahedron']
completed_models=[]
print_models(unprinted_designs,completed_models)
show_completed_models(completed_models)
12.传列表副本!!!!!!! 用切片,可以保证原值不被改变!
####"""重要内容!!!!""""
#禁止函数修改列表
#可以传副本
#"""function_name(list_name[:])"""#这是用切片表示法创建列表的副本
#改成调用:
print_models(unprinted_designs[:],completed_models)
print(unprinted_designs)