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 3

Use conditionals and recursion to write a function palindrome(string), which should return True if the input string is a palindrome and False otherwise. Write the function in your lab1.py file. When you want to test your solution, enter the following command:


python3 autograder.py lab1.py