Table of Contents

  • 1  Control Flow Statements

    • 1.1  if Statements

    • 1.2  if-else Statements

    • 1.3  if-elif Statements

    • 1.4  Nested if-else

If-else Conditions#

[2]:
import os
from IPython.core.display import HTML
def load_style(directory = '../', name='customMac.css'):
        styles = open(os.path.join(directory, name), 'r').read()
        return HTML(styles)
load_style()
[2]:

Control Flow Statements#

if Statements#

Perhaps the most well-known statement type is the if statement. For example:

if some_condition:
    algorithm
[6]:
x = 12
if x >10:
    print("Hello")
Hello

if-else Statements#

if some_condition:
    algorithm
else:
    algorithm
[8]:
x = 12
if x > 10:
    print("hello")
else:
    print("world")
hello

if-elif Statements#

if some_condition:
    algorithm
elif some_condition:
    algorithm
else:
    algorithm
[10]:
x = 10
y = 12

if x > y:
    print("x>y")
elif x < y:
    print("x<y")
else:
    print("x=y")
x<y

There can be zero or more elif parts, and the else part is optional. The keyword elif is short for else if, and is useful to avoid excessive indentation. An

if some_condition:
    algorithm
elif some_condition:
    algorithm
elif:
    algorithm

sequence is a substitute for the switch or case statements found in other languages.

Nested if-else#

if statement inside a if statement or if-elif or if-else are called as nested if statements.

[11]:
x = 11
y = 12
if x > y:
    print("x>y")
elif x < y:
    print("x<y")
    if x==10:
        print("x=10")
    else:
        print("invalid")
else:
    print("x=y")
x<y
invalid
[ ]: