8.python中的数字

  python中数字对象的创建如下,

a = 123
b = 1.23
c = 1+1j

  可以直接输入数字,然后赋值给变量。

  同样也可是使用类的方式:

a = int(123)
b = float(1.23)
c = complex(1+1j)

  但一般不用类的方式创建,直接输入数字就好了。

  python中的数字包括了整型 int ,长整型 long , 浮点型 float , 复数 complex ,其差别为:

int(整型)

  也称有符号整数,只有正或负整数,不带小数点。
  其和长整型在于整数有一定的位数限制:
    在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
    在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
  一旦超过位数,则会自动转换为长整型(python2.2以后的版本)
 
long(长整型)
  理论上是无限长的,但数据终究是储存在内存中的,所以实际长度限制取决于内存的大小

float(浮点型)
  带有小数的数字都被称为浮点型
 
complex(复数)
  就是数学中的复数,定义和数学中的一样(z=a+bi,这里a和b是实数,i是虚数单位,例如1+5j)
 

一.整型

 
Help on class int in module __builtin__:

class int(object)
 |  int(x=0) -> int or long
 |  int(x, base=10) -> int or long
 |  
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is floating point, the conversion truncates towards zero.
 |  If x is outside the integer range, the function returns a long instead.
 |  
 |  If x is not a number or if base is given, then x must be a string or
 |  Unicode object representing an integer literal in the given base.  The
 |  literal can be preceded by '+' or '-' and be surrounded by whitespace.
 |  The base defaults to 10.  Valid bases are 0 and 2-36.  Base 0 means to
 |  interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4
 |  
 |  Methods defined here:
 |  
 |  __abs__(...)
 |      x.__abs__() <==> abs(x)
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __and__(...)
 |      x.__and__(y) <==> x&y
 |  
 |  __cmp__(...)
 |      x.__cmp__(y) <==> cmp(x,y)
 |  
 |  __coerce__(...)
 |      x.__coerce__(y) <==> coerce(x, y)
 |  
 |  __div__(...)
 |      x.__div__(y) <==> x/y
 |  
 |  __divmod__(...)
 |      x.__divmod__(y) <==> divmod(x, y)
 |  
 |  __float__(...)
 |      x.__float__() <==> float(x)
 |  
 |  __floordiv__(...)
 |      x.__floordiv__(y) <==> x//y
 |  
 |  __format__(...)
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getnewargs__(...)
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __hex__(...)
 |      x.__hex__() <==> hex(x)
 |  
 |  __index__(...)
    '''用于切片,但不接受参数,返回的是整个数字,所以切片对数字而言没有意义'''
 |      x[y:z] <==> x[y.__index__():z.__index__()]  
 |  
 |  __int__(...)
 |      x.__int__() <==> int(x)
 |  
 |  __invert__(...)
 |      x.__invert__() <==> ~x
 |  
 |  __long__(...)
 |      x.__long__() <==> long(x)
 |  
 |  __lshift__(...)
 |      x.__lshift__(y) <==> x<<y
 |  
 |  __mod__(...)
 |      x.__mod__(y) <==> x%y
 |  
 |  __mul__(...)
 |      x.__mul__(y) <==> x*y
 |  
 |  __neg__(...)
 |      x.__neg__() <==> -x
 |  
 |  __nonzero__(...)
 |      x.__nonzero__() <==> x != 0
 |  
 |  __oct__(...)
 |      x.__oct__() <==> oct(x)
 |  
 |  __or__(...)
 |      x.__or__(y) <==> x|y
 |  
 |  __pos__(...)
 |      x.__pos__() <==> +x
 |  
 |  __pow__(...)
 |      x.__pow__(y[, z]) <==> pow(x, y[, z])
 |  
 |  __radd__(...)
 |      x.__radd__(y) <==> y+x
 |  
 |  __rand__(...)
 |      x.__rand__(y) <==> y&x
 |  
 |  __rdiv__(...)
 |      x.__rdiv__(y) <==> y/x
 |  
 |  __rdivmod__(...)
 |      x.__rdivmod__(y) <==> divmod(y, x)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __rfloordiv__(...)
 |      x.__rfloordiv__(y) <==> y//x
 |  
 |  __rlshift__(...)
 |      x.__rlshift__(y) <==> y<<x
 |  
 |  __rmod__(...)
 |      x.__rmod__(y) <==> y%x
 |  
 |  __rmul__(...)
 |      x.__rmul__(y) <==> y*x
 |  
 |  __ror__(...)
 |      x.__ror__(y) <==> y|x
 |  
 |  __rpow__(...)
 |      y.__rpow__(x[, z]) <==> pow(x, y[, z])
 |  
 |  __rrshift__(...)
 |      x.__rrshift__(y) <==> y>>x
 |  
 |  __rshift__(...)
 |      x.__rshift__(y) <==> x>>y
 |  
 |  __rsub__(...)
 |      x.__rsub__(y) <==> y-x
 |  
 |  __rtruediv__(...)
 |      x.__rtruediv__(y) <==> y/x
 |  
 |  __rxor__(...)
 |      x.__rxor__(y) <==> y^x
 |  
 |  __str__(...)
 |      x.__str__() <==> str(x)
 |  
 |  __sub__(...)
 |      x.__sub__(y) <==> x-y
 |  
 |  __truediv__(...)
 |      x.__truediv__(y) <==> x/y
 |  
 |  __trunc__(...)
 |      Truncating an Integral returns itself.
 |  
 |  __xor__(...)
 |      x.__xor__(y) <==> x^y
 |  
 |  bit_length(...)
 |      int.bit_length() -> int
 |      '''返回改数字用二进制要几位来表示'''
 |      Number of bits necessary to represent self in binary.
 |      >>> bin(37)  #得出其二进制表示形式
 |      '0b100101'  #0b是二进制的标识,用来说明其是二进制形式,其后的100101才是真正的二进制代码
 |      >>> (37).bit_length()
 |      6  #37的二进制表示是 100101,一共6位,所以返回6
 |  
 |  conjugate(...)
 |      Returns self, the complex conjugate of any int.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  denominator
 |      the denominator of a rational number in lowest terms
 |  
 |  imag
 |      the imaginary part of a complex number
 |  
 |  numerator
 |      the numerator of a rational number in lowest terms
 |  
 |  real
 |      the real part of a complex number
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
int

 

可以看出其内置方法分为3种:

  1.普通方法(已经在代码中注释说明)

  2.相当于某些内置函数的方法(参考这里

  3.与运算符相关的方法

这里详细介绍一下python的运算符:

1.算数运算符

  以下假设变量a=10,变量b=20

 

2.比较运算符

  以下假设变量a=10,变量b=20

  关于python中比较运算符使用的总结:

  1.python中任意对象都可以比较

  2.相同类型的对象(实例),如果是数字型(int/float/long/complex),则按照简单的大小来比较;如果是非数字型,且类(型)中定义了__cmp__(含__gt__,__lt__等)则按照__cmp__来比较,否则按照地址(id)来比较。

  3.不同类型的对象(实例),如果其中一个比较对象是数字型(int/float/long/complex等),则数字型的对象<其它非数字型的对象;如果两个都是非数字型的对象,则按照类型名的顺序比较,如{} < "abc"(按照"dict" < "str"),而"abc" > [1,2], "abc" < (1,2)。

  4.对于自定义的类(型)实例,如果继承自基本类型,则按照基本类型的规则比较(1-3)。否则,old-style class < new-style class, new-style class之间按照类型名顺序比较,old-style class之间按照地址进行比较。

  5.bool类型是int的子类,且True=1, False=0,比较时按照1-4来比较,如True > -1, True < 4.2, True < "abc"等
  
  这部分内容 转载于: 戳这里


3.赋值运算符

  以下假设变量a=10,变量b=20

4.位运算符

  按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下:

  下表中变量 a = 60,b = 13

5.逻辑运算符

  以下假设变量 a = 10, b=20

 

6.成员运算符

  除了以上的一些运算符之外,Python还支持成员运算符,测试实例中包含了一系列的成员,包括字符串,列表或元组。

 

7.身份运算符

  身份运算符用于比较两个对象的存储单元

 

  这里要讲讲python中的内存池(缓冲池)了,python使用内存池来管理小的整型数和小的字符串等等。
  什么意思呢?

  当我们执行以下赋值运算时

a = 123
b = 123

 

  理论上是要分别在内存中创建两个值,然后赋值给变量的,但是这样做实在是有点浪费,明明是一样的数,却要占用两个内存空间。

  所以python为了节约内存,引入了内存池,当小的整型(-5~257,不包括257)要多次创建时,只创建一次,后面的都将引用指向同一个地方,此时使用身份运算符会出现:

  字符串的则以256个ascll码为分界

  有兴趣的可以参考:戳这里

 

8.运算符的优先级

  另外,我们也可以像数学一样使用括号() ,来指定某个运算先进行

  更多参考:戳这里

  有别人做好的轮子就是不一样,一路复制粘贴就好了,真轻松。


 

二、长整型

  和整型基本一样,这里就不重复了。自己可以用 help() 函数查看。
  遇事不决喊救命

 


 

三、浮点型

   这里就不全部列举了,只讲讲不同的。

 |  __setformat__(...)
 |      float.__setformat__(typestr, fmt) -> None
 |      
 |      You probably don't want to use this function.  It exists mainly to be
 |      used in Python's test suite.
 |      
 |      typestr must be 'double' or 'float'.  fmt must be one of 'unknown',
 |      'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be
 |      one of the latter two if it appears to match the underlying C reality.
 |      
 |      Override the automatic determination of C-level floating point type.
 |      This affects how floats are converted to and from binary strings.

 

  一个内部方法,用于内部测试的,官方都说了You probably don't want to use this function(你可能不想使用这个函数),而实际上我们也用不到,我也不知道有什么用,一般可以无视。

 

 |  __trunc__(...)
 |      Return the Integral closest to x between 0 and x.

  返回最接近x从0积分和x,貌似和积分运算有关,没用过

 

 

 |  as_integer_ratio(...)
 |      float.as_integer_ratio() -> (int, int)
 |      
 |      Return a pair of integers, whose ratio is exactly equal to the original
 |      float and with a positive denominator.
 |      Raise OverflowError on infinities and a ValueError on NaNs.
 |      
 |      >>> (10.0).as_integer_ratio()
 |      (10, 1)
 |      >>> (0.0).as_integer_ratio()
 |      (0, 1)
 |      >>> (-.25).as_integer_ratio()
 |      (-1, 4)

  返回一个由两个数字组成的元祖,而两个数字相除就等于原浮点数,其实就是返回一个最简分数,分子在前面,分母在后面。

 

 

 |  conjugate(...)
 |      Return self, the complex conjugate of any float.

  返回本身的共轭复数

 

 |  fromhex(...)
 |      float.fromhex(string) -> float
 |      
 |      Create a floating-point number from a hexadecimal string.
 |      >>> float.fromhex('0x1.ffffp10')
 |      2047.984375
 |      >>> float.fromhex('-0x1p-1074')
 |      -4.9406564584124654e-324

  用一个十六进制的字符串来创建一个浮点数

 

 

 |  hex(...)
 |      float.hex() -> string
 |      
 |      Return a hexadecimal representation of a floating-point number.
 |      >>> (-0.1).hex()
 |      '-0x1.999999999999ap-4'
 |      >>> 3.14159.hex()
 |      '0x1.921f9f01b866ep+1'

  上面方法的逆运算,返回一个浮点数的十六进制表示的字符串

 

 

 |  is_integer(...)
 |      Return True if the float is an integer.

  判断一个浮点数是否为整数

  其实就是看小数位是否都为0

 


 

四、复数

  都是一些运算和内置函数相关的,唯一特别的是

 |  conjugate(...)
 |      complex.conjugate() -> complex
 |      
 |      Return the complex conjugate of its argument. (3-4j).conjugate() == 3+4j.

   返回一个原复数的共轭复数

 

转载于:https://www.cnblogs.com/scolia/p/5534334.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值