python--函数学习

函数
函数是实现某个功能,可以重复调用,只有调用的时候才会执行
# 函数定义
def fn(形参1,形参2):
    pass
fn(实参,实参,)
# 默认参数,传和不传都可以
def fn(a,b,c=4):
    print("a=",a)
    print("b=",b)
    print("c=",c)
fn(2,4)
# 位置参数,关键字参数(放在最后)
def fn(a,b,c=4):
    print("a=",a)
    print("b=",b)
    print("c=",c)
fn(2,b=8)

# 不定长参数,如:计算任意个数字的和,但是他不能接受关键字实参
所以就有**
def fn(*args):
    result = 0
    for i in args:
        result += i
    print(result)
fn(2, 5, 6, 7, 8, 9, 4, 3)
============================
# 不定长参数,如:计算任意个数字的和
def fn(*args, **kwargs):
    result = 0
    for i in args:
        result += i
    print(result)
fn(2, 5, 6, 7, 8, c=9, b=4, a=3)
实参
# 实参,可以是任何类型,容易出错
def fn(a):
    a[0] = 8
    print(a)
c = [3, 45, 5]
# fn(c.copy()) 将c的副本传进去,这样c不会跟着变
fn(c[:])
print(c)
实参解包
# 解包,两种方式
def fn(a, b, c):
    print(a, b, c)
t = [3, 45, 5]
fn(t[0], t[1], t[2])
fn(*t)
函数的返回值
# 返回值:我想要函数的返回值,如何处理由我自己
# 要点:1 返回值必须要有变量来接受
# 此函数的意思:fn()==a,b,c   fn()==return返回的值
# return执行后后面的语句不会再执行
def fn(a, b, c):
    return a,b,c
t = [3, 45, 5]
# 必须要有变量来接受函数
T=fn(t[0], t[1], t[2])
print(T)
fn 和fn()的区别
# fn和fn()的区别
# fn()=====等于10
def fn():
    return 10
f = fn()
# print(fn) <function fn at 0x00000241579B1EA0>
# print(fn()) 10
# fn函数对象==相当于机器    fn()机器做成的冰激凌
# help函数查询  参数是对像,步是调用
print(help(print))
=====================================打印结果,将print的用法打印出来
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
None
若是你也想用help打印出来===========================================
def penson():
    """
    文档说明:
    此函数的功能====,这里就是写说明的
    为的是让别人易读
    """
    pass
print(help(penson))
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

人工智能小李子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值