CS50 Python Week 1 Conditionals

CS50 Python Week 1 Conditionals

Harvard CS50 Python

·

4 min read

> # greater than
>= # greater than or equal to
< # less than
<= # less than or equal to
== # equal to
!= # not equal to

if

if - youtube

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

if - google slide

elif

elif - youtube

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")

elif - google slide

else

else - youtube

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")

else - google slide

or

or - youtube

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")

or - google slide

Not Equal

Not Equal - youtube

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")

Not Equal - google slide

and

and - youtube

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

Modulo - youtube

code parity.py
+
-
*
/
%
x = int(input("What's x?"))

if x % 2 == 0:
    print("Even")
else:
    print("Odd")
python parity.py

Boolean

Boolean - youtube

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

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

match - youtube

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?")