Menu
Coddy logo textTech

If and Else

Lesson 18 of 19 in Coddy's Into the Past || Complete Beginner for Python Strings course.

We have looked at testing and returning True and False answers.  In Python these are how actions are controlled.  The next part to this control are two more keywords:

if

else

The if keyword handles the True answers to an operation test.

The else keyword handles the False answers. See below:

def my_test(name): 
    if name[0] == "S":
        return "The name starts with an S"
    else:
        return "The name does not start with an S"

As you can see there is an indented part under if and else.  The way use read is if the argument name's first letter a capital S.

return "The name starts with an S"

Else (if not or False)

return "The name does not start with an S"

***if lines have to be True to enact the indented section of code

challenge icon

Challenge

Easy

Now run the code that is given to you in the code editor and see for yourself

Try it yourself

def my_test(name): 
    if name[0] == "S":
        return "The name starts with an S"
    else:
        return "The name does not start with an S"

All lessons in Into the Past || Complete Beginner for Python Strings