1. 位操作 除了一般的数学运算,Python也支持C语言中的大多数数学表达式,这包括那些把整数当做二进制位串对待的操作。例如,实现位移及布尔操作
    # ipthyon3
    
    In [1]: x = 1
    
    In [2]: x << 2
    Out[2]: 4
    
    In [3]: x | 2
    Out[3]: 3
    
    In [4]: x & 1
    Out[4]: 1
    

    在第一个表达式中,二进制数1(0001)左移了两位,变成二进制数4(0100)。第二个操作是一个二进制或(0001|0010=0011)和 一个二进制与(0001&0001=0001)。这样的按位进行掩码的运算,使我们可以对一个整数进行多个标志位和值进行编码。

    	
    In [1]: X = 0b0001
    
    In [2]: X << 2
    Out[2]: 4
    
    In [3]: bin(X << 2)
    Out[3]: '0b100'
    
    In [4]: 
    
    In [5]: bin(X << 4)
    Out[5]: '0b111111110000'
    
    In [6]: bin(X & 0b1) 
    Out[6]: '0b1'
    
    In [7]: X = 0xFF
    
    In [8]: bin(X)
    Out[8]: '0b11111111'
    
    In [9]: X ^ 0b10101010
    Out[9]: 85
    
    In [10]: bin(X ^ 0b10101010)
    Out[10]: '0b1010101'
    
    In [11]: int('1010101',2)
    Out[11]: 85
    
    In [12]: hex(85)
    Out[12]: '0x55'
    

    如果Python代码必须由C程序生成的网络包或封装了二进制数打交道的话,它是很实用的。

  2. 其它内置数学工具 除了核心对象类型以外,Python还支持用于数字处理的内置函数和内置模块。
    In [1]: import math
    
    In [2]: math.pi
    Out[2]: 3.141592653589793
    
    In [3]: math.e
    Out[3]: 2.718281828459045
    
    In [4]: math.sin(2 * math.pi / 180)
    Out[4]: 0.03489949670250097
    
    In [5]: math.sqrt(144),math.sqrt(2)
    Out[5]: (12.0, 1.4142135623730951)
    
    In [6]: pow(2,4),2 ** 4
    Out[6]: (16, 16)
    
    In [7]: abs(-42.0),sum((1,2,3,4))
    Out[7]: (42.0, 10)
    
    In [8]: min(3,1,2,4),max(3,2,1,4)
    Out[8]: (1, 4)
    
    In [9]: math.floor(2.567),math.floor(-2.567)
    Out[9]: (2, -3)
    
    In [10]: math.trunc(2.567),math.trunc(-2.567)
    Out[10]: (2, -2)
    
    In [11]: int(2.567),int(-2.567)
    Out[11]: (2, -2)
    
    In [12]: round(2.567),round(2.467),round(2.567,2)
    Out[12]: (3, 2, 2.57)
    
    In [13]: '%.1f' % 2.567,'{0:.2f}'.format(2.567)
    Out[13]: ('2.6', '2.57')
    
  3. 其它数字类型
    • 小数数字

      比其它数据类型复杂一些,小数是通过一个导入的模块调用函数后创建的,而不是通过运行常量表达式创建的。从功能上来讲,小数对象就像浮点数,只不过 他们有固定的位数和小数点,因此小数是由固定的精度的浮点值。使用小数我们可以定义如何省略和截断额外的小数数字,尽管它对平常的浮点数类型带来了微小的 性能损失,小数类型对表现固定精度的特性(例如钱的总和)以及对实现更好的数字精度是一个理想的工具。

      浮点数学缺乏精确性,因为用来存储数值的空间有限。例如,下面的计算应该得到零,但是结果却没有,结果接近零,但是却没有足够的位数去实现这样的精度:

      In [1]: 0.1 + 0.1 + 0.1 - 0.3
      Out[1]: 5.551115123125783e-17
      

      不过如果使用小数对象,结果就能修正

      In [2]: from decimal import Decimal
      
      In [3]: De
      Decimal             DeprecationWarning  Desktop/            
      
      In [3]: Decimal('0.1') + Decimal('0.1') + Decimal('0.1') - Decimal('0.3')
      Out[3]: Decimal('0.0')
      

      decimal模块中的其它工具可以用来设置所有小数数值的精度,设置错误处理等。例如,这个模块中的一个上下文对象允许指定精度(小数位数)和舍入模式(舍去,进位等)。该精度全局性地适用于调用线程中创建的所有小数:

      In [1]: import decimal
      
      
      In [2]: decimal.Decimal(1) / decimal.Decimal(7)
      Out[2]: Decimal('0.1428571428571428571428571429')
      
      In [3]: decimal.getcontext().prec = 4
      
      In [4]: decimal.Decimal(1) / decimal.Decimal(7)
      Out[4]: Decimal('0.1429')
      

      这对于处理货币的应用程序特别有用,其中,美分表示为两个小数位数。在这个上下文利,小数实际上是手动舍入和字符串格式化的一种替代方式。

    • 分数类型

      分数以类似于小数位数和指定舍入或截断策略来控制数值精度。分数以类似于小数的方式使用,它也存在于模块中,导入其构造函数并传递一个分子和一个分母就可以产生一个分数。

      In [1]: from fractions import Fraction
      
      In [2]: x = Fraction(1,3)
      
      In [3]: y = Fraction(4,6)
      
      In [4]: x
      Out[4]: Fraction(1, 3)
      
      In [5]: y
      Out[5]: Fraction(2, 3)
      
      In [6]: print(y)
      2/3
      
      In [10]: Fraction('.25')
      Out[10]: Fraction(1, 4)
      
      In [11]: Fraction('1.25')
      Out[11]: Fraction(5, 4)
      
      In [12]: Fraction('.25')+Fraction('1.25')
      Out[12]: Fraction(3, 2)
      

      数值精度:

      	
      In [1]: a = 1 / 3.0
      
      In [2]: b = 4 / 6.0
      
      In [3]: a
      Out[3]: 0.3333333333333333
      
      In [4]: b
      Out[4]: 0.6666666666666666
      
      In [5]: a + b
      Out[5]: 1.0
      
      In [6]: a - b
      Out[6]: -0.3333333333333333
      
      In [7]: a * b
      Out[7]: 0.2222222222222222
      

      对于那些用内存中给定的有限位数无法精确表示的值,浮点数的局限尤为明显。分数和小数都提供了得到精确结果的方式,虽然要牺牲一些速度:

      In [1]: 0.1 + 0.1 + 0.1 - 0.3
      Out[1]: 5.551115123125783e-17
      
      In [2]: from fractions import Fraction
      
      In [3]: Fraction(1,10) + Fraction(1,10) + Fraction(1,10) - Fraction(3,10)
      Out[3]: Fraction(0, 1)
      

      实际上,分数保持精确性,并自动简化结果。

    • 转换和混合类型

      浮点数对象和分数对象的转换

      In [1]: (2.5).as_integer_ratio()
      Out[1]: (5, 2)
      
      In [2]: from fractions import Fraction
      
      In [3]: f = 2.5
      
      In [4]: z = Fraction(*f.as_integer_ratio())
      
      In [5]: z
      Out[5]: Fraction(5, 2)
      

      混合转换

      In [1]: from fractions import Fraction
      
      In [2]: x = Fraction(1,3)
      
      In [3]: x + 2
      Out[3]: Fraction(7, 3)
      
      In [4]: x + 2.0
      Out[4]: 2.3333333333333335
      
      In [5]: x + (1./3)
      Out[5]: 0.6666666666666666
      
      In [6]: x + (4./3)
      Out[6]: 1.6666666666666665
      
      In [7]: x + Fraction(4,3)
      Out[7]: Fraction(5, 3)
      

      通过限制最大分母值来简化

      In [1]: 4.0 / 3
      Out[1]: 1.3333333333333333
      
      In [2]: (4.0 / 3).as_integer_ratio()
      Out[2]: (6004799503160661, 4503599627370496)
      
      In [3]: from fractions import Fraction
      
      In [4]: x = Fraction(1,3)
      
      In [5]: a = x + Fraction(*(4.0 / 3).as_integer_ratio())
      
      In [6]: a
      Out[6]: Fraction(22517998136852479, 13510798882111488)
      
      In [7]: a.limit_denominator(10)
      Out[7]: Fraction(5, 3)
      
  4. 集合 set是一些唯一的,不可变的对象的无序集合,这些对象支持数学集合理论相应的操作。一个项在集合中只能出现一次,尤其是在涉及数字和数据库的工作中。
    In [1]: x = set('abcde')
    
    In [2]: x
    Out[2]: {'a', 'b', 'c', 'd', 'e'}
    
    In [3]: set([1,2,3,4])
    Out[3]: {1, 2, 3, 4}
    
    In [4]: {1,2,3,4}
    Out[4]: {1, 2, 3, 4}
    
    In [1]: {1,2,3} | {3,4}
    Out[1]: {1, 2, 3, 4}
    
    In [2]: {1,2,3} | [3,4]
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
     in ()
    ----> 1 {1,2,3} | [3,4]
    
    TypeError: unsupported operand type(s) for |: 'set' and 'list'
    
    In [3]: {1,2,3}.union([3,4])  
    Out[3]: {1, 2, 3, 4}
    
    In [4]: {1,2,3}.union({3,4})
    Out[4]: {1, 2, 3, 4}
    
    In [5]: {1,2,3}.union(set([3,4]))
    Out[5]: {1, 2, 3, 4}
    
    In [6]: {1,2,3}.intersection((1,3,5))
    Out[6]: {1, 3}
    
    In [7]: {1,2,3}.issubset(range(-5,5))
    Out[7]: True
    

    不可变限制和冻结集合。集合是强大而灵活地对象,但是,它们很大程度上是由于其实现,集合只包含不可变的(即可散列的)对象类型。因此,列表和字典不能嵌入到集合中,但是,如果你需要存储复合值得话,元组是可以嵌入的。在集合操作中使用元组的时候,元组比较其完整的值

    	
    In [1]: S = {1.23}
    
    In [2]: S.add([1,2,3])
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
     in ()
    ----> 1 S.add([1,2,3])
    
    TypeError: unhashable type: 'list'
    
    In [3]: S.add({'a':1})
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
     in ()
    ----> 1 S.add({'a':1})
    
    TypeError: unhashable type: 'dict'
    
    In [4]: S.add((1,2,3))
    
    In [5]: S
    Out[5]: {1.23, (1, 2, 3)}
    
    In [6]: S | {(4,5,6),(1,2,3)}
    Out[6]: {1.23, (4, 5, 6), (1, 2, 3)}
    
    In [7]: (1,2,3) in S
    Out[7]: True
    
    In [8]: (1,4,3) in S
    Out[8]: False
    

    例如,集合中的元组可以用来表示日期,记录,ip地址等。集合本身也是不可变的,因此,不能直接嵌入到其它集合中。如果需要在一个集合中存储另一个集合,可以调用frozenset,但是,它创建一个不可变的集合,该集合不可修改并且可以嵌套到其它集合中。

    集合解析类似于列表解析的形式,但是,编写在大括号而不是方括号中,并且作用于集合而不是列表。集合解析运行一个循环并且每次迭代时收集一个表达式的结果,通过一个循环变量俩访问当前的迭代值以用于集合表达式中。结果就是通过运行代码创建一个新的集合

    In [1]: {x ** 2 for x in [1,2,3,4]}
    Out[1]: {1, 4, 9, 16}
    
    In [2]: {x for x in 'spam'}
    Out[2]: {'a', 'm', 'p', 's'}
    
    In [3]: {c * 4 for c in 'spam'}
    Out[3]: {'aaaa', 'mmmm', 'pppp', 'ssss'}
    
    In [4]: {c * 4 for c in 'spamham'} 
    Out[4]: {'aaaa', 'hhhh', 'mmmm', 'pppp', 'ssss'}
    
    In [5]: S = {c * 4 for c in 'spam'}
    
    In [6]: S | {'mmmm','xxxx'}
    Out[6]: {'aaaa', 'mmmm', 'pppp', 'ssss', 'xxxx'}
    
    In [7]: S & {'mmmm','xxxx'}
    Out[7]: {'mmmm'}
    

    集合可以很方便有限的剔除重复项,当你遍历图形或其他的回环结构的时候,集合可以用来记录已经访问过的位置。

  5. 布尔型 bool原本是一个数字,True和False仅仅是整数1和0不同形式显示后的定制版本而已。
    In [1]: True + 5
    Out[1]: 6
    
    In [2]: False + 5
    Out[2]: 5
    
分类: Python3

6 条评论

677betcasino · 2025-12-19 20:10

Yo, 677betcasino? That’s a new name. Gotta admit, I’m intrigued. Worth a punt, maybe? Check it, see what you think 677betcasino

WinningIsMyGame · 2025-12-24 10:52

I’ve had some lucky streaks on 13winnner! The site is easy to use and it is easy to deposit. What more could you want!

balato88 · 2026-01-14 20:49

balato88 https://www.balato88u.com

fb777 slot · 2026-01-14 21:15

fb777 slot https://www.fb7777-slot.com

gkbet · 2026-01-15 02:00

gkbet https://www.gkbeth.org

boss778 · 2026-01-26 21:48

Boss778 Philippines: Top Slot Games, Secure Login, & Easy Register. Download the App and Access Link Alternatif for the Best Online Casino Experience. Experience the ultimate online casino at Boss778 Philippines. Enjoy top boss778 slot games, secure boss778 login, and easy boss778 register. Access the boss778 download app and link alternatif now! visit: boss778

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注