Python基础学习之函数(1)

来源:互联网 发布:中南民族大学网络 编辑:程序博客网 时间:2024/05/18 02:47

函数

1.在python中创建和定义函数只需要def+函数名+(参数)+冒号

2.怎么调用函数:直接使用函数即可

例子:

>>> def MyFirstFunction():

print('my First function!')

 

>>> MyFirstFunction()

my First function!

>>> #发现MyFirstFuntion()就会网上找到此函数并且执行它。

>>> def MySecondFunction(name):

         print(name+'  hello')

 

        

>>> MySecondFunction('aaa')

aaa hello

多个参数需要用逗号隔开。

 

3.函数的返回值:

只需要在函数体里面return即可

>>> def add(num1,num2):

         returnnum1+num2

 

>>> add(5,6)

11

>>> print(add(5,6))

11

 

4.形参和实参

简单地说,形参是函数定义过程中括号内的参数;实参是调用的时候传递到函数体的参数。

 

5.函数文档用’ ‘来括起来

注释用#

给函数写文档可以让别人更好滴理解自己的函数

注释不会被打印出来函数文档也一般不会被打印出来,但是通过函数名.__doc__可以(双下划线,__doc__是函数的特殊属性)打印函数文档。例如:

>>> def MyFirstFunction(name):

         '这是函数文档'

         #有一个参数

         print('hello'+name)

        

>>> MyFirstFunction('aaa')

hello aaa

>>> MyFirstFunction.__doc__

'这是函数文档'

 

6.使用关键字参数,可以使参数前后的位置调换顺序

>>> def belongs(a,b):

         print(a+' belongs to ' +b)

 

        

>>> belongs(b='fruits',a='apple')

apple belongs to fruits

 

7.默认参数:在函数定义的时候为其赋初值。

 

8.收集参数:在参数前面加上*

会将传递进来的参数打包成一个元组然后传递给形参,例如:

>>> def test(*params):

         print('参数的长度是:',len(params))

         print('第二个参数是:',params[1])

 

        

>>> test(1,'aaa',3,14,5,6,7,8)

参数的长度是:  8

第二个参数是:  aaa

 

注意:如果收集参数后面还有定制的参数,则后面的参数要用关键字参数去指定,否则会将其打包在前面的收集参数中从而出现缺少参数的错误。例如:

>>> def  test(*params,t):

         print('参数的长度是:',len(params))

         print('第二个参数是:',params[1])

         print(t)

 

        

>>> test(1,'aaa',3,14,5,6,7,8)

Traceback (most recent call last):

 File "<pyshell#28>", line 1, in <module>

   test(1,'aaa',3,14,5,6,7,8)

TypeError: test() missing 1 requiredkeyword-only argument: 't'

>>> test(1,'aaa',3,14,5,6,7,t=8)

参数的长度是:  7

第二个参数是:  aaa

8

 

建议:如果收集参数后面还有参数,可以将其设置为默认参数(给定一个默认值)

 

 

函数与过程

1.简单地说,函数是有返回值的,过程是简单的没有返回值的函数体

python严格来说只有函数没有过程。

 

>>> def hello():

         print('helloworld')

 

        

>>> temp = hello()

hello world

>>> temp

>>> #函数没有任何显示,代表没有返回值吗?看看temp的类型就知道了

>>> type(temp)

<class 'NoneType'>

>>> #即没有return语句会返回None

 

 

2.可以用列表让python返回多个类型的值

例如:

>>> def back():

         return[1,'a',3.33]

 

>>> back()

[1, 'a', 3.33]

 

3.函数变量的作用域问题(变量的可见性)

全局变量和局部变量

 

>>> def discount(price,rate):

         final_price= price*rate

         returnfinal_price

 

>>> old_price = float(input('inputprice:'))

input price:100

>>> rate = float(input('inputrate'))

input rate0.8

>>> new_price =discount(old_price,rate)

>>> new_price

80.0

>>> final_price

Traceback (most recent call last):

 File "<pyshell#58>", line 1, in <module>

   final_price

NameError: name 'final_price' is notdefined

此函数中的final_price是局部变量,只能在函数中用,出了函数体则不可以访问其值。

 

在函数中可以访问全局变量。

使用全局变量的时候要小心,如果修改了全局变量,试图修改全局变量时,python会创建一个局部变量来代替全局变量,此局部变量的名称和全局变量相同但是使用不同的内存,所以在全局范围内的全局变量的值还是没有被改变的。

例子:def discount(price,rate):

   final_price = price*rate

   old_price = 50

   print('函数中的old_price的值:',old_price)

   print('--------funciton over--------------')

   return final_price

 

old_price = float(input('input price:'))

rate = float(input('input rate:'))

new_price = discount(old_price,rate)

print('外面的old_price的值:',old_price)

运行效果:

input price:100

input rate:0.8

函数中的old_price的值: 50

--------funciton over--------------

外面的old_price的值: 100.0

 

那怎么去修改全局变量呢?

global关键字修饰 此变量,函数内修改全局变量就可以牵一发而动全身


0 0
原创粉丝点击