zoukankan      html  css  js  c++  java
  • Python 基础入门 7_4 内置模块(Math模块以及随机数模块)

    #Number数据类型的数学功能,需要引入math包(即import math)
    """
    绝对值:abs(变量)
    取最大值: max(多个变量或常量,2个起,用逗号隔开)
    取最小值: min(多个变量或常量,2个起,用逗号隔开)
    求x的y次方: pow( 原数,指数)
    float四舍五入: round( float ,保留的小数【默认0】)
    向上取整 :math.ceil(float) 当小数大于0时,整数+1
    向下取整 :math.floor(float) 当小数小于等于9时,整数不变
    以floct数据类型返回整数部分及小数部分 : math.modf(float)
    算术平方根: math.sqrt(Num)
    """
    import math
    print(abs(-1) , max(1,3,4,5,7) , min(1,2,3,4,5,6,) , pow(5,2) ,round(53.45663,2) ,math.ceil(7.99),math.floor(7.999))
    print(math.modf(33.6),math.sqrt(4))

    """
    随机数生产的几种方式
    random.choice(list/"Str") 参数可以是列表也可以是字符串,列表里可以是数字和字符串组成
    random.choice(range(5)),rang(5)=[0,1,2,3,4]
    随机取开始值到结束值范围内中从开始值递增的值
    random.randrange([start],stop,[step]) start 开始值(包含),默认0 ;stop 结束值(不包含);step:指定的递增基数,默认1
    random.random 随机生成(0,1)的浮点数
    random.shuffle(list) 将list的所有元素随机排序
    random.uniform(num1,num2) 在[num1,num2]范围内随机生成一个实数
    """
    import random
    print(random.choice([1,"a"]))
    print(random.choice(range(7)))
    print(random.randrange(1,100,2))
    print(random.random())
    list = [1,2,5,5]
    random.shuffle(list) #使用该函数时,变量要定义好,并且不能直接放在print输出里
    print(list)
    print(random.uniform(4,6))
  • 相关阅读:
    13、文件修改及函数的基本使用
    12、文件处理 b模式
    作业3月16号
    作业3月13号
    11、文件处理 t模式
    10、数据类型内置之集合
    作业3月11号
    9、基础类型之列表、元组、字典
    作业3月10号
    8、for循环以及数字类型和字符串类型的内置方法
  • 原文地址:https://www.cnblogs.com/hjlin/p/10636751.html
Copyright © 2011-2022 走看看