Logical Operators
If...else
Conditions
Python
There are 5 skills you need to master in your tool set for building algorithms with Python:
The use of conditionals is the easiest way we have to make decisions when coding our algorithms.
Let's say we are building a program to help us pick what to wear, but we hate the blue color. We could tell the computer to avoid that color using a statement like this:
1if color == 'blue': 2 # Do something 3else: 4 # Do something else or do nothing
👉 The
switch
statement is not available in Python.
Logical expressions let you conditionally skip a series of lines of your code. The best way to understand them is to think of them like questions you can ask the computer about your variables, for example:
if user_age > 21:
if day == "tuesday":
if car_model == "toyota" and number_of_tires == 6:
In order to ask a question or conditionally excuse a particular set of lines, you first need to have data (information) stored in useful variables. Above, we had the variables user_age
, day
, car_model
and number_of_tires
.
If we don't have the information pre-stored in variables, we are not able to ask any questions; it is all about strategy and planning!
For example, if we have the user's age stored in a variable age
then, and only then, we are able to code for something like:
1# We use two equal signs (==) when we want to compare two variables for equality 2if age == 21: 3 print("You are old enough!")
The previous example was a simple condition, but in real life, picking what to wear involves a combination of several conditions to make the final decision. For example: Let's look at this algorithm that tells you if you have the flu
If you want to represent this algorithm in Python, it will look something like this:
1feels_like_hit_by_train = True 2you_were_hit_by_train = False 3 4if feels_like_hit_by_train == True: 5 if you_were_hit_by_train == True: 6 print("You don't have the flu") 7 else: 8 print("You have the flu") 9else: 10 print("You don't have the flu")
Basically, this algorithm has two variables to consider: feels_like_hit_by_train
and you_were_hit_by_train
.
Our job as developers is to sit down, try to prepare a strategy, and come up with an algorithm that solves a problem.
Most of the questions can be asked using the following comparisons: ==
, >
, <
, !=
, is None
, is not None
, in
:
Operator | Example | Description |
---|---|---|
== | if a == b | if the value of variable a is equal to the value of b |
< | if a < b | if the value of variable a is less than the value of b |
> | if a > b | if the value of variable a is greater than the value of b |
!= | if a != b | if the value of variable a is different from the value of b |
is not None | if a is not None | This is self-explanatory, isn't it? |
is None | if a is None | This is self-explanatory, isn't it? |
in | if name in ['bob','maria','nancy'] | If the value of name is contained inside the list of names |
AND
& OR
Operators in PythonAnother way to write the algorithm is to combine questions in the same condition using the AND
and OR
:
1feels_like_hit_by_train = True 2you_were_hit_by_train = False 3 4if feels_like_hit_by_train and you_were_hit_by_train: 5 print("You don't have the flu") 6elif feels_like_hit_by_train: 7 print("You have the flu")
As you can see we are using elif
together for the first time, for faster coding. Another trick you can use for faster coding:
Original | Equivalent |
---|---|
instead of if(feels_like_hit_by_train == true) | you write if feels_like_hit_by_train |
instead of if(you_were_hit_by_train == false) | you write if not you_were_hit_by_train |
You can also use the else
expression to refer to the negation of the first condition:
1if color == "blue": 2 # Discard this clothing item 3else: 4 # Put it in your closet 5 6age = 12 7if age > 18: 8 print("You are an adult") 9else: 10 print("You are not an adult")
You can also nest several if...else conditions on top of one another, like this:
1if age < 16: 2 # You cannot do anything 3elif age < 18: 4 # At this point, we know it's older than 15 because if not, it would have entered into the first condition 5elif age < 21: 6 # If the algorithm enters here, we know it's older than 17 7else: 8 # If the algorithm enters here, we know it's older than 20
Here is another example that runs an algorithm to find out if a number is in the "hundreds".
1value = 14 2 3if value < 10: 4 print("single unit value") 5elif value < 100: 6 print("dozens") 7elif value < 1000: 8 print("hundreds") 9elif value < 10000: 10 print("thousands") 11else: 12 print("hundreds of thousands, or maybe more")
switch
statement in PythonPython does not have a switch
statement.
It's all about what question to ask. The previous example was a simple condition, but real life is not that simple. There are lots of nested conditions and complicated flows that will challenge your skills to the limit. For example:
This will be the algorithm for picking what to wear on Valentine's Day:
1if going_out: 2 if can_I_get_burger: 3 if place_bottle_wine: 4 if cool_mix: 5 # Do something 6 else: 7 if blazers > 3: 8 # Do something 9 else: 10 # Do something 11 elif she_pants: 12 # Do something 13 else: 14 # Do something 15else: 16 if naked_she_door: 17 # Do something 18 elif blazers > 3: 19 # Do something 20 else: 21 # Do something