【Python】round方法、random模块

round方法

对浮点数后的小数位数进行处理,第一个参数是一个浮点数,第二个参数是保留的小数位数,可选,如果不写的话默认保留到整数。

BMI=20.957438
BMI=round(BMI,1)
print(BMI)

 运行结果:

注意:round的结果跟python版本有关

阅读一下python的文档,里面是这么写的:

在python2.7的doc中,round()的最后写着,"Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0." 保留值将保留到离上一位更近的一端(四舍六入),如果距离两端一样远,则保留到离0远的一边。所以round(0.5)会近似到1,而round(-0.5)会近似到-1。

但是到了python3.5的doc中,文档变成了"values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice." 如果距离两边一样远,会保留到偶数的一边。比如round(0.5)和round(-0.5)都会保留到0,而round(1.5)会保留到2。

所以如果有项目是从py2迁移到py3的,可要注意一下round的地方(当然,还要注意/和//,还有print,还有一些比较另类的库)。

除非对精确度没什么要求,否则尽量避开用round()函数。近似计算我们还有其他的选择:

  1. 使用math模块中的一些函数,比如math.ceiling(天花板除法)。
  2. python自带整除,python2中是/,3中是//,还有div函数。
  3. 字符串格式化可以做截断使用,例如 "%.2f" % value(保留两位小数并变成字符串……如果还想用浮点数请披上float()的外衣)。
  4. 当然,对浮点数精度要求如果很高的话,请用decimal模块。 

来源:https://www.cnblogs.com/anpengapple/p/6507271.html

  • 在python2中使用round模块,直接四舍五入
     
    >>> round(2.675, 2)
    2.67
  • 在python3中使用round模块,四舍六入,‘5’的情况下,保留最后一位是奇数,则进位,保留最后一位是偶数,就舍去

    print(round(2.685,2))
    print(round(2.695,2))

random模块

和turtle库一样,是python中的内置模块,使用时需要导入:import random

random方法:

1.random.random()  #产生一个0-1之间的随机浮点数,下限必须小于上限,适用下面所有的。

import random
print(random.random())

>>>0.7755621296778215

2.random.randint(1,10)  #产生一个指定区间内的随机整数,包含首尾

import random
print(random.randint(1,10))

>>>9

3.random.uniform(1.1,5.4) )   #产生  一个1.1 到 5.4 之间的随机浮点数,区间可以不是整数,包含首尾

import random
print(random.uniform(1.1,5.4))

>>>2.9536842271871393

4.random.choice() # 从序列中随机选取一个元素

import random
tomorrow=['周一','周二','周三','周四','周五','周六','周日',]
 # 从序列中随机选取一个元素
print(random.choice(tomorrow)) # 列表中的随机元素
print(random.choice('tomorrow()')) #字符串中的随机字符
 
>>>周一
(

5.random.randrange(x,y,z )   # 生成从x到y的间隔为z的随机整数

import random
print(random.randrange(1,100,2))  # 生成从1到100的间隔为2的随机整数,都是奇数
print(random.randrange(0,101,2))  # 生成从0到101的间隔为2的随机整数,都是偶数

>>>51
48

6.random.shuffle(a)  #将列表a中的元素顺序打乱

import random
a=[1,2,3,4,5]
b=['1','2','3','4','5']
random.shuffle(a)
print(a)
random.shuffle(b)
print(b)

>>>[5, 1, 3, 4, 2]
['4', '1', '2', '5', '3']

7.random.sample(str,x)  #多个字符中选取特定数量的字符,str可以是列表或字符串,x是选取字符的个数

import random
#多个字符中选取特定数量的字符
print(random.sample(['a','b','c','d','e','f','123'],4))
print(random.sample('abcdef123',4))

>>>['a', '123', 'b', 'f']
['1', 'f', 'b', 'd']

8.多个字符中选取特定数量的字符组成新的字符串

import random
#多个字符中选取特定数量的字符组成新的字符串
#源代码str1=string.join(random.sample(['a','b','c','d','e','h','i'],2))
#出现报错信息module 'string' has no attribute 'join'
'''
有关.join方法如何工作的简单示例:
", ".join(['a','b','c'])
将为您提供字母ab和c的str对象,并用逗号和空格分隔。
'''
str1=' '.join(random.sample(['a','b','c','d','e','h','i'],5))
print(str1)
str2=''.join(random.sample(['a','b','c','d','e','h','i'],5))
print(str2)

>>>b c e d a
bidhc
发布了57 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/IGGIRing/article/details/105253396
今日推荐