Python圈Python基础

Python8--函数

2020-08-25  本文已影响0人  伊洛的小屋
1.什么是函数

我们在写程序的时候会多次使用重复的代码,函数就可以简化这种重复。我们在函数里写重复的内容, 然后等需要使用的时候直接调用就可以

def 函数名(参数列表):
    #函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明
    函数体
    #return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None
2. 定义函数

先进入到python的命令行模式

➜  ~ source yiluo/bin/activate
(yiluo) ➜  ~ python3
Python 3.7.5 (default, Nov 29 2019, 14:32:46)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> def fib(n):
...     a, b = 0, 1
...     while a<n:
...         print(a, end=' ')
...         a, b = b, a+b
...     print()
...
>>> fib(2000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

【1】.def 关键字定义函数
【2】.函数体的第一个语句可以(可选的)是字符串文字
【3】.函数的 执行 会引入一个用于函数局部变量的新符号表
【4】.在函数被调用时,实际参数(实参)会被引入被调用函数的本地符号表中
【5】.函数名称的值具有解释器将其识别为用户定义函数的类型。这个值可以分配给另一个名称

>>> fib
<function fib at 0x10726a290>
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89
>>> def fib2(n):
...     result = []
...     a, b = 0, 1
...     while a<n:
...         result.append(a)
...         a, b =b ,a+b
...     return result
...
>>> f100 = fib2(100)
>>> f100
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>>
3.内置函数

内置函数就是 Python解释器中不用引入任何包,一直可以使用的函数义和调用

图片来自官方文档
>>> a = 'hello'
>>> len(a)
5
>>> type(a)
<class 'str'>
>>> b = -1
>>> abs(b)
1

len获取来字符串的个数
type获取了类型是字符串
abs是绝对值函数 -1的绝对值是1

4.变量作用域

局部变量全局变量

(yiluo) ➜  Code touch a.py
(yiluo) ➜  Code vim a.py
#!/usr/bin/env python3
def demo():
    n = 100
    print(n)

n = 1
print("Before demo ", n)
print("Inside demo", end=' ')
demo()
print("After demo", n)

(yiluo) ➜  Code python a.py
Before demo  1
Inside demo 100
After demo 1

以上 n=1 是全局变量, n=100是局部变量

#!/usr/bin/env python3
def test_local():
    name = "Yiluo"
    print(name)

name = 'Tom'

print('Before', name)
print('inside',end = ' ')
test_local()
print('After', name)
➜  code python3 local.py
Before Tom
inside Yiluo
After Tom
5. global关键字
#!/usr/bin/env python3
def demo2():
    global n
    n = 100
    print(n)

n = 1
print("Before demo2", n)
print("Inside demo2", end=' ')
demo2()
print("After demo2", n)
(yiluo) ➜  Code python b.py
Before demo2 1
Inside demo2 100
After demo2 100
>>> name = 'Yiluo'
>>> def test_local():
...     print(name)
...     name = 'Tom'
...
>>> test_local()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test_local
UnboundLocalError: local variable 'name' referenced before assignment
>>> name = 'Tom'
>>> def name_global():
...     global name
...     print(name)
...     name = 'Yiluo'
...
>>> print(name)
Tom
>>> name_global()
Tom
>>> print(name)
Yiluo
6.函数的参数

Python 常用参数有四种:必选参数默认参数可变参数关键字参数,复合使用时各类参数的定义顺序须同上

1. 必选参数
>>> def student(name, age):
...     print("NAME: ", name)
...     print("AGE: ", age)
...
>>> student('yiluo', 18)
NAME:  yiluo
AGE:  18

也可是使用参数名进行传参

>>> student(name='yiluo', age=18)
NAME:  yiluo
AGE:  18
>>> def info(student_id, student_name):
...     print("Student ID: ", student_id)
...     print("Student Name: ", student_name)
...
>>> info(1, 'Yiluo')
Student ID:  1
Student Name:  Yiluo
>>> info('Yiluo', 2)
Student ID:  Yiluo
Student Name:  2
>>> info(student_name='Yiluo', student_id=1)
Student ID:  1
Student Name:  Yiluo
2. 默认参数
>>> def student(name, age=18):
...     print("NAME: ", name)
...     print("AGE: ", age)
...
>>> student('yiluo')
NAME:  yiluo
AGE:  18
>>> student('yiluo', 20)
NAME:  yiluo
AGE:  20

如果不传年龄会自动给出默认年龄值

>>> def info(student_name, student_id, city='深圳'):
...     print("Student ID: ", student_id)
...     print("Student Name: ", student_name)
...     print('City: ', city)
...
>>> info('Yiluo', 1)
Student ID:  1
Student Name:  Yiluo
City:  深圳
>>> info('Yiluo', 1, '上海')
Student ID:  1
Student Name:  Yiluo
City:  上海
3. 可变参数

可变参数的定义格式是在参数名前面加上 * ,参数名可以自定义,通常写成 *args

>>> def student(name, age=19, *score):
...     print("NAME: ", name)
...     print("AGE: ", age)
...     print("SCORE: ", score)
...
>>> student('yiluo',19,88,90,100)
NAME:  yiluo
AGE:  19
SCORE:  (88, 90, 100)

学生的得分分别是 8890100

4. 关键字参数

关键字参数的定义格式是在参数名前面加上 ** ,参数名可以自定义,通常写成 **kw

>>> def student(name, age, **score):
...     print("NAME: ", name)
...     print("AGE: ", age)
...     print("SCORE: ", score)
>>> score = {'数学':'88', '语文':'90','英语':'100'}
>>> student('yiluo', 19, **score)
NAME:  yiluo
AGE:  19
SCORE:  {'数学': '88', '语文': '90', '英语': '100'}
5. 命名关键字参数

前文提到,必选参数默认参数可变参数在赋值时都可以不写参数名,而命名关键字参数恰好相反, 赋值时必须写上参数名

>>> def student(*, name):
...     print("Hello", name)
...
>>> student('yiluo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: student() takes 0 positional arguments but 1 was given
>>> student(name='yiluo')
Hello yiluo
>>> def hi(*, name):
...     print('hi ', name)
...
>>> hi('Yiluo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hi() takes 0 positional arguments but 1 was given
>>> hi(name='Yiluo')
hi  Yiluo
6. 函数中修改参数值
>>> ipaddress = '192.168.1.168'
>>> ports = [11, 12, 13]
>>> def connect(ipaddress, ports):
...     print("IP: ", ipaddress)
...     print('Ports: ', ports)
...     ipaddress = '10.10.10.1'
...     ports.append(8000)
...
>>> print(ipaddress, ports)
192.168.1.168 [11, 12, 13]
>>> connect(ipaddress, ports)
IP:  192.168.1.168
Ports:  [11, 12, 13]
>>> connect(ipaddress, ports)
IP:  192.168.1.168
Ports:  [11, 12, 13, 8000]
>>> connect(ipaddress, ports)
IP:  192.168.1.168
Ports:  [11, 12, 13, 8000, 8000]
>>> print(ipaddress, ports)
192.168.1.168 [11, 12, 13, 8000, 8000, 8000]

欢迎下方【戳一下】【点赞】
Author:伊洛Yiluo
愿你享受每一天,Just Enjoy !

上一篇下一篇

猜你喜欢

热点阅读