美文网首页
(十一)函数<1>

(十一)函数<1>

作者: 费云帆 | 来源:发表于2019-01-02 13:22 被阅读0次

1.概念理解:
function:
input(x)--->function--->output(x)

  • 基本实例
def test_function(a,b):
    c=a+b
    return c
if __name__=='__main__':
    result=test_function(2,3)
    print(result)
>>>5
# 更多实例

#打印功能
def print_name():
    print('This is a def print test!')
print_name()
>>>This is a def print test!
#加法
def add_function(x,y):
    return x+y
print(add_function(5,6))
>>>11
  • 延伸的实例
#传入默认值,y默认一直是1000
#def test_function(x,y=1000):
def test_function(x,y):
    print('x={}'.format(x))
    print('y={}'.format(y))
    return x+y
#print(test_function(100,200))
print(test_function(x=100,y=200))
#改变y的默认值,重新赋值即可
#print(test_function(x=100,y=2000))
>>>
x=100
y=200
300
# 修改为乘法,传入字符串
def test_function(x,y=5):
    print('x={}'.format(x))
    print('y={}'.format(y))
    return x*y
print(test_function(x='King'))
>>>
x=King
y=5
KingKingKingKingKing
  • 函数里面包含函数,直接拿过来用即可:
def test():pass
def test_function():
    print('Hello teacher cang!!!')
    test()
test_function()
>>>
Hello teacher cang!!!
# 一行搞定例子
def test_func(x,y):return x+y
>>>3
  • 多个赋值:
def test_function():
    return (1,2,3)
print(test_function())
# test_function()返回"1,2,3",相当于a,b,c=(1,2,3)
"""
>>> a,b,c=(1,2,3)
>>> a
1
>>> b
2
>>> c
3
"""
a,b,c=test_function()
print(a)
print(b)
print(c)
>>>
(1, 2, 3)
1
2
3
  • 相当于break的---return
def test_function():
    print('This is the first test!!!')
    return
    print('This is the second test!!!')
test_function()
>>>
# 第二个print没有执行
This is the first test!!!
  • 自己设置的函数文档doc:
def test_function():
    """
    This is just a test for function!!!
    Remember ,it is just a test!!!
    """
    print('This is the first test!!!')
# 函数不加括号,就是一个对象,这里访问它的"__doc__"特殊属性
print(test_function.__doc__)
>>>
    This is just a test for function!!!
    Remember ,it is just a test!!!

2.函数的属性:

def test_function():
    """
    This is just a test for function!!!
    Remember ,it is just a test!!!
    """
    print('This is the first test!!!')
print(test_function.__doc__)
# 增加属性,函数和类一样,都可以增加属性
test_function.test=100
print(test_function.test)
print(dir(test_function))
>>>
This is just a test for function!!!
    Remember ,it is just a test!!!
    
100
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'test']
# 访问其他属性
print(test_function.__name__)
print(test_function.__module__)
>>>
test_function
__main__
  • 可变的参数---*arg:
def add_function(x,*arg):
    print(x)
    print(arg)
    for i in arg:
        x+=i
    return x
print(add_function(1,2,3,4,5,6,7,8,9))
>>>
1
# 其他参数是元组的形式返回
(2, 3, 4, 5, 6, 7, 8, 9)
45

再来一个实例:

def foo(*test):
    print(test)
foo('King')
foo([1,2,3])
>>>
('King',)
([1, 2, 3],)
  • 传入字典的参数---**karg:
def foo(**karg):
    print(karg)
foo(a=1,b=2,c=3)
>>>
{'a': 1, 'b': 2, 'c': 3}
  • 综合(杂糅):
def foo(x,y,*arg,**karg):
    print(x)
    print(y)
    print(arg)
    print(karg)
foo(1,2)
foo(1,2,3,4,5)
foo(1,2,3,4,name='Jim Green')
>>>
1
2
()
{}
>>>
1
2
(3, 4, 5)
{}
>>>
1
2
(3, 4)
{'name': 'Jim Green'}
  • 一种优雅的处理方式:使用元组和字典作为函数参数传进来
def add(x,y):
    return (x+y)
tuple1=(2,3)
#会报错,因为只传入一个值
#print(add(tuple1))
print(add(*tuple1))
>>>
5
#出入字典,同理
>>> def book(author, name):
... print "{0}is writing {1}".format (author,name) #Python 3: print("{0}}is wri
ting {1}".format (author,name))
...
>>> bars = {"name":"Starter learning Python", "author":"Kivi"}
>>> book(**bars)
Kivi is writing Starter learning Python

相关文章

  • (十一)函数<1>

    1.概念理解:function:input(x)--->function--->output(x) 基本实例 延伸...

  • 十一.函数

    return的作用是:返回到调用函数的下一句函数,然后执行;assembly 汇编 disassembly 反...

  • 2022-05-31

    十一、JS函数 1、 函数的概念 在JS里面,可能会定义非常多的相同代码或者功能相似的代码,这些代码可能需要大量重...

  • python的学习笔记9

    十一、数组的创建 1、通过列表创建数组 2、numpy中定义的原生数组创建函数 (1)numpy.zeros(sh...

  • (十一)函数<2>

    1.函数的参数是"函数": 进阶实例: 2.内嵌函数: 另一个实例 内嵌函数可以直接引用外部函数的指针,但是该指针...

  • 阅读《Python编程从入门到实践》Day14

    第十一章 编写函数或类时,还可为其编写测试。通过测试,可确定代码面对各种输入都能够按要求的那样工作。 1、测试函数...

  • (十一)函数<4>几个特殊函数

    1.lambda()---匿名函数,可以把函数压缩在一行里搞定: 平常的作法: 使用lambda(): 或者这样:...

  • 十一.Excel立方体函数

    十一.Excel立方体函数 1.立方体功能 CUBEKPIMEMBER返回关键绩效指标(KPI)属性,并在单元格中...

  • 函数1

    1.函数对象 函数就是对象。 对象是“名\值”对的集合并拥有一个连接到对象原型的隐藏的连接 函数对象连接到F...

  • 函数1

    rbwb r+w+a+ 函数 函数调用 (先定义后调用) 代码执行的流程:现在内存中建立函数,...

网友评论

      本文标题:(十一)函数<1>

      本文链接:https://www.haomeiwen.com/subject/rmkzlqtx.html