Python编程:从入门到实践读书笔记-8 函数
来源:互联网 发布:字画网络销售 编辑:程序博客网 时间:2024/05/22 17:29
项目上忙活了两个多月,没有学习,当然也不是没时间学习,就是不想学习,总是控制不住自己想玩。。
如今又来了新项目,现在需求调研阶段,继续学习,希望能坚持下去,每天记录学习过程。
函数:带名字的代码块,用于完成具体的工作,当需要执行函数定义的特定任务时,可调用该函数。
8.1 定义函数
eg:
def greet_user(): #定义函数
# 关键字def告诉Python定义一个函数,并指出函数名,括号指出函数为完成任务,需要什么信息,括号内可为空,
最后,定义函数以冒号:结尾
"""显示简单的问候语""" #文档字符串
print("Hello!")
greet_user()
op:
Hello!
注意:
1. 定义函数必须以冒号:结尾;
2. 紧跟在函数后面的所有缩进行构成函数体,被称为文档字符串的注释描述函数是做什么的,必须使用三引号""";
8.1.1 向函数传递信息
情景1:使用函数向用户chandler问好
eg:
def greet_user(username):
print("Hello, " + username.title() + "!")
greet_user('chandler')
op:
Hello, Chandler!
注意:向函数greet_user传递用户姓名信息时,使用单引号'';
8.1.2 实参和形参
以8.1.1为例:
形参:函数完成其工作所需的一项信息。即变量username;
实参:是调用函数时传递给函数的信息。即chandler;
8.2 传递实参
传递实参方式:
位置实参—要求实参和形参顺序相同;
关键字实参—实参由变量名和值组成;
列表、字典
8.2.1 位置实参
情景1: 使用一个显示宠物信息的函数,指出宠物种类及名字。
eg:
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','harry')
op:
I have a dog.
My dog 's name is Harry.
注意:实参的顺序和形参顺序必须一一对应。
1. 调用函数多次
情景2:在情景1的基础上再描述一个宠物。
eg:
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','harry')
describe_pet('cat','willie')
op:
I have a dog.
My dog 's name is Harry.
I have a cat.
My cat 's name is Willie.
2. 位置实参顺序很重要
如位置实参顺序错误,如下:
eg:
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('harry ','dog')
op:
I have a harry .
My harry 's name is Dog.
注意:位置实参顺序错误之后,程序不会报错,但实际结果与期望结果相差甚远。
8.2.2 关键字实参
关键字实参—传递给函数名称-值对。
eg:
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='dog',pet_name='harry')
# 关键字实参必须形参对应的实参,因此顺序无关紧要
op:
I have a dog.
My dog 's name is Harry.
注意:使用关键字实参时,务必准确地指定函数定义中的形参名。
8.2.3 默认值
编写函数时,可给每个形参指定默认值,即调用函数时,给形参提供实参则使用指定值,否则使用形参默认值。
情景1:某函数描述对象为,但宠物狗名字都不一样。
eg:
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')
op:
I have a dog.
My dog's name is Willie.
注意:1. 当形参指定了默认值时,必须将未指定默认值的形参放在首位,否则报错,即
def describe_pet(animal_type='dog',pet_name):
SyntaxError: non-default argument follows default argument。
2. 原因:由于对形参animal_type指定了dog实参,因此函数调用只包含一个实参,
即宠物的名字,并视其为位置实参,需要将pet_name放在形参列表开头。
3. 使用默认值时,在形参列表中必须先列出没有默认值的形参,再列出有默认值的实参。
这让Python依然能够正确地解读位置实参。
8.2.4 等效的函数调用
鉴于可混合使用位置实参、关键字实参和默认值,通常有多种等效的函数调用方式。
eg:
def describe_pet(pet_name, animal_type='dog'):
形参 animal_type在给定实参值'dog'后,另一形参pet_name给定实参可使用位置方式、关键字方式等,
无论哪种方式,只要自己容易理解即可。
如
# 一条名为Willie的小狗
describe_pet('willie') #默认值形式
describe_pet(pet_name='willie') #关键字形式
8.2.5 避免实参错误
实参不匹配错误:提供的实参多于或少于函数完成其工作所需的信息时,将出现实参不匹配错误。
eg:
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()
缺少必要信息,报错!
op:
Traceback (most recent call last):
File "pets.py", line 6, in <module>
describe_pet()
TypeError: describe_pet() missing 2 required positional arguments: 'animal_
type' and 'pet_name' #提示函数调用缺少两个实参,并指出相应形参名称
练习:
8.3
def make_shirt(size,words):
print("I want the T-shirt size is " + size.title() + ", "
+ "the words on it is " + "'" + words.title() + "'.")
make_shirt('x','friends')
make_shirt(size = 'm',words = 'friends')
8.4
def make_shirt(size='L',words='I love Python'):
print("I want the T-shirt size is " + size.title() + ", "
+ "the words on it is " + "'" + words.title() + "'.")
make_shirt()
make_shirt('M')
make_shirt(words='I love Python very much!')
8.5
def describe_city(name,country):
print(name.title() + " is in " + country.title() + ".")
describe_city('wuhan','china')
def describe_city(name,country='china'):
print(name.title() + " is in " + country.title() + ".")
describe_city('wuhan')
describe_city('yichang')
describe_city("xi'an")
阅读全文
0 0
- Python编程:从入门到实践读书笔记-8 函数
- Python编程:从入门到实践读书笔记-8 函数
- Python编程:从入门到实践读书笔记-8 函数
- Python编程:从入门到实践读书笔记-8 函数
- Python编程:从入门到实践读书笔记-4 操作列表
- Python编程:从入门到实践读书笔记-5 if语句
- 【Python编程:从入门到实践】第八章:函数
- 20170118Python编程:从入门到实践
- Python编程:从入门到实践1
- Python编程:从入门到实践2
- Python编程:从入门到实践
- Python编程:从入门到实践读书笔记-4 操作列表 & 5 if语句
- Python编程:从入门到实践读书笔记-5 if语句 & 6 字典
- Python编程:从入门到实践读书笔记-5 if语句 & 6 字典
- Python编程:从入门到实践读书笔记-7 用户输入和while循环
- Python编程:从入门到实践(课后习题8)
- python学习——《python编程从入门到实践》
- python编程 从入门到实践 第八章 函数 及课后题
- java中JVM的原理
- 深入浅出ES6---第三篇
- HDU
- Tinkoff Challenge
- Linux下的tar压缩解压缩命令详解
- Python编程:从入门到实践读书笔记-8 函数
- Win7 SSH Secure connect Ubuntu: Server responded "Algorithm negotiation failed"
- 设计模式之面向对象
- (poj 1986 Distance Queries)<LCA—tarjan>
- RMI
- spring的部分注解
- 关于在SSH环境下struts校验遇到的问题
- Django学习之模型的操作详解
- SpringMVC 拦截器不拦截静态资源的三种处理方式方法