Python函数之系统函数的调用

版权声明:本文为博主原创文章,转载请标明出处http://blog.csdn.net/cdhahaha https://blog.csdn.net/cdhahaha/article/details/83153492

全部测试代码

#!/usr/bin/evn python3
#_*_conding:utf-8 _*_

#系统内置函数
#1.abs():查看绝对值,如果传入的参数不对,会报TypeError

print('-100的绝对值--',abs(-100))


#2.max():查看最大值

print('最大值为---',max(1,2,9))

#不同类型的比较会报类型错误
#print('不同类型的比较',max('1',2,3))

#3.数据类型转换
#int():把其他类型转换为int
i_2_s=int('111')
print('将str转换为int--',i_2_s)

#float():把其他类型转换为float

i_2_f=float('12.3')
print('字符串转为浮点数---',i_2_f)


#str():把其他类型转换为字符串
b_2_s=str(True)

print('将布尔转为str---',b_2_s)

i_2_s=str(109)

print('将int转换为str---',i_2_s)


#bool():将其他类型转为布尔

s_2_b = bool('1')

s_2_b2 = bool('2')

print('str转为布尔--',s_2_b,s_2_b2)

i_2_b = bool(2)

i_2_b2 = bool(3)

print('int转为bool--',i_2_b,i_2_b2)


#更多内置函数,可以查看官方API文档

1,1.abs():查看绝对值,如果传入的参数不对,会报TypeError

print('-100的绝对值--',abs(-100))

执行结果
在这里插入图片描述

2…max():查看最大值

print('最大值为---',max(1,2,9))

执行结果
在这里插入图片描述

3.数据类型转换

#int():把其他类型转换为int
i_2_s=int('111')
print('将str转换为int--',i_2_s)

#float():把其他类型转换为float

i_2_f=float('12.3')
print('字符串转为浮点数---',i_2_f)


#str():把其他类型转换为字符串
b_2_s=str(True)

print('将布尔转为str---',b_2_s)

i_2_s=str(109)

print('将int转换为str---',i_2_s)


#bool():将其他类型转为布尔

s_2_b = bool('1')

s_2_b2 = bool('2')

print('str转为布尔--',s_2_b,s_2_b2)

i_2_b = bool(2)

i_2_b2 = bool(3)

print('int转为bool--',i_2_b,i_2_b2)

执行结果
在这里插入图片描述

疑问:为什么str和int转为bool不管数值是多少,都是True呢,经过百度,原来在python中,除了‘’、""、0、()、[]、{}、None为False, 其他转换都为True,即只要不为空,则永远转换为True。
更多内置函数的学习,可查看官网 https://docs.python.org/3/library/functions.html#abs

参考:1.https://blog.csdn.net/muzizongheng/article/details/9368409 2.https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014316784721058975e02b46cc45cb836bb0827607738d000

猜你喜欢

转载自blog.csdn.net/cdhahaha/article/details/83153492