A review of the if statement pattern.
① No conditional branch ② One conditional branch. True or False ③ Two or more conditional branches. Processing is executed only under the specified conditions. ④ Two or more conditional branches. Also set the processing in case of false
If the conditions are not met, do nothing.
Pattern 1 (no conditional branching)
if A:
AAA
"A": Conditional expression "AAA": Processing when A holds
Example (no conditional branch, True)
A = 100
if A == 100:
print('A is 100.')
#output
#A is 100.
Example (no conditional branch, False)
A = 90
if A == 100:
print('A is 100.')
Pattern 2 (else)
if A:
AAA
else:
BBB
"A": Conditional expression "AAA": Processing when A holds "Else:": When A does not hold "BBB": Processing when A does not hold
Example (with conditional branching, else)
A = 90
if A == 100:
print('A is 100.')
else:
print('A is not 100.')
#output
#A is not 100.
Pattern 3 (elif)
if A:
AAA
elif B:
BBB
elif C:
CCC
"A": Conditional expression "AAA": Processing when A holds "Eli if B": Second conditional expression "BBB": Processing when B holds "El if C": Third conditional expression "CCC": Processing when C holds
Example (with conditional branching, elif, True)
A = 70
if A == 100:
print('A is 100.')
elif A == 80:
print('A is 80.')
elif A == 70:
print('A is 70.')
#output
#A is 70.
Example (multiple matching conditions)
A = 70
B = 100
if A == 100:
print('A is 100.')
elif B == 100:
print('B is 100.')
elif A == 70:
print('A is 70.')
#output
#B is 100.
It ends when the conditions are met from the top. └ Both "B == 100" and "A == 70" are True, but the process ends when the above "B == 100" becomes True.
Pattern 3 (elif, else)
if A:
AAA
elif B:
BBB
elif C:
CCC
else:
DDD
"A": Conditional expression "AAA": Processing when A holds "Eli if B": Second conditional expression "BBB": Processing when B holds "El if C": Third conditional expression "CCC": Processing when C holds "Else:": If none of the conditions are met "DDD": Processing when none of the conditions are met
Examples (elif, else)
A = 50
if A == 100:
print('A is 100.')
elif A >= 80:
print('A is over 80.')
elif A >= 70:
print('A is over 70.')
else:
print('A is 70 or less.')
#output
#A is 70 or less.
** ▼ There are two types of operators ** -Comparison of "comparison operator" elements -A combination of "Boolean operators" conditional expressions
Comparison operator | meaning |
---|---|
== | equal |
!= | Not equal |
> | Greater(Not included) |
>= | that's all(Including) |
< | Less than (not included) |
<= | Below (including) |
in | Including elements |
not in | Does not contain elements |
is | Objects are equal |
Boolean operator | Contents |
---|---|
and | And |
or | Or |
not | Not |
There are two equals. An error will occur if there are one or three or more.
(One equal is an assignment to a variable)
「=="equal
A = 100
if A == 100:
print('A is 100.')
else:
print('A is other than 100.')
#output
#A is 100.
When "!" Is added at the beginning, it means "not".
「!=Not equal
A = 100
if A != 100:
print('A is other than 100.')
else:
print('A is 100.')
#output
#A is 100.
Supplement: The name of "!" Exclamation mark, exclamation mark, surprise mark
③ Larger than ">"
A = 50
if A > 50:
print('A is greater than 50.')
else:
print('A is 50 or less.')
#output
#A is 50 or less.
「>="that's all(Including)
A = 50
if A >= 50:
print('A is greater than 50.')
else:
print('A is 50 or less.')
#output
#A is greater than 50.
Less than "<" (not included)
A = 50
if A < 50:
print('A is less than 50.')
else:
print('A is 50 or more.')
#output
#A is 50 or more.
「<=Below (including)
A = 50
if A <= 50:
print('A is 50 or less.')
else:
print('A is greater than 50.')
#output
#A is 50 or less.
if A in B
└ A is included in B.
Includes "in" element
A = 'pen'
B = 'I have a pen'
if A in B:
print('Yes')
else:
print('No')
#output
# Yes
Does not include "not in" element
A = 'I have a pen'
B = 'pen'
if B not in A:
print('Yes')
else:
print('No')
#output
# No
Stricter than "==".
"Is" objects are equal
A = 'I have a pen'
B = 'I have a pen'
if A is A:
print('Yes')
else:
print('No')
#output
# Yes
"Is" Comparison of different objects
A = 'I have a pen'
B = 'I have a pen'
if A is B:
print('Yes')
else:
print('No')
#output
# No
if A and B:
└ "A" "B" conditional expression
"And"
a = 1
b = 2
if a == 1 and b == 2:
print('a=1, b=2')
else:
print('a=1, b=Not 2')
#output
# a=1, b=2
"And" and example 2
a = 1
b = 2
c = 3
if a == 1 and b == 2 and c != 3:
print('a=1 and b=2 and 3 is other than 3')
else:
print('a=1 and b=2 and 3 is not other than 3')
#output
# a=1 and b=2 and 3 is not other than 3
"And"
a = 1
b = 2
c = 3
if a == 1 and b == 2 or c != 3:
print('a=1 and b=2 or 3 is other than 3')
else:
print('a=1 and b=2 or 3 is not other than 3')
#output
# a=1 and b=2 or 3 is other than 3
"And" and "or"
a = 1
b = 2
c = 3
if a == 100 and b == 200 or c != 3:
print('a=1 and b=2 or 3 is other than 3')
else:
print('a=1 and b=2 or 3 is not other than 3')
#output
# a=1 and b=2 or 3 is not other than 3
if not A:
└ Add to the beginning of the conditional expression
Not "not"
a = 100
b = 100
if not a == b:
print('a=not b')
else:
print('a=b')
#output
# a=b
「!=Will be the same as
a = 100
b = 100
if a != b:
print('a=not b')
else:
print('a=b')
#output
# a=b
> For statement here
Recommended Posts