Conditionals

If statement

Here are a few things to notice about the if statement:

  • The if statement starts with if, it continues with price == 0.0 and it ends with :.

  • Use the == operator to check if the price is equal to 0.0. Don't confuse == with = (= is a variable assignment operator in Python; we use it to assign values to variables — it doesn't tell us anything about equality).

  • Indent ratings.append(rating) four spaces to the right relative to the if statement.

Multiple conditions

we can combine two or more conditions together into a single if statement using the and keyword. In the two diagrams below, we can use and to simultaneously check whether an app is both free and has a gaming genre.

Notice above that code like app_1_price == 0 and app_1_genre == 'Games' outputs a single Boolean value.

Using or in conditions

else statement

The code within the body of an else clause executes only if the if statement that precedes it resolves to False

else if (elif)

The code within the body of an elif clause executes only under the following circumstances:

  • The preceding if statement (or the other preceding elif clauses) resolves to False; and

  • The condition specified after the elif keyword evaluates to True.

Comparison Operators

Last updated