Or を使用した代入が常に True と評価されるのはなぜですか?
論理演算子を使用して複数の値を比較する場合、Python の動作は直感的な理解から逸脱する可能性があります。たとえば、コードでは:
name = input("Hello. Please enter your name: ") if name == "Kevin" or "Jon" or "Inbar": print("Access granted.") else: print("Access denied.")
Python はこの式を次のように評価するため、権限のないユーザーにもアクセスが許可されます。
if (name == "Kevin") or ("Jon") or ("Inbar"):
この場合、結果はどの名前に対しても True になります。 "Jon" と "Inbar" は独立した論理オペランドとして扱われるためです。
値を複数の値と比較する方法その他
複数の値と正しく比較するには:
- 複数の == 演算子を使用します:
if name == "Kevin" or name == "Jon" or name == "Inbar":
- を雇用するコレクション:
if name in {"Kevin", "Jon", "Inbar"}:
- any() とジェネレーター式を使用します:
if any(name == auth for auth in ["Kevin", "Jon", "Inbar"]):
パフォーマンス比較
の場合可読性と効率性を考慮すると、一般にコレクションを使用することが推奨されます。
import timeit timeit.timeit('name in {"Kevin", "Jon", "Inbar"}', setup="name='Inbar'") # Faster timeit.timeit('any(name == auth for auth in ["Kevin", "Jon", "Inbar"])', setup="name='Inbar'") # Slower
解析動作の証明
組み込みの ast モジュールは、a == b のような式が確認されることを確認します。 or c または d は、
BoolOp( op=Or(), values=[ Compare(left=Name(...), ops=[Eq()], comparators=[Name(...)]), Name(...), Name(...), Name(...)])
として解析され、「or」が適用されることを示します。個々の比較と表現。
以上がPython で `name == 'Kevin' または 'Jon' または 'Inbar' が常に True と評価されるのはなぜですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

VSCode Windows 64 ビットのダウンロード
Microsoft によって発売された無料で強力な IDE エディター

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SublimeText3 中国語版
中国語版、とても使いやすい

ホットトピック









