[Pythonパージベース]制御フロー
知らなかったり忘れがちな内容を整理しました.
フローコントロール
True : "values", [values], (values), {values}, 1 False : "", [], (), {}, 0, None
「算術」>「リレーションシップ」>「論理順序で適用」算数:+、-、*、/ 関係:>,>=,<,<=,=,!= 論理:and,or,not
以下の条件も可能です.
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).
文脈
(Cのように)、Pythonのfor文は、ユーザーがイテレーションステップと停止条件を定義できるようにするのではなく、任意のシーケンス(リストまたは文字列)のアイテムのシーケンス内の順序で反復されます.
構文
Built-in Function range start: The value of the start parameter (or 0 if the parameter was not supplied) stop: The value of the stop parameter step: The value of the step parameter (or 1 if the parameter was not supplied) Range objects implement the collections.abc.Sequence ABC, and provide features such as containment tests, element index lookup, slicing and support for negative indices.
多くの場合、range()が返すオブジェクトはリストのように見えますが、実際にはリストではありません.オブジェクトは、イテレーションで必要なシーケンスアイテムを順番に返しますが、実際にはリストが作成されず、スペースが節約されます.
これらの対象はかわいいと呼ばれています.これは関数と構造の目標であり、供給が枯渇するまで一連のプロジェクトから何を得ることを望んでいる.私たちはforドアがその構造であることを見た.
文字列、リスト、チュートリアル、コレクション、ディクショナリ->iterable
iterableコールバック関数:range、reverse、列挙、filter、map、zip
break文は、最も近いforまたはwhileループから離れます.
ループ文にはelseセクションがあります.終了すると、ループのイテレーションが枯渇したり(forの場合)条件が偽であるため、ループが実行されます.ただし、ループがbreak文で終了すると、実行されません.
ループとともに使用する場合、elseセクションはif文と比較してtry文のelseセクションと多くの類似点があります.try文のelseセクションは異常が発生しない場合に実行され、ループのelseセクションはbreakが発生しない場合に実行されます.
フローコントロール
1. If
1.1真偽の判別
1.2算術、関係、論理優先度
「算術」>「リレーションシップ」>「論理順序で適用」
>>> print(5 + 10 > 0 and not 7 + 3 == 10)
False
1.3 Comparison Chaining
以下の条件も可能です.
if (a < b < c): # 가능. a < b and b < c와 동일하다.
...
if (a < b > c): # 가능. a < b and b > c와 동일하다.
...
comparison chaining
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
Formally, if a, b, c, …, y, z are expressions and op1, op2, …, opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.
Note that a op1 b op2 c doesn’t imply any kind of comparison between a and c, so that, e.g., x < y > z is perfectly legal (though perhaps not pretty).
2. for
文脈
(Cのように)、Pythonのfor文は、ユーザーがイテレーションステップと停止条件を定義できるようにするのではなく、任意のシーケンス(リストまたは文字列)のアイテムのシーケンス内の順序で反復されます.
構文
for i in <collection>:
<loop body>
2.1 range
Built-in Function range
class range(stop)
class range(start, stop[, step]) # [start, stop)
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.>> r = range(0, 20, 2)
>> r
range(0, 20, 2)
>> 11 in r
False
>> 10 in r
True
>> r.index(10)
5
>> r[5]
10
>> r[:5]
range(0, 10, 2)
>> r[-1]
18
2.2 Iterables
多くの場合、range()が返すオブジェクトはリストのように見えますが、実際にはリストではありません.オブジェクトは、イテレーションで必要なシーケンスアイテムを順番に返しますが、実際にはリストが作成されず、スペースが節約されます.
これらの対象はかわいいと呼ばれています.これは関数と構造の目標であり、供給が枯渇するまで一連のプロジェクトから何を得ることを望んでいる.私たちはforドアがその構造であることを見た.
文字列、リスト、チュートリアル、コレクション、ディクショナリ->iterable
iterableコールバック関数:range、reverse、列挙、filter、map、zip
2.3 break, continue, else
break文は、最も近いforまたはwhileループから離れます.
ループ文にはelseセクションがあります.終了すると、ループのイテレーションが枯渇したり(forの場合)条件が偽であるため、ループが実行されます.ただし、ループがbreak文で終了すると、実行されません.
ループとともに使用する場合、elseセクションはif文と比較してtry文のelseセクションと多くの類似点があります.try文のelseセクションは異常が発生しない場合に実行され、ループのelseセクションはbreakが発生しない場合に実行されます.
3. while
while <expr>:
<statement(s)>
while文はfor文のようにelse文を使用することもできます.(割り込み時にxを実行)Reference
この問題について([Pythonパージベース]制御フロー), 我々は、より多くの情報をここで見つけました https://velog.io/@haebin/Python-기초-정리-흐름-제어テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol