第12节--function(下)

本文详细介绍了Python中函数的四种参数类型:默认参数、关键字参数、可变参数(VarArgs)。通过具体的代码示例展示了每种参数类型的使用方法及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

函数参数

默认参数
关键字参数
VarArgs参数

Code

# -*- coding: utf-8 -*-

# default parameter
def repeated_str(str,times = 1):
    repeated_strs = str * times
    return repeated_strs

# repeated_strings = repeated_str("hello world")
# print(repeated_strings)

repeated_strings = repeated_str("hello world\n",4)
print(repeated_strings)


# keyword parameter
def func(a,b = 4,c = 8):
    print("a is ",a,"b is ",b,"c is ",c)

func(13,17)
func(100,c = 101)
func(c = 200,a = 10)


# varArgs parameter
def print_paras(fpara,*nums,**words):
    print("fpara= "+str(fpara))
    print("nums= "+str(nums))
    print("words= "+str(words))

print_paras("hello world",1,2,3,4,a = 10,b = 20,c = 30)

console

hello world
hello world
hello world
hello world

a is  13 b is  17 c is  8
a is  100 b is  4 c is  101
a is  10 b is  4 c is  200
fpara= hello world
nums= (1, 2, 3, 4)
words= {'c': 30, 'a': 10, 'b': 20}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值