Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon
    • 1 Python 简介

      • 1.1 初识Python
        • 1.2 Python 代码规范
          • 1.3 Python 虚拟环境
            • 1.4 使用 vscode 打造 python 开发环境
              • 1.5 pypi 配置国内镜像
              • 2 Python 基础

                • 2.1 Python基础语法
                  • 2.2 程序控制
                    • 2.3 Python数据类型

                      • 2.3.1 数值型
                        • 整数
                          • 常用数值处理函数
                            • 随机数
                            • 2.3.2 字符串 str
                              • 2.3.3 字节序列
                                • 2.3.4 列表 list & 元组 tuple
                                  • 2.3.5 集合 set & 字典 dict
                                • 3 高级特性

                                  • 3.1 线性结构特征 可迭代 & 切片
                                    • 3.2 列表、集合、字典解析式
                                      • 3.3 生成器
                                        • 3.4 迭代器
                                        • 4 函数

                                          • 4.1 函数的定义 & 调用 & 返回值
                                            • 4.2 函数参数
                                              • 4.3 作用域
                                                • 4.4 递归函数
                                                • 5 函数式编程

                                                  • 5.1 高阶函数
                                                    • 5.2 返回函数
                                                      • 5.3 匿名函数
                                                        • 5.4 装饰器
                                                          • 5.5 偏函数
                                                          • 6 模块

                                                            • 6.1 Python 模块常用的几种安装方式
                                                              • 6.2 Python 的 setup.py 详解
                                                              • 7 IO编程

                                                                • 7.1 操作文件和目录
                                                                  • 7.2 序列化和反序列化
                                                                  • 8 异常、调试和测试

                                                                    • 8.1 异常处理
                                                                    • 9 面向对象编程

                                                                      • 9.1 类、实例和封装
                                                                        • 9.2 访问控制和属性装饰器
                                                                          • 9.3 继承、多态和Mixin
                                                                          • 10 进程和线程

                                                                            • 10.1 多进程
                                                                              • 10.2 多线程
                                                                                • 10.2 线程同步
                                                                                • 11 网络编程

                                                                                  • 11.1 SocketServer
                                                                                    • 11.2 TCP 编程
                                                                                    • 11 魔术方法
                                                                                      • 17 IO 模型
                                                                                        • python 实际工作中的实例
                                                                                        • 前端学习笔记

                                                                                          2.3.1 数值型

                                                                                          author iconClaycalendar icon2021年6月8日category icon
                                                                                          • Python
                                                                                          timer icon大约 2 分钟

                                                                                          此页内容
                                                                                          • 整数
                                                                                          • 常用数值处理函数
                                                                                          • 随机数

                                                                                          # 2.3.1 数值型

                                                                                          • int、float、complex、bool 都是class,1、5.0、2+3j 都是对象即实例
                                                                                          • int:python3 的 int 就是长整型,且没有大小限制,受限于内存区域的大小
                                                                                          • float:由整数部分和小数部分组成。支持十进制和科学计数法表示。C 的双精度型实现
                                                                                          • complex:有实数和虚数部分组成,实数和虚数部分都是浮点数,3+4.2J
                                                                                          • bool:int 的子类,仅有2个实例 True、False 对应1和0,可以和整数直接运算

                                                                                          # 整数

                                                                                          math模块的 floor()、ceil() 函数;内建函数 int()、round();运算符 //

                                                                                          # 整除
                                                                                          print(3//2, 5//2, 7//2)
                                                                                          print(-3//2, -5//2, -7//2)
                                                                                          print(7//2, 7//-2, -7//2, -(7//2))
                                                                                          # int
                                                                                          print('int ------------')
                                                                                          print(int(1.4), int(1.5), int(1.6))
                                                                                          print(int(-1.4), int(-1.5), int(-1.6))
                                                                                          # ceil floor
                                                                                          print('ceil floor ------------')
                                                                                          import math
                                                                                          print(math.floor(2.5), math.floor(-2.5))
                                                                                          print(math.ceil(2.5), math.ceil(-2.5))
                                                                                          # round
                                                                                          print('round ------------')
                                                                                          print(round(1.4), round(-1.4), round(-1.6), round(1.6))
                                                                                          print(round(2.4), round(-2.4), round(2.6), round(2.6))
                                                                                          print('round .5 ---------')
                                                                                          print(round(0.5), round(1.5), round(2.5), round(3.5))
                                                                                          print(round(-0.5), round(-1.5), round(-2.5), round(-3.5))
                                                                                          
                                                                                          1
                                                                                          2
                                                                                          3
                                                                                          4
                                                                                          5
                                                                                          6
                                                                                          7
                                                                                          8
                                                                                          9
                                                                                          10
                                                                                          11
                                                                                          12
                                                                                          13
                                                                                          14
                                                                                          15
                                                                                          16
                                                                                          17
                                                                                          18
                                                                                          19
                                                                                          20
                                                                                          • round() 四舍六入五取偶
                                                                                          • math.floor() 向下取整
                                                                                          • math.ceil()向上取整
                                                                                          • int() 取整数部分
                                                                                          • // 整除且向下取整

                                                                                          # 常用数值处理函数

                                                                                          • min()、max()
                                                                                          • pow(x,y) 等于 x**y
                                                                                          • math.sqrt() 等于 x ** 0.5
                                                                                          • 进制函数,返回值是字符串
                                                                                            • bin()、oct()、hex()
                                                                                          • math模块
                                                                                            • math.pi π
                                                                                            • math.e 自如常数
                                                                                            • math模块中还有对数函数、三角函数等

                                                                                          # 随机数

                                                                                          random模块

                                                                                          • randint(a, b) 返回[a, b]之间的整数
                                                                                          • randrange ([start,] stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1。 random.randrange(1,7,2)
                                                                                          • choice(seq) 从非空序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中 随机挑选一个整数。random.choice([1,3,5,7])
                                                                                          • 3.6开始提供choices,一次从样本中随机选择几个,可重复选择,可以指定权重
                                                                                          • random.shuffle(list) ->None 就地打乱列表元素
                                                                                          • sample(population, k) 从样本空间或总体(序列或者集合类型)中随机取出k个不同的元素,返回 一个新的列表
                                                                                            • random.sample(['a', 'b', 'c', 'd'], 2)
                                                                                            • random.sample(['a', 'a'], 2)
                                                                                            • 每次从样本空间采样,在这一次中不可以重复抽取同一个元素
                                                                                          import random
                                                                                          for i in range(10):
                                                                                              print(random.randint(1, 5))
                                                                                          print('-' * 30)
                                                                                          
                                                                                          for i in range(10):
                                                                                          	print(random.randrange(1, 5))
                                                                                          print('-' * 30)
                                                                                          
                                                                                          x = [1, 2, 3, 4, 5]
                                                                                          for i in range(10):
                                                                                          	print(random.choice(x))
                                                                                          print('-' * 30)
                                                                                          
                                                                                          # 观察下面的0和1的比例
                                                                                          for i in range(10):
                                                                                          	print(random.choices([0, 1], k=6))
                                                                                          print('-' * 30)
                                                                                          
                                                                                          for i in range(10):
                                                                                          	print(random.choices([0, 1], [10, 1], k=6)) # 10比1权重
                                                                                          print('-' * 30)
                                                                                          
                                                                                          x = [1, 2, 3, 4, 5]
                                                                                          # 采样
                                                                                          for i in range(5):
                                                                                          	print(random.sample(x, 5)) # k能不能是6, 不能
                                                                                          
                                                                                          1
                                                                                          2
                                                                                          3
                                                                                          4
                                                                                          5
                                                                                          6
                                                                                          7
                                                                                          8
                                                                                          9
                                                                                          10
                                                                                          11
                                                                                          12
                                                                                          13
                                                                                          14
                                                                                          15
                                                                                          16
                                                                                          17
                                                                                          18
                                                                                          19
                                                                                          20
                                                                                          21
                                                                                          22
                                                                                          23
                                                                                          24
                                                                                          25
                                                                                          26
                                                                                          27
                                                                                          edit icon编辑此页open in new window
                                                                                          上次编辑于: 2021/6/8 09:28:23
                                                                                          贡献者: clay-wangzhi
                                                                                          下一页
                                                                                          2.3.2 字符串 str
                                                                                          备案号:冀ICP备2021007336号
                                                                                          Copyright © 2023 Clay