当前位置: 移动技术网 > IT编程>脚本编程>Python > python学习-23 函数

python学习-23 函数

2019年07月02日  | 移动技术网IT编程  | 我要评论

126笑话大全,蒋存祺,征婚信息

函数

1.函数分为:数学定义的函数和编程语言中的函数

 

例如: - 数学定义的函数:y=2*x+1

            - 编程语言的函数:

               def test(x):

                          x += 1

                         return x     

              def  :定义函数的关键字

              test :函数名

              ():定义参数

             return : 返回函数

 

def test(x):
    x += 1
    return x
print(test(1))

运行结果:

2

process finished with exit code 0

 

2.函数的过程

没有返回值return的函数

def test():
    x = 1
    print(x)
print(test())

运行结果:

1
none

process finished with exit code 0

 

返回值数=0  返回none

返回值数=1 返回object

返回值数>1 返回tuple

 

3.函数的参数

-----

def test(x,y):                     #   x,y 是形参: 不占内存空间
    a = x**y
    return a

b=(123,321)                       # 123,321是实参
print(b)

 

---- 对应

def test(x,type='mysql'):
    print(x)
    print(type)
test('hello')
test('hey','sql')

运行结果:

hello
mysql
hey
sql

process finished with exit code 0

 

-- 参数组

第一种:

def test(x,*args):
    print(x)
    print(args)
    print(args[1])          # 通过索引获取test元素的字符
test(1,2,4,3)

运算结果:

1
(2, 4, 3)
4

process finished with exit code 0

 

第二种:

def test(x,**kwargs):
    print(x)
    print(kwargs)

test(1,a=1,b=2)

运行结果:

1
{'a': 1, 'b': 2}

process finished with exit code 0

第三种:

def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)

test(1,'abc',333,name='john',age=18)

运行结果:

1
('abc', 333)
{'name': 'john', 'age': 18}

process finished with exit code 0

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网