1. Conditional statements and operators
Python offers vast functionality to make comparisons and conditionally perform tasks. Let's look at how we can do this!
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 do this using comparison operators. These are symbols or combinations of symbols
that compare things,
similar to symbols used to perform calculations.
The first comparison we can make is to check if two things are equal.
We use the equals symbol to assign a variable, so to check for equality we use two equals symbols.
4. Checking for equality
Here, we check if two is equal to three.
We get the boolean value of False, as they are different.
Alternatively, to check for inequality, we use an exclamation mark followed by an equals. This now returns True.
A common use case for equality comparisons is 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 confirms this is True. This is because strings are evaluated in alphabetical order, so because James starts with a J, it is greater than Brian, which starts with a B.
7. Conditional statements
Now we know how to conduct comparisons, we can extend this to build a workflow that conditionally perform tasks.
We can check if one value is equal to another and perform a task if this is True or do nothing if it is False.
We can use the if keyword to perform this.
Say we want to check if we've met the target for the number of units sold on an e-commerce website.
We start with the if keyword then check if units-sold is greater than or equal to the sales-target.
8. Conditional statements
We finish the line with a colon, which lets Python know that we are about to provide a task to perform if this condition is met.
9. Conditional statements
We put the task in a new line underneath. Notice that we indent, which is where we use a tab or four spaces.
As we exceeded our target, the print statement is executed!
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 perform an action if the condition following the if keyword is not met, such as in this case where we have a lower value for units-sold. We do this using the elif keyword, which is short for else if.
Here, if we haven't met our target but have sold at least 320 units then we print "Target almost achieved".
We can customize further by using as many elif keywords as we like!
12. Else statement
If the previous two conditions are not true then nothing will be printed, meaning there's only one option left.
If there's only one other action we want to take, then we use the else keyword, providing a print statement in an indented line underneath.
13. Comparison operators cheat sheet
You can use all operators and keywords shown in the video within conditional statements to build custom workflows!
14. Let's practice!
If the video is complete then proceed to the exercises, else continue watching.