Python函数中三大主要应用形式
Python函数在使用的时候有几大分类,其中以int函数,float函数和str函数的应用最为广泛。下面我们就看看具体的环境中如何进行相关的函数代码的编写。下面我们先来看看int函数在Python函数中的作用。
(1)把符合数学格式的数字型字符串转换成整数
(2)把浮点数转换成整数,但是只是简单的取整,而非四舍五入。
举例:
aa = int("124") #Correct
print "aa = ", aa #result=124
bb = int(123.45) #correct
print "bb = ", bb #result=123
cc = int("-123.45") #Error,Can't Convert to int
print "cc = ",cc
dd = int("34a") #Error,Can't Convert to int
print "dd = ",dd
ee = int("12.3") #Error,Can't Convert to int
print ee float函数将整数和字符串转换成浮点数。
举例:
aa = float("124") #Correct
print "aa = ", aa #result = 124.0
bb = float("123.45") #Correct
print "bb = ", bb #result = 123.45
cc = float(-123.6) #Correct
print "cc = ",cc #result = -123.6
dd = float("-123.34") #Correct
print "dd = ",dd #result = -123.34
ee = float('123v') #Error,Can't Convert to float
print ee Python str函数将数字转换成字符举例:
相关推荐
夜斗不是神 2020-11-17
wordmhg 2020-11-06
世事一场大梦 2020-11-17
xiaoseyihe 2020-11-16
Morelia 2020-11-03
CloudXli 2020-11-03
pythonxuexi 2020-10-30
三石 2020-10-29