Python Conditionals

In Python, conditional statements consist of if, if-else, and if-elif-else:


if condition:
    do something
                    
if condition

if condition:
    do something
else:
    do something else
                    
if-elsecondition

if condition:
    do thing 1
elif condition2:
    do thing 2
else:
    do something else
                    
if-elif-else condition

Recursion!

Recursion is just as elegant in Python! Below is the well known Fibonacci function.


def fib(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib(n - 1) + fib(n - 2)