If statements¶

What we know so far¶

  • Expressions
  • Variables
  • Print Statements
  • Types

We can already do a lot with what we know so far. But what if we wanted to make something that is adaptable to specific situations?

Making Decisions: if, elif, and else¶

  • How do we compute absolute values?

if the value is negative, negate it

Otherwise (else), use the original value

In [7]:
x = -10
x < 0
Out[7]:
True
In [ ]:
 
In [14]:
x = -10

# calculate absolute value of x

if x > 0:
    print("Positive!")
else:
    print("Negative (or zero)!")

# print(result)
Negative (or zero)!
In [19]:
value = 42

if value > 0:
    b = True
print("Positive!")
Positive!
In [ ]:
if value > 0:
    print("Positive!")
In [22]:
b = value > 0
if b:
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    b = True
    print("Positive!")
    print("Negative!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
    print("Positive!")
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Negative!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
Positive!
In [24]:
value = -42
if value < 0:
    if value == -42:
        print("Negative!")
        print("And -42!")
Negative!
And -42!
In [26]:
value = -42
if value < 0:
    print("Negative!")
elif value == -42:
    print("And -42!")
Negative!

Try editing the above code to see if it works for any number! Here are some other ways of writing the same if statement

In [ ]:
x = -10

if x < 0:
    print(-x)
else:
    print(x)

What are some important components of the above if statements?

  • val < 0: This is called a condition and must be a boolean expression.
  • result = -x or print(-val): These lines of code are only run if the conditon is true. They must be indented from the if statement.
  • else: Code to run if the condition is False. This is not required.

Multiple if statements can be used inside each other. These are called nested if statements

In [28]:
x = 0

# calculate absolute value of x
if x < 0:
    print("x is negative")
    result = -x
# else:
#     if x == 0:
#         print("x is zero")
#         result = 0
#     else:
#         print("x is positive")
#         result = x

print(x)
print(result)
0
0

However, nested if statements can be unwieldy especially when you have a lot of conditions. In these cases, you can use elif to specify more conditions besides the initial if.

In [ ]:
x = 0

# calculate absolute value of val
if x < 0:
    print("x is negative")
    result = -x
elif x == 0:
    print("x is zero")
    result = 0
else:
    print("x is positive")
    result = x

print(x)
print(result)

Note that all of the elifs and else should follow the same indentation rules as the first if.

More Examples¶

In [ ]:
# Version 1

height = 72
if height > 100:
    print("space")
else:
    if height > 50:
        print("mesosphere")
    else:
        if height > 20:
            print("stratosphere")
        else:
            print("troposphere")
In [ ]:
# Version 2

if height > 50:
    if height > 100:
        print("space")
    else:
        print("mesosphere")
else:
    if height > 20:
        print("stratosphere")
    else:
        print("troposphere")
In [ ]:
# Version 3 (Best)

if height > 100:
    print("space")
elif height > 50:
    print("mesosphere")
elif height > 20:
    print("stratosphere")
else:
    print("troposphere")
In [30]:
# Broken Version 3

height = 75

if height > 20:
    print("stratosphere")
elif height > 50:
    print("mesosphere")
elif height > 100:
    print("space")
else:
    print("troposphere")
stratosphere

Why does height = 72 give different results?

In [ ]:
# Incomplete Version 3

if height > 20:
    print("stratosphere")
elif height > 50:
    print("mesosphere")
elif height > 100:
    print("space")

In this case it is possible that nothing is printed at all, when?

In [1]:
height = 101

if height > 100:
    print("space")


if height > 50:
    print("mesosphere")

    
if height > 20:
    print("stratosphere")
else:
    print("troposphere")
space
mesosphere
stratosphere

Now what happens when height is 72? Or when height is 101?