Flow

Sometimes the code that is running behaves like walking along a path. At some point, there could be a branch, and you can make a decision about which path to take. If it is cold, then maybe the path that is in the sun would be a good choice.

The following code shows the idea of branching.

The lines following the "if" that are spaced in, are only run when the if line above them is really true. Spaces are important in Python. The lines that have more space at the beginning than the "if" are called "indented". The "if" makes a decision about whether to run the following lines that are indented or not.

The key '>' means "greater than" or for positive numbers, means larger than.

Anything on a line after a '#' is a comment and is not run.

This diagram shows the path in the code when boxA is set equal to 3. The check for "boxA > 2" is true because boxA is 3.

When the value of boxA is set to 10, then the path taken is shown here.

What happens when boxA is set to 1?

Other Checks

The check to see if something is equal to something else is with "==". A single "=" means "set", and a double "=" means "is equal?"

A "!" means "not". So "!=" means "is it not equal?"

Here is a table of the checks.

KeyMeaning
>is greater than
<is less than
=is equal
!=is not equal
>is greater or equal
<is less or equal

Else

The "else" word can be used after an "if".

Elif

There is also "elif", which means "else if".

If Inside If

The "if" word can be used to check multiple path choices in a row.

Notice that if boxA is not two, then the check for boxB cannot happen because it was skipped since it is indented compared to the check for boxA.

Multiple Checks

There is a way to check two things at once using the word "and".

There is a way to check two things at once using the word "or".