Python基础学习_传递参数与参数类型

文章探讨了Python中定义函数时的位置参数、关键词参数的使用,以及如何设置默认值。通过`trapezoid_area`函数的例子展示了如何传递不同类型的参数来计算梯形面积,并引入用户输入动态设定高度。

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

******
20230214
******

# 传递参数与参数类型 (位置参数,关键词参数) (初级版)

def trapezoid_area(base_up,base_down,height):
    return 1/2 * (base_up + base_down) * height

t = trapezoid_area(1,2,3)
# t1 = trapezoid_area(base_up= 1 ,base_down= 2,height=3)
# t2 = trapezoid_area(height=3,base_up= 1 ,base_down= 2)
# print(t,t1,t2)
print(t)

******
# 传递参数与参数类型 (位置参数,关键词参数) (进阶版)

def trapezoid_area(base_up,base_down,height=3):
    return 1/2 * (base_up + base_down) * height

t = trapezoid_area(1,2)
t1 = trapezoid_area(1,2,height=15)
t2 = trapezoid_area(1,2,15)
print(t,t1,t2)
# print(t)
# print(t1)
# print(t2)


******
# 传递参数与参数类型 (位置参数,关键词参数) (高阶版)

temp = int(input("write down the height number: "))

def trapezoid_area(base_up,base_down,height=temp):
    return 1/2 * (base_up + base_down) * height

t = trapezoid_area(1,2)
t1 = trapezoid_area(int(input("write down the base_up number: ")),\
                    int(input("write down the base_down number: ")))
print(t)
print(t1)

******

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值