Python では、and and or はご想像のとおり、ブール値を返しません。代わりに、実際に比較する値の 1 つを返します。
1. and:
>>> 'a' and 'b' 'b' >>> '' and 'b' '' >>> 'a' and 'b' and 'c' 'c'
は、ブールコンテキスト内の式を左から右に評価し、ブールコンテキスト内のすべての値が true の場合、最後の値を返します。
ブールコンテキストの値が false の場合、and は最初の false 値を返します
2. or:
>>> 'a' or 'b' 'a' >>> '' or 'b' 'b' >>> '' or [] or {} {} >>> 0 or 'a' or 'c' 'a'
or を使用する場合、値は and と同様にブールコンテキストで左から右に評価されます。値が true の場合、またはその値をすぐに返します
すべての値が false の場合、または最後の false 値を返します
ブール値コンテキストの または は、最初の true 値が見つかるまで式の計算を実行し続けることに注意してください。その後、残りの比較値は無視されます
3. and-or:
and-or は前の 2 つの構文を組み合わせて推論できます。
>>> a='first' >>> b='second' >>> 1 and a or b 'first' >>> (1 and a) or b 'first' >>> 0 and a or b 'second' >>> (0 and a) or b 'second' >>>
この構文は、C 言語の bool ? a : b 式に似ています。式全体が左から右に評価されるため、and 式が最初に評価されます。 1 と 'first' は 'first' と評価され、次に 'first' または 'second' は 'first' と評価されます。
0 と 'first' は False と評価され、次に 0 または '2 番目' は '2 番目' と評価されます。
and-or は主に三項演算子 bool?a:b を模倣するために使用されます。つまり、式 bool が true の場合は a を取得し、それ以外の場合は b を取得します。
and-or トリック、bool および a または b 式は、ブール値コンテキストで a が false と評価される場合、C 言語の式 bool a : b のように機能しません。
4. and-or を安全に使用する
>>> a="" >>> b="second" >>> (1 and [a] or [b]) [''] >>> (1 and [a] or [b])[0] '' >>>
[a] は空ではないリストなので、 false になることはありません。 a が 0 や ''、あるいは他の false 値であっても、要素が 1 つあるため list [a] は true になります。
責任あるプログラマーは、and-or テクニックを関数にカプセル化する必要があります:
def choose(bool,a,b): return (bool and [a] or [b])[0] print choose(1,'','second') #''

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

ドリームウィーバー CS6
ビジュアル Web 開発ツール

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

Dreamweaver Mac版
ビジュアル Web 開発ツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

ホットトピック









