Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Concepts: Conditionals

check

Running different instructions based on a value.


Conditional Code Execution

In programming, a "conditional" is a code structure that runs a block of code depending on a certain condition being met.

We can apply this idea to normal life by considering something like the weather. If a person goes to the beach and plans to spend all day in the sun, its likely a good idea to wear some type of skin protectant. Conversely, if the same person instead decided to stream a season of their favorite show and avoid the sun at all costs, they would not need to protect their skin from the sun.

We can think of the above as setting up a condition to be met:

(exposure_to_sun == True)

And the code block to run if the condition is true:

> select an area of exposed skin
> apply skin protectant according to instructions
> when all exposed skin is protected
> discontinue application of protectant

This structure of a conditional statement combined with a block of code to run in the event that a certain condition is met is a very commonly used structure in computer programming.

One of the most common ways that conditionals are created is using if statements.

If we think back to our sun exposure example, the structure would become:

  • if: there will be exposure to the sun
    • do: properly apply sunscreen to exposed areas

Conditional statements are the foundations of making decisions in programming, and something that requires a good deal of practice. The first condtional statement that will be covered is if statements.

Please move on to the next exercise when you are ready to start if statements!