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
🔗 Using
switch
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 on 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 a flu") 7 else: 8 print("You have a flu") 9else: 10 print("You don't have a 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 and try to prepare a strategy and come up with an algorithm that solves a problem.
Mostly all 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 identical to the value of B (same data-type and value) |
< | 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 (not identical) 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 a flu") 6elif feels_like_hit_by_train: 7 print("You have a 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(!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("Old enough") 9else: 10 print("Not old enough")
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 5 # into the first condition 6elif age < 21: 7 # If the algorithm enters here, we know its older than 17 8else: 9 # If the algorithm enters here, we know its 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("hundrends 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 to pick what to wear on Valentine's Day:
1if going_out: 2 if can_I_get_burger: 3 if place_bottle_white: 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