python程序设计基础4:python函数设计和使用

本文介绍了Python函数的基础知识,包括输入圆半径计算面积、数值交换、处理不定数量参数及判断闰年等功能的实现。通过示例展示了函数在简化代码和提高复用性上的优势。
摘要由CSDN通过智能技术生成

这一节我们将学习python函数的使用,包括一些变量以及参数的设定还有如何写出可以复用的代码。函数就是一段具有特定功能的、被封装的、可重用的程序。主要还是以例进行学习。

1、输入圆形的半径,得出相应的圆的面积。

少量的几个半径,其实你可以将代码重复的赋值几次代码就行了。但是为了代码的简洁,你可以写一个简单的循环。

代码:

radius=input('please input radiuses in list')
for r in radius:
    print'the area of the circle with the radius of %f is:'%r,3.14*r*r

结果:

please input radiuses in list[2,3,4]
the area of the circle with the radius of 2.000000 is: 12.56
the area of the circle with the radius of 3.000000 is: 28.26
the area of the circle with the radius of 4.000000 is: 50.24

2、比较两个数的大小,若是a小于b,则交换两个的位置。

x,y=input('please input two number: ')


def swap(a,b):
    if a<b:
        a,b=b,a
        print a,b
swap(x,y)
print x,y

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值