DEV Community

Vicki Langer
Vicki Langer

Posted on • Updated on

Charming the Python: Conditionals

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.


Conditionals

If

if condition:
    thisstuffwillhappen
Enter fullscreen mode Exit fullscreen mode
if 3 > 0:
    print('3 is more than 1')
    print('3 is a positive number')
Enter fullscreen mode Exit fullscreen mode

If-Else

number = 9
if number < 0:
    print('It is a positive number')
else:
    print('It is a negative number')
Enter fullscreen mode Exit fullscreen mode

If Elif Else

Elif allows you to run decisions through multiple levels of logic.

In our example, we want to play_ball_with_dog. Then we base our playing with the dog on the weather and other parameters.

Once a condition is fulfilled, the process stops. Forexample, if it's not nice_outside, we move on to decide if dog_is_napping. If dog_is_napping, we spot going through the decision process and skip_playing_ball. If dog is not napping, we move forward to see if it is raining. If it is raining we take_dog_to_indoor_dog_park. If it is not raining we move on to our last statement and choose to play_with_dog_inside

if nice_outside:
    play_ball_with_dog  # if condition isn’t met, stop here and `play_ball_with_dog`. If not, keep going
elif raining:
    take_dog_to_indoor_dog_park   # if condition isn’t met, stop here and `take_dog_to_indoor_dog_park`. If not, keep going
else:
    play_with_dog_inside   # if no other condition is met, end here and `play_with_dog_inside`.
Enter fullscreen mode Exit fullscreen mode

If numbers are more your thing, heres an example with grades.

if 90 > 100:
    print('Congrats, you got an A')
elif 80 < 89:
    print('Nice, you got a B')
elif 70 < 79:
    print('Okay, you got a C. What are you having trouble with?')
elif 65 < 69:
    print('Um, you got a D. What can I explain differently?')
else:
    print('Hmm, you got an F. Would a different environment be helpful?')
Enter fullscreen mode Exit fullscreen mode

Nested Conditions

Nesting jst means some code inside of other code. In this example, we first decide if you_have_a_dog. If you don't, we decide if you_have_a_cat. If you don't we decide if maybe_you_have_no_pet. However, if you_have_a_dog, we would've stopped and processed the nested code and decided if it's nice_outside and so on.

if you_have_a_dog:
    play_with_dog
    if nice_outside:
        play_ball_with_dog
    elif raining:
        take_dog_to_dog_park
    else:
        play_with_dog_inside
elif you_have_a_cat:
    play_with_cat
else maybe_you_have_no_pet:
    no_play_with_dog_or_cat
Enter fullscreen mode Exit fullscreen mode

If Condition and And Logical Operator

if raining and dog_is_napping:
    skip_playing_ball
Enter fullscreen mode Exit fullscreen mode

If and Or Logical Operator

if you_have_a_dog or you_have_a_cat:
    play_with_your_pet
Enter fullscreen mode Exit fullscreen mode

Series based on

Discussion (0)