zoukankan      html  css  js  c++  java
  • python之Number

    1、Python number数字

    Python Number 数据类型用于存储数值。

    数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间。

    创建一个number数据值,改变数据的值,查看内存地址已发生改变:

    >>> num = 123
    >>> id(num)
    8743872
    >>> num = 456
    >>> id(num)
    13991590095640

    使用del语句删除number对象,可以删除多个用','逗号分隔:

    >>> num = 123
    >>> num1 =888
    >>> del num,num1

    2、python支持四种不同的数据类型:

    • 整形(int)-通常被称为整型或整数,是正数或者负整数。
    • 长整型(long integers)-无限大小的整数,整数最后使用大写或小写的L表示。
    • 浮点型(floating point real values)-浮点型由整数部分和小数部分组成。
    • 复数(complex numbers)-复数由实数部分和虚数部分构成,可以使用a+bj,或者complex(a,b)表示,复数的a和b部分都是浮点型.
    长整型的取值范围:
    python2.7版本中长整型的取值范围为-2**63-1次方至2**63次方
    python3中没有long类型,使用int表示长整型
    In [1]: 2**63-1
    Out[1]: 9223372036854775807L
    In [2]: lo1 = 9223372036854775807
    In [3]: type(lo1)
    Out[3]: int
    In [4]: lo2 = 9223372036854775808
    In [5]: type(lo2)
    Out[5]: long
    In [6]: log8 = -2**62
    In [7]: type(log8)
    Out[8]: int
    In [9]: log8 = -2**63-1
    In [10]: type(log8)
    Out[11]: long
    
    创建复数:
    >>> complex1 = 1.2+3.4j
    >>> type(complex1)
    <class 'complex'>
    >>> complex2 = complex(0.3,3.2)
    >>> print(complex1,complex2)
    (1.2+3.4j) (0.3+3.2j)

    3、python number类型转换

    内置的函数可以执行数据类型之间的转换。这些函数返回一个新的对象,表示转换的值

    >>> nu1 = 89
    >>> nu2 = float(nu1)  #转换浮点型
    >>> type(nu2)
    <class 'float'>
    >>> nu3 =complex(nu2)  #转复数
    >>> type(nu3)
    <class 'complex'>
    >>> print(nu3)
    (89+0j)
    >>> nu4 = int(nu2)  #转整数
    >>> type(nu4)
    <class 'int'>
    >>> nu5 =str(nu4) #转字符
    >>> type(nu5)
    <class 'str'>
    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 )                 将一个整数转换为一个八进制字符串

    4、python数字内置函数,数字处理模块math

    >>> import math   #数字处理模块
    >>> help(math.ceil)  #查看帮助
    >>> dir(math)   #打印所有方法
    ['__doc__', '__file__', '__loader__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
    
    >>> nu1 = 12.34
    >>> math.ceil(nu1)   #取上入整数
    13
    >>> math.exp(nu1)  #返回e的nu1次幂,e为定义的常量
    228661.9520568098
    >>> math.fabs(nu1) #返回绝对值
    12.34
    >>> math.floor(nu1) #返回数字的下舍整数部分
    12
    >>> math.modf(nu1) #返回小数部分与整数部分
    (0.33999999999999986, 12.0)
    >>> math.sqrt(nu1) #返回平方根
    3.5128336140500593
    >>> math.e    #模块定义的常量e
    2.718281828459045
    >>> math.pi   #模块定义的常量pi
    3.141592653589793
    内置函数:
    >>> abs(11.2)   #返回绝对值
    11.2
    >>> max(12,24)  #最大值
    24
    >>> min(12,24)  #最小值
    12
    >>> pow(2,4)   #2**4幂次方
    16
    >>> round(1.245,3)  #返回值的四舍五入值,3为定义到小数第几位
    1.245
    >>> round(1.245)   #默认为0
    1

    5、python随机数模块random

    >>> import random    #导入模块
    >>> random.random()   #获取0到1之间的随机数
    0.1781419039493949
    >>> random.random()
    0.914421842727102
    
    >>> random.uniform(10,20)  #生成10,20之间的浮点数
    19.774883012515218
    >>> random.uniform(10,20)
    11.654111952867027
    
    >>> random.randint(10,20)  #生成指定范围内的整数
    18
    >>> random.randint(10,20)
    11
    
    >>> random.randrange(1,100,8)  #从指定范围内按指定基数递增获取随机数
    33
    >>> random.randrange(1,100,8)
    17
    >>> random.randrange(1,100,8)
    33
    
    >>> random.choice([1,2,3,4,5]) #从序列元素中随机获取元素,只能是有序类型
    2
    >>> random.choice([1,2,3,4,5])
    1
    >>> random.choice([1,2,3,4,5])
    2
    >>> random.choice('abcd')
    'd'
    >>> random.choice('abcd')
    'a'
    >>> random.choice('abcd')
    'c'
    
    >>> a = [1,2,3,4,5]  #将一个列表元素打乱
    >>> random.shuffle(a)
    >>> a
    [1, 2, 3, 5, 4]
    >>> random.shuffle(a)
    >>> a
    [2, 5, 4, 3, 1]
    >>> random.sample(a,2)  #从指定序列中随机获取N个元素,生成新对象
    [5, 2]
    >>> random.sample(a,2)
    [5, 4]
    >>> random.sample(a,3)
    [3, 4, 1]
    >>> random.sample(a,5)
    [5, 1, 4, 2, 3]

    2017-09-2412:27:23™

  • 相关阅读:
    运算符重载
    简单函数template max
    const static extern
    python 关闭垃圾回收
    Easy and cheap cluster building on AWS backup
    [转] Maven更新父子模块的版本号, mvn versions:set
    [转] ansible批量执行命令展示
    HBase 批量删除表 disable_all drop_all
    自动添加 ssh key 到远程主机的脚本,应用sshpass和ssh-copy-id
    MongoDB ver 4 几个常用命令
  • 原文地址:https://www.cnblogs.com/zhangxinqi/p/7583816.html
Copyright © 2011-2022 走看看