栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python课程笔记 第8章 Python函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python课程笔记 第8章 Python函数

8-1 认识函数
round(a,2)表示保留a的小数点前两位
round(a,3)表示保留a的小数点前三位
round()函数支持四舍五入
a = 1.12386
result = round(a,2)
print(result)
result1 = round(a,3)
print(result1)

用IDLE输入help(函数)就可以得到相关函数的讲解

help(round)
Help on built-in function round in module builtins:

round(...)
round(number[, ndigits]) -> number

Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the

same type as the number. ndigits may be negative.

8-2 函数的定义及运行特点

函数定义

def funcname(parameter_list):
pass

1.参数列表可以没有 2.用return来返回value,没有return则为none 1.实现两个数字的相加 2.打印输入参数

def add(x,y):
result = x + y
return result

不可以定义和内置函数同名的函数

def print1(code):
print(code)
a = add(1,2)
b = print1('python')
print(a,b)

运行结果为:
python(#这个是运行b = print1('python')时候打印出来的)
3 None(#因为print1函数没有return,所有输出结果为None)

8-3 如何让函数返回多个结果

定义R人物的两个技能的伤害值 R skill1 skill2

def damage(skill1,skill2):
damage1 = skill1 3
damage2 = skill2 2 + 10
return damage1,damage2
damages = damage(3,6)
print(type(damages))
print(damages)

skill1_damage,skill2_damage = damage(3,6)
print(skill1_damage,skill2_damage)

8-4 序列解包与链式赋值
a = 1
b = 2
c = 3

a,b,c = 1,2,3

d = 1,2,3
print(type(d))

d类型是元组

a,b,c = d #这个过程就叫做序列解包,不一定要写成a,b,c=[1,2,3]

前后元素个数要相等 当abc都为1时,也可以这么赋值:

a = b = c = 1

8-5 必须参数与关键字参数
参数类型:
1.必须参数,调用时候必须给参数赋值
定义过程中 def add(x,y)中x,y为形式参数
调用过程中add(1,2)中1,2为实际参数

2.关键字参数
def add(x,y):
result = x + y
return result

c = add(y=3,x=2)
关键字参数可以明确实际参数具体赋值给了哪个形式参数
关键字参数为了提高代码的可读性
必须参数和关键字参数的区别在函数的调用上,不在函数的定义上

8-6 默认参数
def print_student_files(name,gender,age,college):
print('我叫' + name)
print('我今年' + str(age) + '岁')
print('我是' + gender + '生')
print('我在' + college + '上学')

print_student_files('解宁','女',21,'石油大学')

print('~~~~~~~~~~~')

def print_student_files(name,gender='女',age=21,college='石油大学'):
print('我叫' + name)
print('我今年' + str(age) + '岁')
print('我是' + gender + '生')
print('我在' + college + '上学')

print_student_files('解宁')
print_student_files('呼呼')
print_student_files('宝贝','男',16)
print_student_files('果果',age = 16) # 使用关键字参数可以不用按照形式参数的顺序调用

这里gender,age,college都为默认参数 非默认参数不能在默认参数之后

8-7 可变参数
def demo(*param):
print(param)
print(type(param))

*param为可变参数列表

demo(1,2,3,4,5,6)
'''
输出结果:
(1, 2, 3, 4, 5, 6)

'''
a = (1,2,3,4,5,6,7,8)
demo(*a)

*a,将a元组中的每一个元素拿出来平铺出来

def demo1(param1,*param,param2 = 2):#默认参数要放到最后,不可以默认参数和可变参数夹杂着
print(param1)
print(param)
print(param2)
demo1('a',1,2,3,param2 = 'param')

8-8 关键字可变参数
def city_temp(**param):
print(param)
print(type(param)) #类型为字典
for key,value in param.items():
print(key,':',value)
city_temp(bj = '32c',xm = '36c')

a = {'bj':'32c'}
city_temp(**a)

8-9 变量作用域
c = 50

def add(x,y):
c = x + y
print(c)

变量的作用域 在函数外面定义了变量c,不在函数的作用范围 虽然函数内的c和外部变量名称一样,但是两者没有关系

add(1,2)
print(c) #打印出来是50,因为这个c是函数外部的c

函数外部定义的变量是全局变量 函数内部定义的变量为局部变量 函数内部可以引用外部变量 函数外部不可以引用函数内部变量

print('~~~~~~~')
a = 10
def demo():
print(a)
demo()

print('~~~~~~~')
def demo1():
c = 50
for i in range(0,9):
a = 'a'
c += 1
print(c)
print(a) #可以在for循环外部引用for循环变量
demo()

8-10 作用域链
c = 1
def func1():
c = 2
def func2():
c = 3
print(c)
func2()
func1()
打印出结果为3

c = 1
def func1():
c = 2
def func2():

c = 3
    print(c)
func2()

func1()
打印出结果为2

c = 1
def func1():

c = 2
def func2():
    #c = 3
    print(c)
func2()

func1()
打印出结果为1

8-11 global关键字
def demo():
global c
c = 2
demo()
print(c)
用global将局部变量变成全局变量
全局变量适用于整个应用程序,不仅仅是自己的模块

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/225693.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号