python常用语法3
2021/8/1 14:06:12
本文主要是介绍python常用语法3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python语法帮助文档
Python Cookbook 3rd Edition Documentation
1. 生成随机数
import random var = random.randint(0, 9) print("%s ==> %d" % (type(var), var)) # <class 'int'> ==> 4
参考:Python 随机数生成
1.1 产生一个上下界之间的随机浮点数
random.uniform(0.1, 0.4)
2. Decimal类型导入方式和创建对象方式
decimal是数据库和python的十进制浮点类型
from decimal import * >>> getcontext().prec = 6 >>> Decimal(1) / Decimal(7) Decimal('0.142857') >>> getcontext().prec = 28 >>> Decimal(1) / Decimal(7) Decimal('0.1428571428571428571428571429')
参考: https://docs.python.org/zh-cn/3/library/decimal.html
DateAndDecimalEncoder
只有默认序列化器无法序列化时才会采用这个自定义的序列化器
参考: json--- JSON 编码和解码器、JSON编码/自定义对象的解码
3. python计算MD5值
import hashlib deomo_val = 'Follow me,like and comment anything on this video..just say hi
这篇关于python常用语法3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python