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)
                    

Exercise 4

Using conditionals and recursion write the palindrome(string) predicate function in Python, which should return True if the input string is a palindrome and False otherwise. Find Exercise 4 in the virus.py starter file and fill out the function definition. When you want to test your solution, enter the following command:


python3 virus.py palindrome
        

Congrats You've Finished Besides Blocks 1!!!

To confirm that you've completed all of the exercises enter the folowing command and show the output to your teacher:


python3 virus.py run_tests