前の 2 つの記事を結び付けて、この記事では Python の論理演算子、メンバー演算子、および演算子の優先順位の基本的な学習について引き続き説明します。学習価値が高く、興味深いものです。外。
論理演算子
Python 言語は論理演算子をサポートしています。以下では、変数 a が 10、b が 20 であると仮定しています。
#演算子 | 論理式 | 説明 | インスタンス |
---|---|---|---|
and | xx および y | Boolean " AND" - x が False の場合、x と y は False を返し、それ以外の場合は y の計算値を返します。 | (a および b) は 20 を返します。 |
or | x または y | Boolean "or" - x が True の場合は True を返し、それ以外の場合は y の計算値を返します。 。 | (a または b) は 10 を返します。 |
not | not x | ブール値「not」 - x が True の場合、False を返します。 x が False の場合、True を返します。 | not(a and b) Return False |
上記の例の出力結果:
#!/usr/bin/python3 a = 10 b = 20 if ( a and b ): print ("1 - 变量 a 和 b 都为 true") else: print ("1 - 变量 a 和 b 有一个不为 true") if ( a or b ): print ("2 - 变量 a 和 b 都为 true,或其中一个变量为 true") else: print ("2 - 变量 a 和 b 都不为 true") # 修改变量 a 的值 a = 0 if ( a and b ): print ("3 - 变量 a 和 b 都为 true") else: print ("3 - 变量 a 和 b 有一个不为 true") if ( a or b ): print ("4 - 变量 a 和 b 都为 true,或其中一个变量为 true") else: print ("4 - 变量 a 和 b 都不为 true") if not( a and b ): print ("5 - 变量 a 和 b 都为 false,或其中一个变量为 false") else: print ("5 - 变量 a 和 b 都为 true")
上記の出力結果例:
1 - 变量 a 和 b 都为 true 2 - 变量 a 和 b 都为 true,或其中一个变量为 true 3 - 变量 a 和 b 有一个不为 true 4 - 变量 a 和 b 都为 true,或其中一个变量为 true 5 - 变量 a 和 b 都为 false,或其中一个变量为 false
メンバー演算子
上記の演算子の一部に加えて、Python ではメンバー演算子もサポートされており、テスト インスタンスには文字列、リスト、タプルなどの一連のメンバーが含まれています。
演算子 | 説明 | インスタンス |
---|---|---|
in | 戻り値指定されたシーケンス内で値が見つかった場合は True、それ以外の場合は False。 | x は y シーケンス内にあり、x が y シーケンス内にある場合は True を返します。 |
not in | 値が指定されたシーケンス内に見つからない場合は True を返し、それ以外の場合は False を返します。 | x は y シーケンスにありません。x が y シーケンスにない場合は True を返します。 |
次の例は、Python でのすべてのメンバー演算子の操作を示しています:
#!/usr/bin/python3 a = 10 b = 20 list = [1, 2, 3, 4, 5 ]; if ( a in list ): print ("1 - 变量 a 在给定的列表中 list 中") else: print ("1 - 变量 a 不在给定的列表中 list 中") if ( b not in list ): print ("2 - 变量 b 不在给定的列表中 list 中") else: print ("2 - 变量 b 在给定的列表中 list 中") # 修改变量 a 的值 a = 2 if ( a in list ): print ("3 - 变量 a 在给定的列表中 list 中") else: print ("3 - 变量 a 不在给定的列表中 list 中")
上記の例の出力結果は次のとおりです:
1 - 变量 a 不在给定的列表中 list 中 2 - 变量 b 不在给定的列表中 list 中 3 - 变量 a 在给定的列表中 list 中
アイデンティティ演算子が使用されます 2 つのオブジェクトを比較するための保存場所
#例 | #is | |
---|---|---|
x は y、id(x) が id(y) と等しい場合、 | isは結果 1 | is not |
戻り結果 1 |
次の例は、すべての操作を示しています。 Python の ID 演算子: #!/usr/bin/python3 a = 20 b = 20 if ( a is b ): print ("1 - a 和 b 有相同的标识") else: print ("1 - a 和 b 没有相同的标识") if ( id(a) == id(b) ): print ("2 - a 和 b 有相同的标识") else: print ("2 - a 和 b 没有相同的标识") # 修改变量 b 的值 b = 30 if ( a is b ): print ("3 - a 和 b 有相同的标识") else: print ("3 - a 和 b 没有相同的标识") if ( a is not b ): print ("4 - a 和 b 没有相同的标识") else: print ("4 - a 和 b 有相同的标识") | 上記の出力結果の例:
次の表に、すべての演算子を優先順位の高いものから低いものまでリストします。
演算子
説明
インデックス (最高の優先順位) | |
---|---|
ビットごとの反転、単項プラス記号とマイナス記号 (最後の 2 つのメソッドの名前は @ と -@) | |
乗算、除算、モジュロ、整数除算 | |
加算と減算 | ## >> |
& | |
^ | | |
## >= | |
== != | |
= %= /= //= -= = *= **= | #代入演算子|
#is not in | #識別演算子|
メンバー演算子 | |
論理演算子 | |
#!/usr/bin/python3 a = 20 b = 10 c = 15 d = 5 e = 0 e = (a + b) * c / d #( 30 * 15 ) / 5 print ("(a + b) * c / d 运算结果为:", e) e = ((a + b) * c) / d # (30 * 15 ) / 5 print ("((a + b) * c) / d 运算结果为:", e) e = (a + b) * (c / d); # (30) * (15/5) print ("(a + b) * (c / d) 运算结果为:", e) e = a + (b * c) / d; # 20 + (150/5) print ("a + (b * c) / d 运算结果为:", e) | 上記の例の出力結果: (a + b) * c / d 运算结果为: 90.0 ((a + b) * c) / d 运算结果为: 90.0 (a + b) * (c / d) 运算结果为: 90.0 a + (b * c) / d 运算结果为: 50.0 | 関連チュートリアル:
以上がPythonの基礎学習論理演算子、メンバー演算子、演算子の優先順位の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

はい、youcanconcatenateListsusingingaloopinpython.1)useSeparateloopsforeachlisttoeditemstoaresultlist.2)useanestededLooptoAverMultiplElistsForomerConciseapproach.3)applylogingduringConcateNation for forteringEnlumbers

CONCATENATINGLISSTINPYTHONARE:1)theExtend()MethodForin-PlaceModification、2)itertools.chain()formeMoryeficiency withlaredatasets.theextend()MethodModifiestheoriginallist、MakingMemory-efficitientButReisifRecurityifpRESPRESRINVINING

Pythonloopsは、forloopsealforsecences andwhilelcondition basedrepetition.bestPracticesInvolveを使用して、Pythonloopsincludeを使用します


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

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

メモ帳++7.3.1
使いやすく無料のコードエディター

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

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

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