Get startedGet started for free

Conditional statements and operators

1. Conditional statements and operators

As developers, we'll need our code to make decisions such as when to show an error message. Python lets us build this decision-making into our code using comparison operators and conditional statements.

2. Booleans

Remember, we can work with booleans, which have values of true or false. These can be used to compare variables, data structures, and values!

3. Operators

We can make comparisons using comparison operators, symbols that compare values. The first comparison is checking equality. Since we use a single equals sign to assign variables, we use two equals signs to check if two things are equal.

4. Checking for equality

Here, we check if two equals three. We get False, as they're different. Alternatively, to check for inequality, we use an exclamation mark followed by an equals. This now returns True. As developers, we use equality checks all the time, like checking submitted login details against information stored in a user's account.

5. Numeric comparison operators

We can use a left arrow to check if one value is less than another. To check if it is less than or equal to another value, we also include an equals symbol. We can change the arrow direction to ask if a value is greater or equal to another.

6. Other comparisons

We mentioned that we can check for equality in strings, but what about asking Python if one string is greater than another? Surprisingly, this works! If we check whether "James" is greater than "Brian", Python returns True because J comes after B in the alphabet.

7. Conditional statements

Now that we can make comparisons, we can use them to control what our code does. This is one of the most important skills developers use, running different code based on different conditions. The if statement lets us say "if this is True, run this code; otherwise, skip it." For our recipe scaler, let's check if we have enough pasta before we continue. We start with the if keyword, then write our condition, checking if pasta_quantity is greater than or equal to required_quantity.

8. Conditional statements

We finish the line with a colon, which lets Python know that we are about to provide code to run if this condition is met.

9. Conditional statements

We put the action in a new indented line underneath using tab or four spaces. Python requires this indentation to know which code belongs to the if statement. If we have enough pasta, the print statement runs.

10. Indentation

Whenever we end a line with a colon Python expects an indentation in the following line. If we don't do this then we will get an error.

11. Elif statement

We can extend our workflow to check multiple conditions with elif, which is short for else if. This checks another condition only if the previous one was False. Here, if we don't have enough for the full recipe but have at least 300 grams, we can suggest making a smaller portion instead. We can use as many elif statements as we need.

12. Else statement

If none of our conditions are True, we use else as a catch-all for any remaining cases. This ensures we always give some kind of response.

13. Comparison operators cheat sheet

These conditional statements are fundamental tools for developers. We'll use them to check conditions, handle different situations, make decisions based on data, and control what our code does.

14. Let's practice!

If the video is complete then proceed to the exercises, else continue watching.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.