python程序设计基础4:python函数设计和使用
2014-03-28 15:05
615 查看
这一节我们将学习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
结果:
please input two number: 2,3
3 2
2 3
可以看出来,没有得到我们想要的结果。在C语言中使用指针的方式来解决这个问题。
所以,我们使用全局变量:
def func():
global a,b
a,b=input('please input 2numbers:')
if a<b:
a,b=b,a
print 'a=',a,'b=',b
func()
结果:
please input 2numbers:2,3
a= 3 b= 2
3、python中的‘*’和'**‘可以处理输入参数的个数不确定的情况,’**‘主要用于字典的引用。区别于C语言中的指针引用。
例子:定义一个函数,如下:
def func(a,b,c=1,*p,**pa):
print a,b,c
print p
print pa
func(1,2,3,4,5,6,7,x=3,y=5,z='iker')
结果:
1 2 3
(4, 5, 6, 7)
{'y': 5, 'x': 3, 'z': 'iker'}
4、输入一个年份,判断是否为闰年。
代码:
def runnian(a):
if a%400==0 or (a%4==0 and a%100!=0):
print'%d is a run year!'%a
else:
print'%d is not a run year!'%a
a=input('please input a year number:')
runnian(a)
结果:
please input a year number:2013
2013 is not a run year!
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
结果:
please input two number: 2,3
3 2
2 3
可以看出来,没有得到我们想要的结果。在C语言中使用指针的方式来解决这个问题。
所以,我们使用全局变量:
def func():
global a,b
a,b=input('please input 2numbers:')
if a<b:
a,b=b,a
print 'a=',a,'b=',b
func()
结果:
please input 2numbers:2,3
a= 3 b= 2
3、python中的‘*’和'**‘可以处理输入参数的个数不确定的情况,’**‘主要用于字典的引用。区别于C语言中的指针引用。
例子:定义一个函数,如下:
def func(a,b,c=1,*p,**pa):
print a,b,c
print p
print pa
func(1,2,3,4,5,6,7,x=3,y=5,z='iker')
结果:
1 2 3
(4, 5, 6, 7)
{'y': 5, 'x': 3, 'z': 'iker'}
4、输入一个年份,判断是否为闰年。
代码:
def runnian(a):
if a%400==0 or (a%4==0 and a%100!=0):
print'%d is a run year!'%a
else:
print'%d is not a run year!'%a
a=input('please input a year number:')
runnian(a)
结果:
please input a year number:2013
2013 is not a run year!
相关文章推荐
- python程序设计基础--函数(下)
- python程序设计基础5:python文件使用(遍历一个文件夹中所有的图片)
- Python基础之——使用字典和函数构造switch
- Python基础知识——split()函数的使用
- Python程序设计基础 函数与模块习题
- python基础:os模块中关于文件/目录常用的函数使用方法
- Python学习入门基础教程(learning Python)--2.3.1 Python传参函数设计
- Python3.5基础——函数的定义与使用
- python 9-1 如何使用函数装饰器,定义函数装饰器,用来生成一个原函数基础添加新功能的函数,替代原函数
- Python基础语法(2)——字典、函数定义使用、控制流(if&for&while)
- Python基础-函数及函数参数神奇的使用
- Python基础—08-函数使用(02)
- Python学习入门基础教程(learning Python)--2.3.1 Python传参函数设计
- Python学习入门基础教程(learning Python)--2 Python简单函数设计 .
- 流程的Python 第六章:使用一等函数来设计模式
- 嵌入式开发之C基础学习笔记05--模块化设计和函数的使用
- as汇编基础程序设计--函数设计及函数堆栈传参问题
- python基础===getattr()函数使用方法
- [Python学习笔记][第五章Python函数设计与使用]
- 第十周刷OJ题 统计程序设计基础课程学生的平均成绩(静态成员函数的使用复习)