python函数

函数介绍


提高代码利用率,将重复执行的代码单独放在一块;

定义函数:函数名满足标识符规则,首字母不能大写。

def 函数名():
           代码
#定义函数: 
  1 def print_hell():
  2     print("hello,world")
#执行函数
  3 print_hell()
[root@localhost python]# python3 25.py 
hello,world

函数中的参数:

[root@localhost python]# 
  1 def test1(r):
  2     s=3.14*(r**22)
  3     print("圆的面积为:%s"%s)
  4 
  5 r=int(input("input r:"))
  6 test1(r)

  7 test1(3) #直接传参

[root@localhost python]# python3 26.py 
input r:3
圆的面积为:98536527172.26001
圆的面积为:98536527172.26001

 函数中的多参数:

def 函数名(参数1,参数2 .......) #参数名可以随便写,在之前不需要定义,形参:特点不需要在前面定义,也不会和之前定义的变量冲突
  1 a=1
  2 def test2(a,b):
  3     sum = a + b
  4     print("%s+%s=%s"%(a,b,sum))
  5 b=2
  6 test2(a,b)
  7 test2(a,2)#在调用函数的时候传给函数参数的数据叫实参,20是实参,a=1为实参。实参可以引用定义的变量,也可以定义一个值

[root@localhost python]# python3 27.py 
1+2=3
1+2=3

定义时小括号中定义的参数为形参,调用时小括号中定义的参数为实参

函数返回值

  1 def cel(a,b,c):
  2     sum = a + b -c
  3     return s #return被执行之后,不管return后面还有什么代码都能不会执行
  4 def s_noreturn(a,b):
  5     sum1=a+b
  6 a=1
  7 b=2
  8 c=1
  9 s=cel(a,b,c) #需要一个变量接收函数返回值。
 10 s2=s_noreturn(a,b)
 11 print(s2)
 12 print(s)
 13 
[root@localhost python]# python3 28.py 
None  #默认函数返回值
2
  
  1 def cel(a,b):
  2     if not isinstance(a,(int,float)):#判断变量类型,满足其中一种类型返回True
  3         print("传入的a:%s不是数字类型"%a)
  4         return
  5     elif not isinstance(b,(int,float)):#判断变量类型,满足其中一种类型返回True
  6         print("传入的a:%s不是数字类型"%a)
  7         return
  8     else:
  9         sum = a + b 
 10         return sum    
 15 s=cel("abc",2)
 16 print(s)
 15 s=cel(1,"2")
 16 print(s)
 17 a=1 
 18 b=2 
 19 s=cel(a,b) 
 20 print(s)
[root@localhost python]# python3 28.py 
传入的a:abc不是数字类型
None
传入的a:2不是数字类型
None
3

练习:

  
  1 def avg_3(a,b,c):
  2     if is_number(a) and is_number(b) and is_number(c):
  3         return (a+b+c)/3
  4     else:
  5         print("传入参数类型不对")
  6 
  7 
  8 def is_number(a):
  9     if not isinstance(a,(int,float)):
 10         print("传入的参数%s不是一个数字"%a)
 11         return False
 12     else:
 13         return True
 14 
 15 avg=avg_3("abc",2,3)
 16 print(avg)
 17 avg=avg_3(4,4,4)
 18 print(avg)

[root@localhost python]# python3 29.py 
传入的参数abc不是一个数字
传入参数类型不对
None
4.0

 python中代码的执行顺序。

  1 avg=avg_3("abc",2,3)
  2 print(avg)
  3 avg=avg_3(4,4,4)
  4 print(avg)
  5 def avg_3(a,b,c):
  6     if is_number(a) and is_number(b) and is_number(c): 
  7         return (a+b+c)/3
  8     else:
  9         print("传入参数类型不对")
 10 
 11 
 12 def is_number(a):
 13     if not isinstance(a,(int,float)):
 14         print("传入的参数%s不是一个数字"%a)
 15         return False
 16     else:
 17         return True
[root@localhost python]# python3 29.py Traceback (most recent call last): File "29.py", line 1, in <module> avg=avg_3("abc",2,3) NameError: name 'avg_3' is not defined
#python中代码执行从第一行开始执行,读一行,翻译一行,调用函数要放在定义之后。先执行第一行代码def,内存中加载函数体,下来执行第12行代码,加载第二个函数体,下来执行19行,执行调用函数,

1 def avg_3(a,b,c): 2 if is_number(a) and is_number(b) and is_number(c): 3 return (a+b+c)/3 4 else: 5 print("传入参数类型不对") 6 7 avg=avg_3("abc",2,3) 8 print(avg) 9 avg=avg_3(4,4,4) 10 print(avg) 11 12 def is_number(a): 13   if not isinstance(a,(int,float)): 14     print("传入的参数%s不是一个数字"%a) 15     return False 16    else: 17    return True [root@localhost python]# python3 29.py Traceback (most recent call last): File "29.py", line 7, in <module> avg=avg_3("abc",2,3) File "29.py", line 2, in avg_3 if is_number(a) and is_number(b) and is_number(c): NameError: name 'is_number' is not defined [root@localhost python]#

全局变量和局部变量

局部变量:函数内部定义的变量;不同的函数可以定义相同名字的局部变量,但是各用各的不会产生影响,局部变量的作用,为了临保存数据需要在函数中定义变量来进行存储

全局变量:如果一个变量,既能在一个函数中使用,也能在其他函数中使用,这样的变量就是全局变量。

  1 def test1(): #第一行加载函数体,并未执行
  2     a=200 #a为局部变量,局部变量只能在该函数中使用,作用范围在该函数里面,出了函数之外就不生效了
  3     print(a)
  4 print(a) #第二条执行此行代码
  5 test1()
[root@localhost python]# python3 30.py 
Traceback (most recent call last):
  File "30.py", line 4, in <module>
    print(a)
NameError: name 'a' is not defined
  1 def test1():
  2     a=200
  3     print(a)
  4 def test2():
  5     a=300
  6     print(a)
  7 test1()
  8 test2()
[root@localhost python]# python3 30.py 
200
300
  1 a=400#全局变量可以在后面的代码中使用。
  2 def test1():
  3     a=200#局部变量只能在该函数中使用,作用范围在该函数中,不一定是修改全局变量。一般来说是在函数中声明了一个局部变量。
  4     a+=1
  5     print(a)
  6 def test2():
  7     #a=300
  8     print(a) #输出的a为全局变量。
  9 test1()
 10 test2()
[root@localhost python]# python3 30.py 
201
400      
  1 a=400#全局变量可以在后面的代码中使用
  2 def test1():
  3     a=200#局部变量只能在该函数中使用,作用范围在该函数中
  4     a+=1
  5     print(a)
  6     return a #将函数中的返回值通过参数传递到其他函数中
  7 def test2(a):
  8     #a=300#如果函数中的局部变量和全局变量的名字一样,优先使用局部变量,形参可以理解为局部变量。
  9     print(a)
 10 b=test1()
 11 test2(b)
[root@localhost python]# python3 30.py 
201
201
原文地址:https://www.cnblogs.com/zhaoyujiao/p/9032711.html