python 零基础入门教程第 6 章. 函数与代码复用

本贴最后更新于 1127 天前,其中的信息可能已经事过景迁

看下面一段伪代码:

	if cpu使用率 >80%:
		连接邮箱
		发送邮件
		关闭邮箱
	if 内存使用率 >80%:
		连接邮箱
		发送邮件
		关闭邮箱
	if 硬盘使用率 >80%:
		连接邮箱
		发送邮件
		关闭邮箱

思考这段代码有什么问题?

1. 函数的概念

函数是一段具有特定功能的,可重用的语句组,用函数名来表示并通过函数名进行完成功能调用。

函数也可以看作是一段具有名字的子程序,可以在需要的地方调用执行,不需要再每个执行地方重复编写这些语句。每次使用函数可以提供不同的参数作为输入,以实现对不同数据的处理;函数执行后,还可以以反馈相应的处理结果。

函数是一种功能抽象。

2. python中函数的定义

Python定义一个函数使用def关键字,语法形式如下:

def <函数名>(<参数列表>)

	<函数体>

	return <返回值列表>

实例:生日歌

过生日时要为朋友唱生日歌,歌词为:

Happy birthday to you!

Happy birthday to you!

Happy birthday,dear<名字>

Happy birthday to you!

编写程序为MikeLily输出生日歌。最简单的方式是重复使用print()语句

# 最简单的方式
print('Happy birthday to you!')
print('Happy birthday to you!')
print('Happy birthday, dear Mike!')
print('Happy birthday to you!')

print('Happy birthday to you!')
print('Happy birthday to you!')
print('Happy birthday, dear Lily!')
print('Happy birthday to you!')

Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Mike!
Happy birthday to you!
Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Lily!
Happy birthday to you!
以函数的方式

# 定义函数
def happy():
    print('Happy birthday to you!')

def happy_birthday(name):
    happy()
    happy()
    print('Happy birthday, dear {}!'.format(name))
    happy()

# 调用函数
happy_birthday('Mike')
print()
happy_birthday('Lily')

Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Mike!
Happy birthday to you!

Happy birthday to you!
Happy birthday to you!
Happy birthday, dear Lily!
Happy birthday to you!
看起来感和上面的直接print还多了几行代码,但是考虑如果要给100个人唱生日歌的情况。

3. 函数的调用过程

程序调用一个函数需要执行以下四个步骤:

  1. 调用程序在调用处暂停执行
  2. 在调用时将实参赋值给函数的形参
  3. 执行函数体语句
  4. 函数调用结束给出返回值,程序回到调用前的暂停处继续执行

上面的happy_birthday函数的调用过程

image.png

image.png

image.png

4. 函数的参数

定义函数时()里的参数叫形参(形式参数),它只是一个变量名,供函数体中的代码调用。

函数调用时,传入()里的参数叫实参(实际参数),它是实际的数据,会传递给形参,供函数体执行。

4.1 形参

定义函数时,形参根据功能不同,可以定义几种类型。

4.1.1 必须参数

在定义函数时,如果要求调用者必须传递实参给这个形参,它就是必须参数。

直接定义在函数名后的()中的形参就是必须参数。

例如上面的happy_birthday函数中的name

案例:

定义一个函数接收两个数,然后打印它们的和

def add(x,y):
    print(x+y)
add(1)  # 调用时必须传递实参给必须参数,否则报错

TypeError Traceback (most recent call last)

in
----> 1 add(1) # 调用时必须传递实参给必须参数,否则报错

TypeError: add() missing 1 required positional argument: 'y'

4.1.2 默认参数

在定义函数时,某些形参有可能在调用时不用接收实参,这种情况可以定义为默认参数。

在函数名后()中,以参数名=默认值的形式定义的形参就是默认参数。

注意:默认参数必须定义在必须参数的后面

案例:

定义一个函数,它接收两个参数contenttimes,

content是函数要打印的内容

times是函数打印的次数,如果不传递times默认打印1次

# 定义
def my_print(content, times=1):
    for i in range(times):
        print(content)
# 调用
my_print('happy birthday!')
my_print('happy birthday!', 2)

happy birthday!
happy birthday!
happy birthday!
调用函数时传递实参给默认形参会覆盖默认值。

4.1.3 不定参数

在定义函数时,不确定在调用时会传递多少个实参时,可以定义不定参数。

不定参数根据传递实参的不同(详见4.2实参)有分为两种。

位置不定参

在函数名后的()中,在形参前加 *号可以定义位置不定参,通常它会定义为 *args

它用来接收函数调用时,以位置参数传递过来的超过形参数量的多余的实参。

注意:不订参必须定义在默认参数后面

位置不定参数会将所有多余的位置实参创建成元组。

def func(a, *args):
    print(args,type(args))

func(1,2,3,4)

(2, 3, 4) <class 'tuple'>

案例:

定义一个函数,接收2个以上的数,打印它们的和。

def add(x,y,*args):
    x += y
    for i in args:
        x += i
    print(x)
add(1,2,3,4)

10

关键字不定参

在函数名后的()中,在形参前加 **号可以定义关键字不定参,通常它会定义为 **kwargs

它用来接收函数调用时,以关键字参数传递过来的超过形参数量的多余的实参。

注意:不订参必须定义在默认参数后面

关键字不定参数会将所有多余的关键字实参创建成字典。

def func(a, **kwargs):
    print(kwargs,type(kwargs))

func(a=1,b=2,c=3,d=4)

{'b': 2, 'c': 3, 'd': 4} <class 'dict'>

4.2 实参

调用函数是传递实参有两种方式。

4.2.1 位置参数

调用函数时,传递实参时默认会按照形参的位置一一对应,这种实参传递叫做位置参数。

案例

定义一个函数实现打印一个数的n次幂。

def my_power(x, n):
    print(x**n)

my_power(3,2)
my_power(2,3)

9
8

4.2.2 关键字参数

调用函数时,传递实参时以形参名=实参的形式传递参数,叫做关键字参数。

这是不用考虑参数的位置。

注意:关键字参数必须写在位置参数后面。

案例

使用关键字参数调用上面的案例

my_power(x=3,n=2)
my_power(n=2,x=3)
my_power(3,n=2)

9
9
9

my_power(n=2,3)

File "", line 1
my_power(n=2,3)
^
SyntaxError: positional argument follows keyword argument

4.2.3 *,**在传递实参时的用法

*解包

在传递实参时,可以通过*对迭代对象进行解包。

def fun(a,b,*arg):
    print(a,b,arg)
ls = [1,2,3,4,5,6]
fun(*ls) # => fun(1,2,3,4,5,6)

1 2 (3, 4, 5, 6)

**解包

在传递实参时,可以通过**对字典对象进行解包。

def fun(a,b, **kwargs):
    print(a,b,kwargs)
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
fun(**d) # => fun(a=1,b=2,c=3,d=4)

1 2 {'c': 3, 'd': 4}

4.3 返回值

函数还有一个很重要的功能就是返回结果。

python中使用return关键字来退出函数,返回到函数被调用的地方继续往下执行。

return 可以将0个,1个,多个函数运算完的结果返回给函数被调用处的变量。

函数可以没有返回值,也就是说函数中可以没有return语句,这时函数返回None,例如上面我们定义的那些函数。

return会将多个返回值以元组的形式返回。

案例:

定义一个函数接收2个或多个数值,并返回它们的和。

def add(x,y,*args):
    x += y
    for i in args:
        x += i
    return x

res = add(1,2)
print(res)

3
定义一个函数接收被除数x和除数y,返回它们的商和余数。

def my_mod(x,y):
    res1 = None
    res2 = None
    if x < y:
        res1 = x
        res2 = 0
    else:
        i = 0
        while x >= y:
            x = x-y
            i += 1
        res1 = i
        res2 = x

    return res1, res2
res = my_mod(10,3)
print(res)

(3, 1)

5. lambda函数

简单来说,lambda函数用来定义简单的,能够在一行内表示的函数。

语法格式如下:

lambda arg1,arg2,... : expression
案例
f = lambda x,y : x + y
res = f(1,2)
print(res)

3
lambda函数一般不会直接定义,通常是作为参数传递给其他函数作为参数使用。

6. 变量作用域

python中一个变量能够被访问的范围叫做作用域。根据作用域的大小简单的分为全局变量和局部变量。

6.1 全局变量

python是解释型编程语言,解释器在运行一个python程序时会在计算机内存中申请一块内存用来运行这个程序。全局变量在这块内存空间中都可以被访问和修改。

直接定义在函数外的变量就是全局变量,在程序运行的全过程有效。

6.2 局部变量

定义在函数里,的变量就是局部变量,它只在它定义的函数里起作用,一旦函数执行完毕它就不存在了。

image.png

案例

a = 1 # 全局变量

def fun():
    print(a)
fun()

1
上面的案例说明全局变量能够在函数里访问。
image.png

def fun():
    b = 2  # 局部变量
    print(b)

fun()
print(b)

2


NameError Traceback (most recent call last)

in
4
5 fun()
----> 6 print(b)

NameError: name 'b' is not defined
上面的案例说明局部变量在函数外部不能直接访问

a = 1
def fun():

    a += 1  # 尝试直接在函数内部修改全局变量
    print(a)
fun()

UnboundLocalError Traceback (most recent call last)

in
4 a += 1 # 尝试直接在函数内部修改全局变量
5 print(a)
----> 6 fun()

in fun()
2 def fun():
3
----> 4 a += 1 # 尝试直接在函数内部修改全局变量
5 print(a)
6 fun()

UnboundLocalError: local variable 'a' referenced before assignment
上面的案例说明在函数内部不能直接修改全局变量(不可变数据类型)

可变数据类型的修改不受影响,所以可变类型的全局变量在使用的过程中需要格外的注意

a = [1,2]
def func():
    a[0] = 2
    print(a)
func()
print(a)

[2, 2]
[2, 2]

6.2 global关键字

有时候需要在函数内部修改全局变量。

使用globals关键字可以在函数内部修改全局变量

案例:

a = 1 # 全局变量

def fun():
    global a  # 申明 a 是全局变量
    a += 1
fun()
print(a)

2

7.python内建函数

python解释器提供了70多个内置函数。

import builtins
print(dir(builtins))

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', 'IPYTHON', 'build_class', 'debug', 'doc', 'import', 'loader', 'name', 'package', 'spec', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'display', 'divmod', 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset', 'get_ipython', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
小写的就是全局内建函数和类

7.1 基本数据类型类

7.1.1 int

将一个数字或字符串转换成整数。
如果x不是一个数字,那么它必须是和base匹配的整数字符串表达式

int(1.1)

1

int('1')

1

int('0b0101',2)

5

int('0x11',16)

17

int('0o11',8)

9

7.1.2 float

将一个字符串或数字转换为浮点数。

float(123)

123.0

float('1.2')

1.2

7.1.3 complex

complex(10,8)

(10+8j)

7.14 str

str(1)

'1'

str([1,2,3])

'[1, 2, 3]'

str({'name':'xinlan'})

"{'name': 'xinlan'}"

7.15 list

根据传入的可迭代对象创建一个列表,如果没有参数返回空列表

list()

[]

list(range(10))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

list('abcde')

['a', 'b', 'c', 'd', 'e']

7.16 tuple

根据传入的可迭代对象创建一个元组,如果没有参数返回空元组

tuple()

()

tuple('abcd')

('a', 'b', 'c', 'd')

7.17 set

根据传入的可迭代对象创建一个集合对象。

set('abc')

{'a', 'b', 'c'}

set(range(10))

{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

7.18 dict

根据传入的参数创建一个字典对象

# 形如(key, value)的键值对数据
dict([('name','xinlan'),('age',18)])

{'name': 'xinlan', 'age': 18}

# 关键字参数
dict(name='xinlan',age=18)

{'name': 'xinlan', 'age': 18}

7.2常用内建函数

7.2.1 print

打印传入对象的字符串形式

print(1,2,3,sep=',')

1,2,3

print(1,end='')
print(2)

12

7.2.2 input

接收用户的输入数据,以字符串的形式返回。

可以接收字符串参数最为提示信息输出

input('>>>:')

:aaa

'aaa'

7.2.3 type

返回object的类型

type(1)

int

7.2.4 dir

返回传入对象的所有属性和方法名的列表

print(dir(1))

['abs', 'add', 'and', 'bool', 'ceil', 'class', 'delattr', 'dir', 'divmod', 'doc', 'eq', 'float', 'floor', 'floordiv', 'format', 'ge', 'getattribute', 'getnewargs', 'gt', 'hash', 'index', 'init', 'init_subclass', 'int', 'invert', 'le', 'lshift', 'lt', 'mod', 'mul', 'ne', 'neg', 'new', 'or', 'pos', 'pow', 'radd', 'rand', 'rdivmod', 'reduce', 'reduce_ex', 'repr', 'rfloordiv', 'rlshift', 'rmod', 'rmul', 'ror', 'round', 'rpow', 'rrshift', 'rshift', 'rsub', 'rtruediv', 'rxor', 'setattr', 'sizeof', 'str', 'sub', 'subclasshook', 'truediv', 'trunc', 'xor', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

7.2.5 help

返回内建对象的帮助信息

help(help)

Help on _Helper in module _sitebuiltins object:

class _Helper(builtins.object)
| Define the builtin 'help'.
|
| This is a wrapper around pydoc.help that provides a helpful message
| when 'help' is typed at the Python interactive prompt.
|
| Calling help() at the Python prompt starts an interactive help session.
| Calling help(thing) prints help for the python object 'thing'.
|
| Methods defined here:
|
| call(self, *args, **kwds)
| Call self as a function.
|
| repr(self)
| Return repr(self).

Data descriptors defined here:
dict
dictionary for instance variables (if defined)
weakref
list of weak references to the object (if defined)

7.2.6 len

返回容器的元素个数

len([1,2,3])

3

7.2.7 hash

返回对象的hash值

hash('a')

3636161774609400683

7.2.8 iter

根据传入的可迭代对象返回一个迭代器

iter('abcd')

<str_iterator at 0x7f98b7dd2eb0>

7.2.9 id

返回传入对象的身份id(虚拟内存地址的整数形式)

id(1)

4344134656

7.2.10 range

返回一个range object对象,产生整数序列

range(10)

range(0, 10)

range(0,10,2)

range(0, 10, 2)
更多方法详见官方文档

  • Python
    101 引用 • 234 回帖 • 1 关注
1 操作
877649301 在 2021-03-25 22:43:20 更新了该帖
回帖
请输入回帖内容 ...