この記事は、米ハーバード大学のCS50 Introduction to Programming with Pythonの Week 1 Conditionals のメモです。授業の中で、出てきたコードや情報などをまとめました。現在受講中の方、これから受ける方、復習などにお使いください。
Lecture 1 - Conditionals YouTube
Visual Studio Code for CS50
> # greater than
>= # greater than or equal to
< # less than
<= # less than or equal to
== # equal to
!= # not equal to
if
code compare.py
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
if x > y:
print("x is greater than y")
if x == y:
print("x is equal to y")
code compare.py
What's x? 1
What's y? 2
x is less than y
elif
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
else
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y:
print("x is less than y")
elif x > y:
print("x is greater than y")
else
print("x is equal to y")
or
x = int(input("What's x? "))
y = int(input("What's y? "))
if x < y or x > y:
print("x is not equal to y")
else:
print("x is equal to y")
Not Equal
x = int(input("What's x? "))
y = int(input("What's y? "))
if x != y:
print("x is not equal to y")
else:
print("x is equal to y")
x = int(input("What's x? "))
y = int(input("What's y? "))
if x == y:
print("x is equal to y")
else:
print("x is not equal to y")
and
code grade.py
score = int(input("Score: "))
if score >= 90 and score <= 100:
print("Greade: A")
elif score >= 80 and score < 90:
print("Greade: B")
elif score >= 70 and score < 80:
print("Greade: C")
elif score >= 60 and score < 70:
print("Greade: D")
else:
print("Greade: F")
score = int(input("Score: "))
if 90 <= score and score <= 100:
print("Greade: A")
elif 80 <= score and score < 90:
print("Greade: B")
elif 70 <= score and score < 80:
print("Greade: C")
elif 60 <= score and score < 70:
print("Greade: D")
else:
print("Greade: F")
Chaining Comparison Operators
Chaining Comparison Operators - youtube
score = int(input("Score: "))
if 90 <= score <= 100:
print("Greade: A")
elif 80 <= score < 90:
print("Greade: B")
elif 70 <= score < 80:
print("Greade: C")
elif 60 <= score < 70:
print("Greade: D")
else:
print("Greade: F")
score = int(input("Score: "))
if score >= 90:
print("Greade: A")
elif score >= 80:
print("Greade: B")
elif score >= 70:
print("Greade: C")
elif score >= 60:
print("Greade: D")
else:
print("Greade: F")
Modulo
code parity.py
+
-
*
/
%
x = int(input("What's x?"))
if x % 2 == 0:
print("Even")
else:
print("Odd")
python parity.py
Boolean
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
if n % 2 == 0:
return True
else:
return False
main()
Pythonic Expressions
Pythonic Expressions - youtube
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
return True if n % 2 == 0 else False
main()
def main():
x = int(input("What's x?"))
if is_even(x):
print("Even")
else:
print("Odd")
def is_even(n):
return n % 2 == 0
main()
match
code house.py
name = input("What's your name? ")
if name == "Harry":
print("Gryffindor")
elif name == "Hermione":
print("Gryffindor")
elif name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")
python house.py
What's your name? Harry
Gryffindor
name = input("What's your name? ")
if name == "Harry" or name = "Hermione" or name == "Ron":
print("Gryffindor")
elif name == "Draco":
print("Slytherin")
else:
print("Who?")
name = input("What's your name? ")
match name:
case "Harry":
print("Gryffindor")
case "Hermione":
print("Gryffindor")
case "Ron":
print("Gryffindor")
case "Draco":
print("slytherin")
case _:
print("Who?")
name = input("What's your name? ")
match name:
case "Harry" | "Hermione" | "Ron":
print("Gryffindor")
case "Draco":
print("slytherin")
case _:
print("Who?")