今天咱们来简单分享一下关于 python 中的一种数据类型和操作方法. 费话不多说哈, 咱们直接来进行实践加理论. 首先, 我们要知道, Python Number 数据类型用于存储数. 数据类型是不允许改变的, 这就意味着如果改变 Number 数据类型的值, 将重新分配内存空间. 下面的实例在变量赋值时 Number 对象将被创建:
var1 = 1 var2 = 10
我们同样可以使用 del 语句删除一些 Number 对象引用. 语法格式为:
del var1[,var2[,var3[....,varN]]]]
我们可以通过使用 del 语句删除单个或多个对象, 例如:
- del var
- del var_a, var_b
我们得了解 Python 支持四种不同的数值类型:
整型(Int) - 通常被称为是整型或整数, 是正或负整数, 不带小数点.
长整型(long integers) - 无限大小的整数, 整数最后是一个大写或小写的 L.
浮点型(floating point real values) - 浮点型由整数部分与小数部分组成, 浮点型也可以使用科学计数法表示(2.5e2 = 2.5 x 102 = 250)
复数 (complex numbers) - 复数由实数部分和虚数部分构成, 可以用 a + bj, 或者 complex(a,b) 表示, 复数的实部 a 和虚部 b 都是浮点型.
下面是本人整理的一个表格:
int | long | float | complex |
---|---|---|---|
10 | 51924361L | 0.0 | 3.14j |
100 | -0x19323L | 15.20 | 45.j |
-786 | 0122L | -21.9 | 9.322e-36j |
080 | 0xDEFABCECBDAECBFBAEl | 32.3+e18 | .876j |
-0490 | 535633629843L | -90. | -.6545+0J |
-0x260 | -052318172735L | -32.54e100 | 3e+26J |
0x69 | -4721885298529L | 70.2-E12 | 4.53e-7j |
长整型也可以使用小写 "L", 但是还是建议您使用大写 "L", 避免与数字 "1" 混淆. Python 使用 "L" 来显示长整型.
Python 还支持复数, 复数由实数部分和虚数部分构成, 可以用 a + bj, 或者 complex(a,b)表示, 复数的实部 a 和虚部 b 都是浮点型
我们再来看一下 python Number 的类型转换:
int(x [,base ]) 将 x 转换为一个整数
long(x [,base ]) 将 x 转换为一个长整数
float(x ) 将 x 转换到一个浮点数
complex(real [,imag ]) 创建一个复数
str(x ) 将对象 x 转换为字符串
repr(x ) 将对象 x 转换为表达式字符串
eval(str ) 用来计算在字符串中的有效 Python 表达式, 并返回一个对象
tuple(s ) 将序列 s 转换为一个元组
list(s ) 将序列 s 转换为一个列表
chr(x ) 将一个整数转换为一个字符
unichr(x ) 将一个整数转换为 Unicode 字符
ord(x ) 将一个字符转换为它的整数值
hex(x ) 将一个整数转换为一个十六进制字符串
oct(x ) 将一个整数转换为一个八进制字符串
再来看一下 python 的 math 模块, cmath 模块. Python 中数学运算常用的函数基本都在 math 模块, cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致, 区别是 cmath 模块运算的是复数, math 模块运算的是数学运算. 要使用 math 或 cmath 函数必须先导入, 如下:
import math
我们来查看 math 查看包中的内容:
- >>> import math
- >>> dir(math)
- ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']
- >>>
下文会介绍各个函数的具体应用, 我们再来看一下 cmath 查看包中的内容:
- >>> import cmath
- >>> dir(cmath)
- ['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'cos', 'cosh', 'e', 'exp', 'inf', 'infj', 'isclose', 'isfinite', 'isinf', 'isnan', 'log', 'log10', 'nan', 'nanj', 'phase', 'pi', 'polar', 'rect', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau']
- >>>
来看一个实例:
- >>> import cmath
- >>> cmath.sqrt(-1)
- 1j
- >>> cmath.sqrt(9)
- (3+0j)
- >>> cmath.sin(1)
- (0.8414709848078965+0j)
- >>> cmath.log10(100)
- (2+0j)
- >>>
好啦, 我们来看一下, python 中的一些函数, 首先是数学函数:
再来看一下随机函数, 随机数可以用于数学, 游戏, 安全等领域中, 还经常被嵌入到算法中, 用以提高算法效率, 并提高程序的安全性. Python 包含以下常用随机数函数:
函数 | 描述 |
---|---|
choice(seq) http://www.runoob.com/python/func-number-choice.html | 从序列的元素中随机挑选一个元素,比如 random.choice(range(10)),从 0 到 9 中随机挑选一个整数。 |
randrange ([start,] stop [,step]) http://www.runoob.com/python/func-number-randrange.html | 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为 1 |
random() http://www.runoob.com/python/func-number-random.html | 随机生成下一个实数,它在 [0,1) 范围内。 |
seed([x]) http://www.runoob.com/python/func-number-seed.html | 改变随机数生成器的种子 seed。如果你不了解其原理,你不必特别去设定 seed,Python 会帮你选择 seed。 |
shuffle(lst) http://www.runoob.com/python/func-number-shuffle.html | 将序列的所有元素随机排序 |
uniform(x, y) http://www.runoob.com/python/func-number-uniform.html | 随机生成下一个实数,它在 [x,y] 范围内。 |
再来看一下三角函数:
函数 | 描述 |
---|---|
acos(x) http://www.runoob.com/python/func-number-acos.html | 返回 x 的反余弦弧度值。 |
asin(x) http://www.runoob.com/python/func-number-asin.html | 返回 x 的反正弦弧度值。 |
atan(x) http://www.runoob.com/python/func-number-atan.html | 返回 x 的反正切弧度值。 |
atan2(y, x) http://www.runoob.com/python/func-number-atan2.html | 返回给定的 X 及 Y 坐标值的反正切值。 |
cos(x) http://www.runoob.com/python/func-number-cos.html | 返回 x 的弧度的余弦值。 |
hypot(x, y) http://www.runoob.com/python/func-number-hypot.html | 返回欧几里德范数 sqrt(x*x + y*y)。 |
sin(x) http://www.runoob.com/python/func-number-sin.html | 返回的 x 弧度的正弦值。 |
tan(x) http://www.runoob.com/python/func-number-tan.html | 返回 x 弧度的正切值。 |
degrees(x) http://www.runoob.com/python/func-number-degrees.html | 将弧度转换为角度, 如 degrees(math.pi/2) , 返回 90.0 |
radians(x) http://www.runoob.com/python/func-number-radians.html | 将角度转换为弧度 |
最后就是数学常量:
常量 | 描述 |
---|---|
pi | 数学常量 pi(圆周率,一般以π来表示) |
e | 数学常量 e,e 即自然常数(自然常数)。 |
好啦, 今天就到这里了, 如果感觉不错的话, 请多多点赞支持哦...
来源: https://blog.csdn.net/luyaran/article/details/79900632