Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
70
|
3 章 Python 語言
whentrue
whenfalse
這兩個子運算式中只有一個會被估算,取決於
condition
的真假值。
這個三元運算子中子運算式的順序可能有點令人困惑。推薦的風格是永遠
都在整個運算式周圍放上括弧。
數值運算
Python 提供一般的數值運算,如我們在表 3-2 中所見。數字(numbers
是不可變的物件:當你在數字物件上進行運算,你會產生一個新的數字物
件,永遠都不會修改現有的物件。你可以透過唯讀特性
z.real
z.imag
來存取一個複數物件
z
的各個部分。試著重新繫結這些屬性會提出一個
例外。
一個數字的選擇性的
+
-
號,以及將一個浮點數字面值連接到一個虛數
以形成一個複數的
+
並非該字面值語法的一部分。它們是普通的運算子,
適用一般的運算子優先序規則(參閱表 3-2)。舉例來說,
-2**2
的估算結
果是
-4
:指數運算的優先序比單元減號(unary minus)還要高,所以整個
運算式的剖析方式是
-(2**2)
,而非
(-2)**2
數值轉換
你可以在任何兩個 Python 內建型別的數字之間進行算術運算(arithmetic
operations)和比較。如果運算元的型別不同,就會套用
強制轉型
coercion
Python 會將帶有「較小」型別的運算元轉為「較大」型別。
這些型別,從小到大依序是整數、浮點數、以及複數。你可以傳入一個
非複數引數給任何的內建數字型別,
int
float
complex
,來請求明確
的轉換。
int
會捨棄其引數的小數部分(如果有的話 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819