zoukankan      html  css  js  c++  java
  • Python3数据类型之数字

    http://img2.mukewang.com/5dd91e090001391719201080.jpg

    1. Python数字类型的作用

       Python数字类型用来存储数值,它是不可变对象,一旦定义之后,其值不可以被修改。如果改变了数字类型的值,就要重新为其分配内存空间。

       定义一个数字类型的变量:a = 100, 变量a就存储了100这个数值

    2. Python支持三种不同的数值类型

       (1)整型(int):Python整型数据,包含正整数和负整数。在Python3中,整型是没有限制大小的,也没有Python2中的Long类型。 同时可以使用十六进制(0x)或八进制(0o)来表示一个整数。

       (2)浮点型(float): Python浮点型由整数部分和小数部分组成,也可以使用科学计数法表示,比如:3.14e+10, 60.99E-5

       (3)复数(complex): 复数由实数部分和虚数部分组成,可以使用a + bj或者complex(a, b)来表示,其中a是实数部分,b是虚数部分。复数的实部a和虚部b都是浮点型。

    3. Python数字类型转换

       Python数字类型转换就是将一个数据内置的类型进行转换,而数据类型的转换,只需要将数据类型作为函数名即可。

       数字类型转换的函数方法如下:

       int(x): 将x转换为一个整数;

       float(x): 将x转换为一个浮点数

       complex(x): 将x转换为一个复数,其中实部为x,虚部为0;

       complex(x, y): 将x和y转换为一个复数,实部为x,虚部为y,x和y是数字表达式。

    4. Python数字元素

       (1)最常见的就是数字之间的加减乘除运算,与其他语言比如Java,C一样。

       (2)Python的浮点数运算在不同的机器上可能有不一样的结果。

       (3)Python的整数除法(/)总是会返回一个浮点型数值结果;而取整除法(//)总是返回向下取整的整数结果值。取整除法得到的并不一定是整数类型的数,它与参与运算的分母分子的数据类型有关。

       (4)不同类型的数进行混合运算时,会将整数转换为浮点数。

    5. 数学常量:

       (1)pi: 数学常量 pi(圆周率,一般以π来表示)

       (2)e: 数学常量 e,e即自然常数(自然常数)。

    示例代码:

    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
    28
    29
    30
    31
    32
    33
    34
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # author : liuhefei
    # Time : 2019/11/7
    # desc:  Python数据类型之数字
     
    if __name__ == "__main__":
        = 24  # 正整数
        = -4  # 负整数
        pi = 3.14 # 浮点数
        num1 = 0xA8F  # 十六进制整数
        num2 = 0o37 # 八进制整数
        num3 = 32.3e+10  # 科学计数法浮点数
        num4 = -67.34e20 # 浮点数
        com1 = 45j  # 复数,只有实部
        com2 = 3j+7 # 复数,含有实部和虚部
     
        # 数字运行
        = 20
        = 3
        = 2.3
        print("1 - ", x+y)  # 23
        print("2 - ", x-y)  # 17
        print("3 - ", x*y)  # 60
        print("4 - ", x/y)  # 6.666666666666667
        print("5 - ", x//y) # 6
        print("6 - ", x**y) # 8000
        print("7 - ", x%y)  # 2
        print("8 - "int(z)) # 2
        print("9 - ", x*y/z) # 26.086956521739133
     
        # 复数运算
        print("10 - ", com1 + com2)  # (7+48j)
        print("11 - ", com1 - com2)  # (-7+42j)

    http://img2.mukewang.com/5dd920cc000148b708680486.jpg

    数学函数:

    函数     返回值 ( 描述 )

    abs(x) 返回数字的绝对值,如abs(-10) 返回 10。

    max(x1, x2,...) 返回给定参数的最大值,参数可以为序列。

    min(x1, x2,...) 返回给定参数的最小值,参数可以为序列。

    math.ceil(x) 返回数字的上入整数,如math.ceil(4.1) 返回 5。

    math.exp(x) 返回e的x次幂(ex),如math.exp(1) 返回2.718281828459045。

    math.fabs(x) 返回数字的绝对值,如math.fabs(-10) 返回10.0。

    math.floor(x) 返回数字的下舍整数,如math.floor(4.9)返回 4。

    math.log(x) 用于获取x的自然对数,其中x > 0。如math.log(math.e)返回1.0,math.log(100,10)返回2.0。

    math.log(x, y)  用于获取x,y的对数, 其中x,y > 0,其中x是指数,y是底数。

    math.log10(x) 返回以10为基数的x的对数,其中x > 0,如math.log10(100)返回 2.0。

    math.modf(x) 返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。

    pow(x, y) 返回x的y次方的值, 其中y>0  (内置函数)。

    pow(x, y, z) 计算x的y次方,其中y>0(内置函数),再对结果进行z取模,其结果等效于pow(x,y) %z。

    math.pow(x, y) 返回x的y次方的值, 其中y>0。

    round(x [,n]) 返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数。

    math.sqrt(x) 返回数字x的平方根。

     前进轮胎

    随机数函数

    随机数可以用于数学,游戏,安全等领域中,还经常被嵌入到算法中,用以提高算法效率,并提高程序的安全性。

    Python包含以下常用随机数函数:

    函数         描述

    random.choice(seq) 从序列的元素中随机挑选一个元素,比如random.choice(range(10)),从0到9中随机挑选一个整数。

    random.randrange ([start,] stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1。

    random.random() 随机生成下一个实数,它在[0,1)范围内。

    random.seed([x]) 改变随机数生成器的种子seed。如果你不了解其原理,你不必特别去设定seed,Python会帮你选择seed。

    random.shuffle(lst) 将序列的所有元素随机排序。

    random.uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。

    三角函数

    Python包括以下三角函数:

    函数     描述

    math.acos(x) 返回x的反余弦弧度值。 x -- -1到1之间的数值。如果x是大于1,会产生一个错误。

    math.asin(x) 返回x的反正弦弧度值。 x -- -1到1之间的数值。如果x是大于1,会产生一个错误。

    math.atan(x) 返回x的反正切弧度值。 x -- -1到1之间的数值。如果x是大于1,会产生一个错误。

    math.atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。

    math.cos(x) 返回x的弧度的余弦值。

    math.hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。

    math.sin(x) 返回的x弧度的正弦值。

    math.tan(x) 返回x弧度的正切值。

    math.degrees(x) 将弧度转换为角度,如degrees(math.pi/2) , 返回90.0。

    math.radians(x) 将角度转换为弧度。

    代码示例:

    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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    # author : liuhefei
    # Time : 2019/11/9
    # desc:  Python数字函数
     
    import math
    import random
     
    if __name__ == "__main__":
        # 数学函数
        # 1. abs(x)    返回数字x的绝对值,如abs(-10) 返回 10 (内置函数)
        #  abs() 还可以运用在复数中。
        print("abs(-100) = "abs(-100))
        print("abs(-3.1415) =  "abs(-3.1415))
        print("abs(-4j-6) = "abs(-4j-6))
     
        # 2. ceil(x) 返回数字x的上入整数(向上取整),如math.ceil(4.1) 返回 5
        print("math.ceil(4.1) =  ", math.ceil(4.1))
        print("math.ceil(-4.6) = ", math.ceil(-4.6))
        print("math.ceil(100.12) = ", math.ceil(100.12))
        print("math.ceil(100.72) = ", math.ceil(100.72))
     
        # 3. exp(x)    返回e的x次幂(e^x),其中e是自然常数,如math.exp(1) 返回2.718281828459045
        print("math.exp(1) = ", math.exp(1))
        print("math.exp(10) = ", math.exp(10))
        print("math.exp(-100.25) = ", math.exp(-100.25))
     
        # 4. fabs(x)返回数字x的绝对值,如math.fabs(-10) 返回10.0
        # fabs() 函数只对浮点型跟整型数值有效。
        print("math.fabs(-23.35) = ", math.fabs(-23.35))
        print("math.fabs(-123) = ", math.fabs(-123))
     
        # 5. floor(x)返回数字的下舍整数(向下取整),其结果值小于或等于x。如math.floor(4.9)返回4
        print("math.floor(4.9) = ", math.floor(4.9))
        print("math.floor(4.1) = ", math.floor(4.1))
        print("math.floor(-78.18) = ", math.floor(-78.18))
     
        # 6. log(x)用于获取x的自然对数,x > 0。如math.log(math.e)返回1.0,math.log(100,10)返回2.0
        print("math.log(math.e) = ", math.log(math.e))
        print("math.log(10) = ", math.log(10))
     
        # log(x, y)  用于获取x,y的对数,其中x是指数,y是底数
        print("math.log(100, 10) = ", math.log(10010))  # 指数是底数的2次方
        print("math.log(10, 10) =  ", math.log(1010))   # 指数与底数相等
     
        # 7. log10(x)  返回以10为基数的x的对数,其中x > 0,如math.log10(100)返回 2.0
        print("math.log10(100) = ", math.log10(100))  # 底数为10,指数为100
        print("math.log10(25.88) = ", math.log10(25.88))
     
        # 8. max(x1, x2,...)   返回给定参数的最大值,参数可以为序列。(内置函数)
        print("max(1, 0, -9, 23, 4, 7, -99, 8) = "max(10-92347-998))
        print("max(-9, -8, -7, -3) = "max(-9-8-7-3))
     
        # 9. min(x1, x2,...)   返回给定参数的最小值,参数可以为序列。(内置函数)
        print("min(-99, 0, -87, -20, -45, 100) = "min(-990-87-20-45100))
        print("min(10, 30, 60, 90) = "min(10306090))
     
        # 10. modf(x)  返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示。
        print("math.modf(100.88) = ", math.modf(100.88))
        print("math.modf(-100.12) = ", math.modf(-100.12))
     
        # 11. pow(x, y)    返回x的y次方的值, 其中y>0  (内置函数)
        print("pow(2, 4) = "pow(24))
        print("pow(-2, 3) = "pow(-23))
     
        # pow(x, y, z) 计算x的y次方,再对结果进行z取模,其结果等效于pow(x,y) %z
        print("pow(2, 4, 2) = "pow(242))
        print("pow(4, 2, 3) = "pow(423))
     
        # 12. math.pow(x, y)内置方法会把参数作为整型,而math模块则会把参数转换为float。
        print("math.pow(4, 5) = ", math.pow(45))
        print("math.pow(-6, 2) = ", math.pow(-62))
     
        # 13.round(x [,n])返回浮点数x的四舍五入值,如给出n值,则代表舍入到小数点后的位数(内置函数)
        print("round(50.23456) = "round(50.23456))
        print("round(66.659, 1) = "round(66.6591))
        print("round(80.264, 2) ="round(80.2642))
        print("round(100.000056, 3) = "round(100.0000563))
        print("round(-100.000056, 3) = "round(-100.0000563))
     
        # 13. sqrt(x)  返回数字x的平方根
        print("math.sqrt(100) = ", math.sqrt(100))
        print("math.sqrt(10) = ", math.sqrt(10))
        print(" ")
     
        # 随机函数
        # 1. random.choice(seq) 返回一个列表,元组或字符串的随机项。 
        # 比如random.choice(range(10)),从0到9中随机挑选一个整数返回。
        print("random.choice(range(10)) = ", random.choice(range(10)))
        print("random.choice([2, 7, 9, 0, 1, 4, 3, 6, 5]) = "
               random.choice([279014365]))
        print("random.choice string = "
               random.choice("天国虽美,没有了你,万杯觥筹,只不过是提醒寂寞罢了"))
        print("random.choice tuple = ", random.choice(("你好""很好笑""好开心""好难过")))
     
        # 2. random.randrange ([start,] stop [,step]) 
        # 从指定范围内,按指定基数递增的集合中获取一个随机数,基数默认值为 1
        # 参数: start -- 指定范围内的开始值,包含在范围内。
        # stop -- 指定范围内的结束值,不包含在范围内。
        # step -- 指定递增基数。
        #  从 1-100 中选取一个奇数
        print("random.randrange(1,100, 2) = ", random.randrange(11002))  
        print("random.randrange(100) = ", random.randrange(100))  # 从 0-99 选取一个随机数
        # 返回100到1000之间的随机数
        print("random.randrange(100, 1000) = ", random.randrange(1001000))  
     
        # 3. random.random()   随机生成下一个实数,它在[0,1)范围内。
        print("random.random() : ", random.random())
     
        # 4.  random.seed([x]) 改变随机数生成器的种子seed。x -- 改变随机数生成器的种子seed。
        # 其中的 x 可以是任意数字,如10,这个时候,先调用它的情况下,
        # 使用 random() 生成的随机数将会是同一个。
        random.seed()
        print("使用默认种子生成随机数:", random.random())
        print("使用默认种子生成随机数:", random.random())
     
        random.seed(10)
        print("使用整数 10 种子生成随机数:", random.random())
        random.seed(100)
        print("使用整数 100 种子生成随机数:", random.random())
     
        random.seed("HelloWorld"2)
        print("使用字符串种子生成随机数:", random.random())
     
        # 5. random.shuffle(lst)   将序列的所有元素随机排序
        print("随机排序:", random.shuffle([097145623]))
        print("随机排序:", random.shuffle([097145623]))
     
        # 6. uniform(x, y) 随机生成下一个实数,它在[x,y]范围内。
        print("random.uniform(10, 100) = " , random.uniform(10100))
        print("random.uniform(-10, 10) = ", random.uniform(-1010))
        print(" ")
     
     
        # 三角函数
        # 1. math.sin(x) 返回的x弧度的正弦值。数值在 -1 到 1 之间。x是一个数值。
        print("math.sin(30) = ", math.sin(30))
        print("math.sin(-30) = ", math.sin(-30))
        print("math.sin(0) = ", math.sin(0))
        print("math.sin(math.pi) = ", math.sin(math.pi))
        print("math.sin(math.pi/2) = ", math.sin(math.pi / 2))
     
        # 2. math.asin(x) 返回x的反正弦弧度值。x -- -1到1之间的数值。如果x是大于1,会产生一个错误。
        print("math.asin(0.64) = ", math.asin(0.64))
        print("math.asin(0) = ", math.asin(0))
        print("math.asin(-1) = ", math.asin(-1))
        print("math.asin(1) = ", math.asin(1))
     
        # 3. math.cos(x) 返回x的弧度的余弦值。-1 到 1 之间, x是一个数值
        print("math.cos(30) = ", math.cos(30))
        print("math.cos(45) = ", math.cos(45))
        print("math.cos(60) = ", math.cos(60))
        print("math.cos(90) = ", math.cos(90))
        print("math.cos(math.pi) = ", math.cos(math.pi))
        print("math.cos(2*math.pi) = ", math.cos(2 * math.pi))
        print("math.cos(0) = ", math.cos(0))
        print("math.cos(-90) = ", math.cos(-90))
        print("math.cos(-60) = ", math.cos(-60))
     
        # 4. math.acos(x)返回x的反余弦弧度值。x -- -1到1之间的数值。如果x是大于1,会产生一个错误。
        print("math.acos(0.64) = ", math.acos(0.64))
        print("math.acos(0) = ", math.acos(0))
        print("math.acos(-1) = ", math.acos(-1))
        print("math.acos(1) = ", math.acos(1))
     
        # 5. math.tan(x) 返回x弧度的正切值。数值在 -1 到 1 之间。x是一个数值。
        print("math.tan(30) = ", math.tan(30))
        print("math.tan(-30) = ", math.tan(-30))
        print("math.tan(0) = ", math.tan(0))
        print("math.tan(math.pi) = ", math.tan(math.pi))
        print("math.tan(math.pi/2) = ", math.tan(math.pi / 2))
        print("math.tan(math.pi/4) = ", math.tan(math.pi / 4))
     
        # 6. math.atan(x) 返回x的反正切弧度值。x -- 一个数值。
        print("math.atan(0.64) = ", math.atan(0.64))
        print("math.atan(0) = ", math.atan(0))
        print("math.atan(10) = ", math.atan(10))
        print("math.atan(-60) = ", math.atan(-60))
        print("math.atan(-1) = ", math.atan(-1))
        print("math.atan(1) = ", math.atan(1))
     
        # 7. math.atan2(y, x) 返回给定的 X 及 Y 坐标值的反正切值。x,y -- 一个数值。
        print("math.atan2(-0.50,-0.50) = ", math.atan2(-0.50-0.50))
        print("math.atan2(0.50,0.50) = ", math.atan2(0.500.50))
        print("math.atan2(5,5) = ", math.atan2(55))
        print("math.atan2(-10,10) = ", math.atan2(-1010))
        print("math.atan2(10,20) = ", math.atan2(1020))
     
        # 8. math.hypot(x, y) 返回欧几里德范数 sqrt(x*x + y*y)。x,y -- 一个数值。
        print("math.hypot(3, 2) = ", math.hypot(32))
        print("math.hypot(-3, 6) = ", math.hypot(-36))
        print("math.hypot(0, 4) = ", math.hypot(04))
     
        # 9. math.degrees(x)将弧度转换为角度,返回一个角度值。如degrees(math.pi/2) , 返回90.0
        print("math.degrees(30) = ", math.degrees(30))
        print("math.degrees(-30) = ", math.degrees(-30))
        print("math.degrees(0) = ", math.degrees(0))
        print("math.degrees(math.pi) = ", math.degrees(math.pi))
        print("math.degrees(math.pi/2) = ", math.degrees(math.pi / 2))
        print("math.degrees(math.pi/4) = ", math.degrees(math.pi / 4))
     
        # 10. math.radians(x)  将角度转换为弧度,返回一个角度的弧度值。x是一个数值
        print("radians(30) = ", math.radians(30))
        print("radians(-30) = ", math.radians(-30))
        print("radians(0) = ", math.radians(0))
        print("radians(math.pi) = ", math.radians(math.pi))
        print("radians(math.pi/2) = ", math.radians(math.pi / 2))
        print("radians(math.pi/4) = ", math.radians(math.pi / 4))

    来源于:https://mp.weixin.qq.com/s/9NNN_ahgHC6V5h6ZoEhCzg

  • 相关阅读:
    jquery 左右滚动插件
    MagicAjax使用及注意事项~!!!AJAX无刷新的DLL文件!!
    android 中播放声音要注意的细节
    JNIEnv*的常用函数详解
    http协议上传数据的 优化 方法
    jni 设计的哲学
    listview动态加载数据,并更新数据列表
    Android HOME键的屏蔽
    Django在pycharm业余版进行创建项目文件
    SQL Server 日常维护查询当前正在执行的语句、死锁、堵塞
  • 原文地址:https://www.cnblogs.com/xyou/p/11933479.html
Copyright © 2011-2022 走看看