Python之路,第十篇:Python入门与基础10
python3 函数
函数(function)
什么是函数:
函数是可以重复执行的代码块,可以重复使用;
作用: 定义用户级的函数;实现了一个代码块的封装;
语法:
def 函数名(参数列表):
语句块(代码块)......
语法说明:
函数名是语句块的名称;
函数名的命名规则和变量名相同(标识符);
函数的名字是一个变量,是绑定代码块的名称;
函数有自己的名字空间,要让函数处理外部数据需要用参数给函数传入一些数据,如果不需要传入参数,参数列表可以为空;
语句块部分不能为空,如果为空填充pass语句;
定义函数
def my_test():
print("hello, this is line one.")
print("hello, this is line two.")
print("hello, this is line three.") def my_test_2(a,b):
print("%s and %s" % (a,b))
函数的调用:
函数名(实际参数)
调用说明: 1,函数调用是一个表达式;(表达式用于语句中,表达式通常会返回一个值;语句可以是表达式,也可以是表达式组合;)
2,如果没有return语句, 函数执行完毕返回None值对象;
3, 如果函数需要返回其他的值对象函数需要return语句;
def my_test():
print("hello, this is line one.")
print("hello, this is line two.")
print("hello, this is line three.") def my_test_2(a,b):
print("%s and %s" % (a,b)) my_test() #函数的调用
my_test()
my_test_2(100,200) hello, this is line one.
hello, this is line two.
hello, this is line three.
hello, this is line one.
hello, this is line two.
hello, this is line three.
100 and 200
def my_test():
print("hello, this is line one.")
print("hello, this is line two.")
print("hello, this is line three.") def my_test_2(a,b):
print("%s and %s" % (a,b))
sum2 = a + b
print("sum2 = " , sum2) my_test() #函数的调用
my_test()
my_test_2(100,200)
my_test_2(300,400)
#print(sum2) 出错,sum2 不存在
hello, this is line one.
hello, this is line two.
hello, this is line three.
hello, this is line one.
hello, this is line two.
hello, this is line three.
100 and 200
sum2 = 300
300 and 400
sum2 = 700
Traceback (most recent call last): print(sum2)
NameError: name 'sum2' is not defined
def doNothing():
pass doNothing()
def my_test():
print("hello") #print( my_test() ) # 相当于以下3条语句
_temp = my_test()
print(_temp)
del _temp ============
hello
None
return 语句:
语法: return [表达式] ;([ ] 代表可以省略))
作用: 结束当前函数的执行,返回到调用该函数的地方,同时返回一个值对象的引用关系;
def my_test():
print("hello") r = my_test()
print(r) #结果None
#执行结果
#hello
#None
#================================
def my_test():
print("hello")
return None r = my_test()
print(r) # 结果None
# 执行结果
# hello
# None
#===============================
def my_test():
print("hello")
return 123 r = my_test()
print(r) # 结果None
# 执行结果
# hello 函数被调用
#
语法说明:1 ,return后跟的表达式可以省略,省略后相当于return None
2, 如果函数内没有return语句,则函数执行完最后一条语句后,返回None (相当于加了一条return None语句);
3, 函数的调用可以返回一个值或者一组值;
补充:return 语句就是讲结果返回到调用的地方,并把程序的控制权一起返回 ;
程序运行到所遇到的第一个return即返回(退出def块),不会再运行第二个return。
要返回两个数值,写成一行即可
def fun1():
return
print("此行不会打印") r = fun1()
print("r = ", r)
#================
r = None
def fun2():
#return 123
return [1,2,3] x,y,z = fun2()
print("x =", x,"y =",y,"z =",z)
#==========================
x = 1 y = 2 z = 3
练习:
def sum3(a,b,c):
print(sum([a,b,c]))
return sum([a,b,c]) def pow3(x):
print(pow(x, 3))
return pow(x, 3) #立方的和
p1 = pow3(1)
p2 = pow3(2)
p3 = pow3(3)
s = sum3(p1,p2,p3)
print("立方的和",s) #和的立方
r = sum3(1,2,3)
p = pow3(r)
print("3个数和的立方:",p)
#====================
1
8
27
36
立方的和 36
6
216
3个数和的立方: 216
函数的参数传递:
传递方式:1,位置传参; 2, * 序列传参; 3, ** 关键字传参
位置传参: 实参对应关系与形参对应关系是以位置来依次对应的;
说明: 1, 实参和形参能过位置进行对应和传递; 2,实参和形参的个数必须完全相同;
序列传参:序列的元素个数必须与列表的个数相同;
关键字传参:1, 关键字传参是指传参时,按着形参的名字给形参赋值; 2, 实参和形参按名称进行匹配;
说明: 1,字典传参的键名和形参名必须一致; 2,键名必须为字符串; 3,键名要在形参中存在;
综合传参:以上3种传递方式混合使用;
def myfunc(a, b, c):
print("a-->", a)
print("b-->", b)
print("c-->", c) #位置参数
myfunc(10,20,30)
#序列参数
s1 = [11, 12, 13]
myfunc(*s1) #等同于myfunc(s1[0],s1[1],s[2]) s2 = (11.1, 12.1, 13.1)
myfunc(*s2) s3 = "ABC"
myfunc(*s3) #关键字传参
myfunc(a=20, b=21, c=22)
myfunc(c=101, a=99, b=100)
d1 = {'c':31 , 'b':30 , 'a':29} #字典关键字传参
myfunc(**d1) #混合传参
myfunc(10, *(20,30))
myfunc(*[100,200], 300)
myfunc(*[100],200,*(300,))
myfunc(100,a=200, c=300)
函数的定义:
创建函数,函数形参
函数的缺省参数:
语法:
def 函数名(形参1=默认参数1, 形参2=默认参数2,....):
语句....
缺省参数说明:1, 缺省参数必须自右向左依次存在,如果一个参数有缺省值,则其右侧所有的参数必须要有缺省参数(缺省值);
2, 缺省参数可以有0 个或多个,甚至全部都有缺省参数;
def sum4(a, b, c=0, d=0):
return a+b+c+d print(sum4(1, 2))
print(sum4(1.1, 2.2, 3.3))
print(sum4(100,200,300,400))
===============
3
6.6
1000
函数的不定长参数:
有两种:1 , 单星号元组传参; 2, 双星号字典传参;
单星号元组传参:
语法: def 函数名(*元组形参名):
语句...
函数不定长形参
def myfunc(*args):
print("形参的个数:", len(args))
print(args)
i = 1
for x in args:
print("第",i ,"个参数是:", x)
i += 1 myfunc(1, 2)
print("-" * 100)
myfunc("one", 20, "three", 50)
=================================
形参的个数: 2
(1, 2)
第 1 个参数是: 1
第 2 个参数是: 2
----------------------------------------------------------------------------------------------------
形参的个数: 4
('one', 20, 'three', 50)
第 1 个参数是: one
第 2 个参数是: 20
第 3 个参数是: three
第 4 个参数是: 50
命名关键字形参(named):
语法: def 函数名(* , 命名关键字形参名):
语句块...
或语法: def 函数名(*args , 命名关键字形参名):
语句块...
def myfunc(a, b, *, c): #c 为命名关键字形参
print(a, b, c) myfunc(1,3,c=5) #对的
#myfunc(11,22,33) #错的 def myfunc2(a, *args, b, c): #b ,c 为命名关键字形参
print(a, b, c, args) #myfunc2(1, 2, 3, 4) #错的
myfunc2(1,2, b=3, c=5) #对的
myfunc2(1,2,3,4,5,6, b=3, c=5) #对的
myfunc2(1, b=3, c=5) #对的
==================
#执行结果
#1 3 5
#1 3 5 (2,)
#1 3 5 (2, 3, 4, 5, 6)
#1 3 5 () 空元组
双星号字典传参:
语法: def 函数名(**字典形参名):
语句...
def myfunc(**kwargs):
print("参数的个数:",len(kwargs))
for k,v in kwargs.items():
print(k,"-->",v)
print(kwargs) #调用
myfunc(name="xiaoming", age=20)
myfunc(a=10, b="BB", c=[1,2,3,4], d=True)
#myfunc(1,2,3) 错的
=================
参数的个数: 2
age --> 20
name --> xiaoming
{'age': 20, 'name': 'xiaoming'}
参数的个数: 4
c --> [1, 2, 3, 4]
b --> BB
d --> True
a --> 10
{'c': [1, 2, 3, 4], 'b': 'BB', 'd': True, 'a': 10}
练习:
def minmax(*args):
if len(args) < 2:
print("参数量太少。")
#求最小值
min_v = args[0]
for i in range(1,len(args)):
if min_v > args[i]:
min_v = args[i]
#求最大值
max_v = args[0]
for i in range(1,len(args)):
if args[i] > max_v:
max_v = args[i] return (min_v,max_v) x,y = minmax(11,22,31,14,25,36)
print("最小值",x)
print("最大值:",y)
函数的参数列表顺序:
位置形参, 缺省参数, 单星号元组形参, 双星号字典形参,命令关键字参数 都可以混合使用。
参数自左至右的顺序为:
位置形参, 单星号元组形参,命令关键字参数, 双星号字典形参,
例子: def func( a, b, *args, c, **kwargs ) :
pass
func( 100,200, 300, 400, c='C' , d='D' , e='E' )
详细语法:help(def)
练习:
def isprime(x):
if x <= 1: return False
for i in range(2, x):
if x % i == 0:
return False
return True print(isprime(1))
print(isprime(2))
print(isprime(3))
print(isprime(5))
print(isprime(6))
# def prime_m2n(m,n):
L = []
for x in range(m, n + 1):
if isprime(x):
L.append(x)
return L prime_m2n(1,10)
prime_m2n(1,20)
M = prime_m2n(1,50)
print(M)
#
def primes(n):
return prime_m2n(1, n) L = primes(100)
print(L) #结果
#False
#None
#True
#True
#False
#[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
#[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
可变和不可变类型 的实参的参数传递的区别:
可变:list, set, dict
不可变:frozenset , tuple, str, float, int....
问题:函数只能通过返回值传递参数吗?
L = []
def func(x):
x.append(10) func(L)
print(L)
#L的值是10 #栗子2
D = {}
def func2(x):
x["name"] = "xiaoming" func2(D)
print(D) #{'name': 'xiaoming'}
区别: 不可变的类型的数据作为函数参数传入时,函数内部不会改变变量的源数据值,是安全的;
可变类型的数据作为函数参数传入时,函数内部可变原数据,多用来返回更多数据结果;
L = []
def func(x=[]):
while True:
names = input("请输入学生姓名:")
if not names:
break
x.append(names)
return x #方式1,
r = func()
print(r)
#方式2,
func(L)
print(L)
===============
请输入学生姓名:aa
请输入学生姓名:bb
请输入学生姓名:
['aa', 'bb']
请输入学生姓名:cc
请输入学生姓名:dd
请输入学生姓名:
['cc', 'dd']
函数嵌套
函数嵌套是指一个函数里用语句来创建其他函数的情况;
函数变量
函数名是变量, 它在创建函数是绑定一个函数
def fn():
print("hello") f1 = fn
f1() #等同于调用函数fn()
def fn_outer(): #外部函数
print("外部函数被调用")
def fn_inner():
print("fn_inner被调用")
fn_inner()
fn_inner()
print("外部函数被调用结束") fn_outer()
===================
外部函数被调用
fn_inner被调用
fn_inner被调用
外部函数被调用结束
函数作为函数的返回值
def func1():
def func2():
print("hello")
return func2
fn = func1()
fn() #返回hello
练习: 加乘法
def get_op(op):
if op == '+':
def myadd(x,y):
return x + y
return myadd
elif op == '*':
def mymul(x,y):
return x * y
return mymul a = int(input("Please enter the first number:"))
operator = input("请输入操作方式:")
b = int(input("Please enter the second number:"))
fn = get_op(operator)
print("Result:", fn(a, b))
=====================================
Please enter the first number:3
请输入操作方式:+
Please enter the second number:4
Result: 7
Please enter the first number:3
请输入操作方式:*
Please enter the second number:6
Result: 18
函数作为函数的参数传递
def tab(x, y):
return "|" + x.center(12) + "|" + y.center(12) + "|" def string(x, y):
return "names : " + x + "ages : " + y def myprint(fn, x, y):
s = fn(x, y)
print(s) myprint(tab, "xiaoming", "")
myprint(string, "xiaoli", "")
#结果
#| xiaoming | 18 |
#names : xiaoliages : 19
def goodbye(L):
for x in L:
print("886:", x) def hello(L1):
for x in L1:
print("hello:", x) def operator(fn, M):
fn(M) operator(goodbye, ("xiaoming","xiaoli"))
operator(goodbye, ["xiaoming","xiaoli"])
operator(hello, ("xiaoming","xiaoli"))
operator(hello, ["xiaoming","xiaoli"])
#结果
#886: xiaoming
#886: xiaoli
#886: xiaoming
#886: xiaoli
#hello: xiaoming
#hello: xiaoli
#hello: xiaoming
#hello: xiaoli
全局变量和局部变量
局部变量: 定义在函数内部的变量(包含函数参数)
全局变量: 定义在函数外部,模块内部的变量
v = 100 #此为全局变量 def fn():
v = 200 #此为局部变量
print(v) fn()
print(v)
#结果
#
#
python作用域
作用域: 也叫名字空间,是变量访问的时候查找变量名的范围空间;
python 四个作用域:
局部作用域(函数内) Local L
外部嵌套函数作用域 Encloseing function locals E
函数定义所在模块(文件)的作用域 Global (module) G
python内置模块的作用域 Builtin(Python) B
变量名的查找规则:
在访问变量时, 先查找本地变量, 然后是包裹此函数的外部函数的函数内部的变量, 之后是全局变量,最后是内置变量。
字母顺序: L --> E ---> G ---> B
v = 100 def fun1():
v = 200
print("fun1_v :" , v)
def fun2():
v = 300
print("fun2_v :",v)
fun2() fun1()
print("v=", v)
#结果
#fun1_v : 200
#fun2_v : 300
#v= 100
在默认情况下,变量名赋值会创建或修改本地变量
v = 100
def fn():
v = 200 fn()
print(v) #
global 语句
作用:告诉解释器,global语句声明的一个或多个变量,这些变量的作用域为模块级的作用域,也称作全局变量;
对全局声明(global)的变量赋值将映射到模块文件的内部作用域;
语法: global 变量名1 ,变量名2 ....
v = 100
def fn():
global v #声明全局变量
v = 200 fn()
print(v) #
global 说明:1 , 全局变量如果要在函数内部被赋值, 则必须经过全局声明,否则 被认为是局部变量;
2, 全局变量在函数内部不经过声明就可以直接访问(前提是变量已经存在);
3, 不能先声明局部变量,再用global声明为全局变量,此做法不符合语法规则;
4, global 变量列表里的变量名不能出现在此作用域的参数列表里,for 循环控制目标,类定义,函数定义及import导入名字中;
def fn2():
v = 200
global v # 错的 fn2()
print("3:",v)
#4global 变量列表里的变量名不能出现在此作用域的参数列表里 def fn3(v):
global v
v = 300 fn3(11)
print(v) #SyntaxError: name 'v' is parameter and global
nonlocal语句
作用: 告诉解释器,nonlocal声明的变量不是局部变量,也不是全局变量,而是外部嵌套函数内的变量。
语法: nonlocal 变量名1, 变量名 .....
nonlocal在嵌套函数里的内层函数里加载;
说明:1,nonlocal 语句只能在被嵌套函数的内层使用;
2, 访问nonlocal 变量 将对外部嵌套函数 的作用域内的变量进行操作;
3, 嵌套函数有两层或者两层以上时,访问nonlocal 变量只对最近一层的变量操作;
4,nonlocal语句的变量列表里的变量名,不能出现在此作用域的参数列表里;
vars = 100 def outter():
var = 200
def inner():
nonlocal var #指定var为外层嵌套函数作用域
var += 1 #么有nonlocal var,此行会出错UnboundLocalError: local variable 'var' referenced before assignment
print("inner_var:",var)
inner()
print("outter_var:",var) outter()
=========================
#inner_var: 201
#outter_var: 201
#shuoming3
def f1():
v = 100
def f2():
v = 200
def f3():
nonlocal v
v = 400
print("f3:",v)
f3()
print("f2:",v)
f2()
print("f1:",v)
f1()
=============
400
400
100
Python之路,第十篇:Python入门与基础10的更多相关文章
- Python之路(第二十篇) subprocess模块
一.subprocess模块 subprocess英文意思:子进程 那什么是进程呢? (一)关于进程的相关理论基础知识 进程是对正在运行程序的一个抽象,进程的概念起源于操作系统,是操作系统最核心的概念 ...
- Python之路(第十篇)迭代器协议、for循环机制、三元运算、列表解析式、生成器
一.迭代器协议 a迭代的含义 迭代器即迭代的工具,那什么是迭代呢? #迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 b为何要有迭代器? 对于序列类型:字符串.列表 ...
- 【Python之路】第九篇--Python基础之线程、进程和协程
进程与线程之间的关系 线程是属于进程的,线程运行在进程空间内,同一进程所产生的线程共享同一内存空间,当进程退出时该进程所产生的线程都会被强制退出并清除.线程可与属于同一进程的其它线程共享进程所拥有的全 ...
- Python之路(第五篇) Python基本数据类型集合、格式化、函数
一.变量总结 1.1 变量定义 记录某种状态或者数值,并用某个名称代表这个数值或状态. 1.2 变量在内存中的表现形式 Python 中一切皆为对象,数字是对象,列表是对象,函数也是对象,任何东西都是 ...
- 【Python之路】特别篇--Python面向对象(进阶篇)
上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使 ...
- Python之路【第九篇】堡垒机基础&数据库操作
复习paramiko模块 Python的paramiko模块,是基于SSH用于连接远程服务器并执行相关操作. SSHClient #!/usr/bin/env python #-*- coding:u ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- Python之路(第七篇)Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
一.作用域 return 可以返回任意值例子 def test1(): print("test1") def test(): print("test") ret ...
- 【Python之路】特别篇--Python切片
字符串切片操作 切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割. 注意: 数是可选的,而冒号是必须的. consequence[start:end:step] 切片操作符中的 ...
- 【Python之路】特别篇--Python内置函数
abs() 求绝对值 i = abs(-100) print(i) # 100 all() 循环里面的参数 如果每个元素都为真,那么all返回值为真 假: 0 False None "&q ...
随机推荐
- 解决iOS第三方SDK之间重复的symbols问题
前言:今天公司项目准备使用高德导航,其中用到了高德3D地图SDK,然后就出现bug了.在真机上可以完美运行,但是在模拟器上,就出现了一大片的bug:提示有82个Duplicate symbols,仔细 ...
- Django之cookie+session
前言 HTTP协议 是短连接.且状态的,所以在客户端向服务端发起请求后,服务端在响应头 加入cokie响应给浏览器,以此记录客户端状态: cook是来自服务端,保存在浏览器的键值对,主要应用于用户登录 ...
- python字符串内建函数
- 把旧系统迁移到.Net Core 2.0 日记(3) - 详解依赖注入 (转)
关于DI 依赖注入, 转载这篇文章, 写得很好的. ----------------------------- DI在.NET Core里面被提到了一个非常重要的位置, 这篇文章主要再给大家普及一下关 ...
- MFC消息-自定义消息
转:http://blog.csdn.net/penpenandtongtong/article/details/18598907 像MFC的窗口发送消息,可以进行自定义的动作行为,因此很多时候非常有 ...
- 每天进步一点点out1
1● attend ətend 2● infant əfənd
- Echarts dataZoom缩放功能参数详解:
dataZoom=[ //区域缩放 { id: 'dataZoomX', show:true, //是否显示 组件.如果设置为 false,不会显示,但是数据过滤的功能还存在. backgroundC ...
- 服务消费和负载(Ribbon)
使用RestTemplate调用服务 在上一篇教程中,我们是这样调用服务的,先通过 LoadBalancerClient 选取出对应的服务,然后使用 RestTemplate 进行远程调用. Load ...
- oracle servicename 与SID的区别
http://blog.csdn.net/z69183787/article/details/25706269
- It is never too late!
整理着过去的学习笔记,零零碎碎的,偶尔夹杂着当时的心境. 泛泛的学着东西,不很系统,不很深入,倒像是在拾海,有时捡捡贝壳,有时抓抓螃蟹.叹服大海的神奇,还没来得及深钻某个领域. (以下内容写于2016 ...