1. 程式人生 > >1. python 類型和運算

1. python 類型和運算

con eval 1.2 有效 form mod 八進制 ESS use

類型和運算 (Types and Operations)

Introducing Python Object Types

在非正式的意義上, 在 Python 中, 我們用一些東西做事情.

  • "事物" 采取像加法和串聯的形式的操作。
  • "東西" 是指我們執行這些操作的對象。

從更正式的角度來看,在 Python 中,數據以對象的形式出現。

As we’ll see, everything is an object in a Python script. Even simple numbers qualify, with values (e.g., 99), and supported operations (addition, subtraction, and so on).

The Python Conceptual Hierarchy (概念上的層級結構)

  1. Programs are composed of modules. (程序由模塊構成)
  2. Modules contain statements. (模塊包含語句)
  3. Statements contain expressions. (語句包含表達式)
  4. Expressions create and process objects. (表達式建立和處理對象)

Built-in Types (內置類型)

技術分享圖片

  • 在 Python 中沒有類型聲明,運行的表達式的語法決定了創建和使用的對象的類型。
  • 一旦創建了一個對象,它就和操作集合綁定了
    ——只能對一個字符串執行字符串操作,並在列表上進行列表相關操作。

在正式術語中, 這意味著 Python 是動態類型的, 它會自動跟蹤類型, 而不是要求聲明代碼, 但它也是強類型的, 這意味著您可以在僅對象操作上執行對其j進行適合該類型的有效操作。

在 Python 中的每一個對象都可以分為不可變性和可變性。比如數字、字符串、元組是不可變的。不可變的:在創建後不能就地改變。

多態意味著一個操作符 (如 +) 的意義取決於被操作的對象。這將是 Python 的關鍵思想:不要將代碼限制在特定的類型上,使代碼自動適用於多種類型。

數字類型 (Numeric Types)

In Python, numbers are not really a single object type, but a category of similar types.

完整的 Python 數值類型清單:

英文 中文
Integer and floating-point objects 整數和浮點數
Complex number objects 復數
Decimal: fixed-precision objects 固定精度的十進制數
Fraction: rational number objects 有理分數
Sets: collections with numeric operations 具有數值運算的集合
Booleans: true and false 布爾類型
Built-in functions and modules: round, math, random, etc. 數值相關的內建函數和模塊
Expressions; unlimited integer precision; bitwise operations; hex, octal, and binary formats 表達式; 無窮的整數精度; 按位運算; 16 進制; 8 進制; 2 進制
Third-party extensions: vectors, libraries, visualization, plotting, etc. 第三方擴展

Numeric Literals (數值常量)

技術分享圖片

Integers may be coded in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2), the last three of which are common in some programming domains. Hexadecimals start with a leading 0x or 0X, followed by a string of hexadecimal digits (\(0\sim 9\) and \(A\sim F\)). Hex digits may be coded in lower- or uppercase. Octal literals start with a leading 0o or 0O (zero and lower- or uppercase letter o), followed by a string of digits (\(0\sim 7\)). In 2.X, octal literals can also be coded with just a leading 0, but not in 3.X—this original octal form is too easily confused with decimal, and is replaced by the new 0o format, which can also be used in 2.X as of 2.6. Binary literals, new as of 2.6 and 3.0, begin with a leading 0b or 0B, followed by binary digits (\(0\sim 1\)).

技術分享圖片

Floor 除法和截斷除法

請註意,在 Python 3.X 中 // (floor, 向下取整) 運算: 如果是浮點型, 則結果為浮點型;否則, 它是一個整數。下面展示一些 Floor 除法和截斷除法的運算:

20 // 3
6
20. // 3
6.0
from math import floor, trunc
floor(2.5)
2
floor(-2.5)
-3
trunc(2.5)
2
trunc(-2.5)
-2
5 / 2, 5 / -2
(2.5, -2.5)
5 // 2, 5 // -2
(2, -3)
5 / 2., 5 / -2.
(2.5, -2.5)
5 // 2., 5 // -2.
(2.0, -3.0)
trunc(5 / -2)
-2

進制數

0o1, 0o20, 0o377 # 八進制
(1, 16, 255)
0x01, 0x10, 0xFF  # 十六進制
(1, 16, 255)
0b1, 0b10000, 0b11111111  # 二進制
(1, 16, 255)
oct(64), hex(64), bin(64)
(‘0o100‘, ‘0x40‘, ‘0b1000000‘)

int 會將一個數字的字符串變換為一個整數,並且通過第二個參數來確定變換後的數字進制:

int(‘64‘), int(‘100‘, 8), int(‘40‘, 16), int(‘1000000‘, 2)
(64, 64, 64, 64)
int(‘0x40‘, 16), int(‘0b1000000‘, 2)
(64, 64)
eval(‘64‘), eval(‘0o100‘), eval(‘0x40‘), eval(‘0b1000000‘)
(64, 64, 64, 64)
‘{0:0}, {1:x}, {2:b}‘.format(64, 64, 64)
‘64, 40, 1000000‘
‘%o, %x, %X‘ % (64, 255, 255)
‘100, ff, FF‘

位操作

x = 1

x << 2 # Shift left 2 bits
4
x | 2   # Bitwise OR
3
x & 1  # Bitwise AND
1
X = 99
bin(X), X.bit_length()  # `X.bit_length()` 獲得二進制字符串的長度
(‘0b1100011‘, 7)
round(1/3, 7)
0.3333333

使用分數小數可以避免精度的損失!

小數

from decimal import Decimal, getcontext
Decimal(0.1)
Decimal(‘0.1000000000000000055511151231257827021181583404541015625‘)
Decimal.from_float(1.25)
Decimal(‘1.25‘)

設置全局精度

Decimal(1) / Decimal(7)
Decimal(‘0.1428571428571428571428571429‘)
getcontext().prec
28
getcontext().prec = 4   # 改變全局精度
Decimal(1) / Decimal(7)
Decimal(‘0.1429‘)

分數

from fractions import Fraction
x = Fraction(1, 3)
x
Fraction(1, 3)
y = Fraction(4, 6)
y
Fraction(2, 3)
x + y
Fraction(1, 1)
x - y
Fraction(-1, 3)
x * y
Fraction(2, 9)
Fraction(‘.25‘)
Fraction(1, 4)
Fraction(‘1.25‘)
Fraction(5, 4)
Fraction(‘.25‘) + Fraction(‘1.25‘)
Fraction(3, 2)

分數還有一個 from_float 方法,並且接受一個 Fraction 作為參數。

(2.5).as_integer_ratio()
(5, 2)
f = 2.5
z = Fraction(*f.as_integer_ratio())
z
Fraction(5, 2)
x
Fraction(1, 3)
float(x)
0.3333333333333333
Fraction.from_float(1.75)
Fraction(7, 4)
a = x + Fraction(*(4.0/3).as_integer_ratio())
a
Fraction(22517998136852479, 13510798882111488)
a.limit_denominator(10)   # 取近似
Fraction(5, 3)

1. python 類型和運算