Skip to main content

Week 1: Conditionals

·664 words·4 mins·
Table of Contents

Conditionals
#

  • Ability to ask and answer questions

Comparison Operators in Python
#

SymbolMeaning
>Greater than
<Less than
==Equal to
!=Not equal to
>=Greater than or equal to
<=Less than or equal to

if
#

Example of a code using if
#

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")
  • In this example “x < y” is a boolean expression

Flowchart of the code
#

Flowchart image

elif
#

Example of a code using 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")

Flowchart of the code
#

Flowchart image

else
#

Example of a code using 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")

Flowchart of the code
#

Flowchart image

or
#

Example of a code using 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")

Flowchart of the code
#

Flowchart image

Not Equal
#

Example of a code using 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")

Flowchart of the code
#

Flowchart image

Example of a code using equal
#

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

Flowchart of the code
#

Flowchart image

Identation, Colons
#

  • Identation is important in Python, if you don’t use it, you will get an error

  • The colon is also important, it tells Python that the next line is going to be a block of code

and
#

Example of a code using and
#

score = int(input("Score: "))

if score >= 90 and score <= 100:
    print("Grade: A")
elif score >= 80 and score < 90:
    print("Grade: B")
elif score >= 70 and score < 80:
    print("Grade: C")
elif score >= 60 and score < 70:
    print("Grade: D")
else:
    print("Grade: F")

Chaining Comparison Operators
#

Example of a code using chaining comparison operators
#

score = int(input("Score: "))

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Bugs
#

Example of a code with a bug: using if instead of elif
#

score = int(input("Score: "))

if score >= 90:
    print("Grade: A")
if score >= 80:
    print("Grade: B")
if score >= 70:
    print("Grade: C")
if score >= 60:
    print("Grade: D")

Modulo
#

Some remaining operators
#

SymbolMeaning
+Addition
-Subtraction
*Multiplication
/Division
%Modulo (Remainder)

Example of a code using modulo
#

x = int(input("What's x? "))

if x % 2 == 0:
    print("x is Even")
else:
    print("x is Odd")

Boolean
#

Example of a code using boolean
#

def main():
    x = int(input("What's x? "))
    y = int(input("What's y? "))

    if is_even(x):
        print("x is Even")
    else:
        print("x is Odd")


def is_even(n):
    if n % 2 == 0:
        return True
    else:
        return False


main()

Pythonic Expressions
#

Example of a code using Pythonic Expressions
#

def main():
    x = int(input("What's x? "))
    y = int(input("What's y? "))

    if is_even(x):
        print("x is Even")
    else:
        print("x is Odd")


def is_even(n):
    return n % 2 == 0


main()

match
#

Example of a code using match
#

name = input("What's your name? ")

match name:
    case "Harry" | "Herminone" | "Ron":
        print("Gryffindor")
    case  "Draco:":
        print("Slytherin")
    case _:
        print("Who?")
Gael Mora
Author
Gael Mora
IT Security student, Python and Go developer. Specialized in Linux systems administration and automation. Passionate about cloud and network infrastructure, software development and open source technologies.