搜尋
首頁後端開發Python教學Python中內建常數的深入理解

這篇文章主要跟大家介紹了關於Python中內建常數的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看吧。

前言

大家都知道Python內建的常數不多,只有6個,分別是True、False、None、 NotImplemented、Ellipsis、debug。以下就來看看詳細的介紹:

一. True

#1. True是bool型別用來表示真值的常量。

>>> True
True
>>> type(True)
<class &#39;bool&#39;>

2. 對常數True進行任何賦值運算都會拋出語法錯誤

>>> True = 1
SyntaxError: can&#39;t assign to keyword

二. False

#1. False是bool型別用來表示假值的常數。

>>> False
False
>>> type(False)
<class &#39;bool&#39;>

2. 對常數False進行任何賦值運算都會拋出語法錯誤。

>>> False = 0
SyntaxError: can&#39;t assign to keyword

三. None

#1. None表示無,它是NoneType的唯一值。

>>> None #表示无,没有内容输出
>>> type(None)
<class &#39;NoneType&#39;>

2. 對常數None進行任何賦值運算都會拋出語法錯誤。

>>> None = 2
SyntaxError: can&#39;t assign to keyword

3. 對於函數,如果沒有return語句,即相當於傳回None。

>>> def sayHello(): #定义函数
 print(&#39;Hello&#39;)

 
>>> sayHello()
Hello
>>> result = sayHello()
Hello
>>> result
>>> type(result)
<class &#39;NoneType&#39;>

四. NotImplemented

#1.  NotImplemented是NotImplementedType類型的常數。

>>> NotImplemented
NotImplemented
>>> type(NotImplemented)
<class &#39;NotImplementedType&#39;>

2. 使用bool()函數進行測試可以發現,NotImplemented是一個真值。

>>> bool(NotImplemented)
True

3. NotImplemented不是一個絕對意義上的常數,因為他可以被賦值卻不會拋出語法錯誤,我們也不應該去對其賦值,否則會影響程式的執行結果。

>>> bool(NotImplemented)
True
>>> NotImplemented = False
>>> 
>>> bool(NotImplemented)
False

4. NotImplemented多用於一些二元特殊方法(比如eq、lt等)中做為返回值,表明沒有實現方法,而Python在結果返回NotImplemented時會聰明的交換二個參數進行另外的嘗試。

>>> class A(object):
 def init(self,name,value):
  self.name = name
  self.value = value
 def eq(self,other):
  print(&#39;self:&#39;,self.name,self.value)
  print(&#39;other:&#39;,other.name,other.value)
  return self.value == other.value #判断2个对象的value值是否相等

>>> a1 = A(&#39;Tom&#39;,1)
>>> a2 = A(&#39;Jay&#39;,1)
>>> a1 == a2
self: Tom 1
other: Jay 1
True
>>> class A(object):
 def init(self,name,value):
  self.name = name
  self.value = value
 def eq(self,other):
  print(&#39;self:&#39;,self.name,self.value)
  print(&#39;other:&#39;,other.name,other.value)
  return NotImplemented

>>> a1 = A(&#39;Tom&#39;,1)
>>> a2 = A(&#39;Jay&#39;,1)
>>> a1 == a2
self: Tom 1
other: Jay 1
self: Jay 1
other: Tom 1
False

當執行a1==a2(即呼叫eq(a1,a2)),傳回NotImplemented時,Python會自動交換參數再次呼叫eq(a2,a1)。

五. Ellipsis

1. Ellipsis是ellipsis類型的常數,它和…是等價的。

>>> Ellipsis
Ellipsis
>>> type(Ellipsis)
<class &#39;ellipsis&#39;>
>>> ...
Ellipsis
>>> ... == Ellipsis
True

2. 使用bool()函數進行測試可以發現,Ellipsis是一個真值。

>>> bool(Ellipsis)
True

3. Ellipsis不是一個絕對意義上的常數,因為他可以被賦值卻不會拋出語法錯誤,我們也不應該去對其賦值,否則會影響程式的執行結果。

>>> bool(Ellipsis)
True
>>> Ellipsis = False
>>> bool(Ellipsis)
False

4. Ellipsis多用於表示循環的資料結構。

>>> a = [1,2,3,4]
>>> a.append(a)
>>> a
[1, 2, 3, 4, [...]]
>>> a
[1, 2, 3, 4, [...]]
>>> len(a)
>>> a[4]
[1, 2, 3, 4, [...]]
>>>

#六. debug

#1. debug是一個bool型別的常數。

>>> debug
True
>>> type(debug)
<class &#39;bool&#39;>

2. 對常數debug進行任何賦值運算都會拋出語法錯誤。

>>> debug = False
SyntaxError: assignment to keyword

3. 如果Python沒有使用-O選項啟動,此常數為真值,否則是假值。

總結

#

以上是Python中內建常數的深入理解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具