大家好,本文将围绕python中3//5等于多少展开说明,python中3&6的值为是一个很多人都想弄明白的事情,想搞清楚python中【6,0】需要先了解以下几个事情。
文章目录
repr 函数
功能:repr() 函数将对象转化为供解释器读取的形式
语法:repr(object) object 对象
返回值:返回一个对象的 string 格式
实例:
s = 'RUNOOB'
res1 = repr(s)
print(type(res1),res1) # <class 'str'> 'RUNOOB'
dict = {
'runoob': 'runoob.com', 'google': 'google.com'}
res2 = repr(dict)
print(type(res2),res2) # <class 'str'> {'google': 'google.com', 'runoob': 'runoob.com'}
reverse 方法
功能:reverse() 函数用于反向列表中元素
语法:list.reverse()
返回值:无
实例:
aList = [123, 'xyz', 'zara', 'abc', 'xyz']
aList.reverse()
print("List : ", aList) # List : ['xyz', 'abc', 'zara', 'xyz', 123]
round 函数
功能:round() 方法返回浮点数x的四舍六入值
语法:round( x [, n])
参数:
x - - 数值表达式
n - - 数值表达式,表示从小数点位数
返回值:返回浮点数 x 的四舍六入值
实例:
print("round(80.23456, 2) : ", round(80.23456, 2))
print("round(100.000056, 3) : ", round(100.000056, 3))
print("round(-100.000056, 3) : ", round(-100.000056, 3))
注意:round 函数是有一些坑的
round 的返回值很 python 版本有关系
例如 python 2.7 中 round 保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1
但是在python 3.5 中 round 值,如果距离两边一样远,会保留到偶数的一边python基础知识点精心整理。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2
print("round(2.355,2):",round(2.355,2))#2.35
print(round(2.675,2)) #2.67
总之尽量避免使用 round
还有其他选择
- 使用math模块中的一些函数,比如math.ceiling(天花板除法)
- python自带整除,python2中是/,3中是//,还有div函数
- 字符串格式化可以做截断使用,例如 “%.2f” % value(保留两位小数并变成字符串……如果还想用浮点数请披上float()的外衣)
- 对浮点数精度要求如果很高的话,用 decimal 模块
set 函数
功能:创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等
语法:class set([iterable]) iterable - - - 可迭代对象对象
返回值:返回新的集合对象
实例:
>>> x = set('eleven')
>>> y = set('twelve')
>>> x,y
({
'l', 'e', 'n', 'v'}, {
'e', 'v', 'l', 't', 'w'})
>>> x & y #交集
{
'l', 'e', 'v'}
>>> x | y #并集
{
'e', 'v', 'n', 'l', 't', 'w'}
>>> x - y #差集
{
'n'}
>>> y -x #差集
{