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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival

Publisher Resources

ISBN: 9789864766819