Python Basics3 : conditional execution, boolean expressions and logical operators 真偽判定

Boolean expression in if statement(if condition)

if statementの書き方

== Trueはいらない。

comparison operator 

Logical Operators

There are three logical operatorsandor, and not. All three operators take boolean operands and produce boolean values. The semantics (meaning) of these operators is similar to their meaning in English:

  • x and y is True if both x and y are True. Otherwise, and produces False.
  • x or y yields True if either x or y is True. Only if both operands are False does or yield False.
  • not x yields False if x is True, and vice versa.

 

Pythonではif statement は前から読まれて、適合しなければそれ以降のifstatementの条件は読まない。

crashさせないために、順番が大事らしいが意味がわからない。

if statementの途中で「予想外」が起きないように?

 

logical operatorの書き方の注意

こんな感じに読み込まれます。logical operatoの前と後で処理されるため、省略できません。

 

 boolean values

There are only two boolean values. They are True and False

Capitalization is important, since true and false are not boolean values (remember Python is case sensitive).

The in and not in Operators

#listの場合は、完全一致でないといけない。

'x' in y or z

wrong : the in operator is only on the left side of the or statement.

'x' in y or 'x' in z

correct

 

course_1_assessment_7

items = [“whirring”, “wow!”, “calendar”, “wry”, “glass”, “”, “llama”,”tumultuous”,”owing”]
acc_num = 0
for item in range(len(items)):
if “w” in items[item] :
acc_num +=1

 

sentence = “python is a high level general purpose programming language that can be applied to many different classes of problems.”

items = sentence.split(” “)
print(items)

num_a_or_e = 0
for item in range(len(items)):
if “e” in items[item] or “a” in items[item]:
num_a_or_e += 1

print(num_a_or_e)

s = “singing in the rain and playing in the rain are two entirely different situations but both can be fun”
vowels = [‘a’,’e’,’i’,’o’,’u’]

item = 0
num_vowels = 0
# Write your code here.
for item in range(len(s)):
if s[item] in vowels:
num_vowels += 1

 

if statementの選択肢 unary selection、Nested Conditionals, and Chained Conditionals

unary selection

else: がないもの

../_images/flowchart_if_only.png

x = 10
if x < 0:
print("The negative number ", x, " is not valid here.")
print("This is always printed")

Nested Conditionals

elseの中にif statementがある。

if x < y:
    print("x is less than y")
else:
    if x > y:
        print("x is greater than y")
    else:
        print("x and y must be equal")

../_images/flowchart_nested_conditional.png

 

Chained Conditionals

elifを使う。

if x < y:
    print("x is less than y")
elif x > y:
    print("x is greater than y")
else:
    print("x and y must be equal")

../_images/flowchart_chained_conditional.png

shows a unary conditiona, a binary conditional, a conditional with if, elif, else, and a conditional with if, elif, and elif.

状況によってはifを繋げたらいい。

 

 

この結果は

x = -10
if x < 0:
    print("The negative number ",  x, " is not valid here.")
else:
    if x > 0:
        print(x, " is a positive number")
    else:
        print(x, " is 0")

 

このコードでは出ない

if x < 0:
    print("The negative number ",  x, " is not valid here.")
if (x > 0):
    print(x, " is a positive number")
else:
    print(x, " is 0")

最初のif statement がTrueの時、else もTrueになる。

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

#resps = []がここにあるとだめ。for loopが回るその都度リセットしないと!

for idn in range(len(percent_rain)):
resps = []
item_pr = percent_rain[idn]
if item_pr > 90:
resps = resps + [“Bring an umbrella.”]
elif 80 < item_pr <= 90:
resps = resps + [“Good for the flowers?”]
elif 50 < item_pr <= 80:
resps = resps + [“Watch out for clouds!”]
else:
resps = resps + [“Nice day!”]
print(item_pr, resps)

 

データ型がlistのオブジェクトに対してappendするとlistの中に入れてくれる。

スペースを除外してstringの文字数を数える方法!

stringは文字のsequenceです。

for _ in <string>でもOK.

欲しいitemの数を数えられる!あー、これ欲しかったわ。

同様に最大値最小値求められる。

 

listの中に真偽値がvalueとして入れる!

 

 

 

 

コメント