4Geeks logo
About us

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Data Science and Machine Learning - 16 wks

Full-Stack Software Developer - 16w

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

← Back to Lessons
Edit on Github

Logical conditions in Python explained

Introduction to conditionals in Python
Logical Operators in Python
  • AND & OR Operators in Python

Introduction to conditionals in Python

There are 5 skills you need to master in your tool set for building algorithms with python:

  1. Variables.
  2. Conditionals.
  3. Lists (or arrays).
  4. Loops.
  5. Functions.

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

Condicionales

🔗 Using switch is not available in python.

What is a logical expression 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:

  1. if user_age > 21:
  2. if day == "tuesday"
  3. 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!!")

What type of conditions/questions can we use/ask?

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

Hit by car or have a 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.

Logical Operators in Python

Mostly all the questions can be asked using the following comparisons: ==, >, <, !=, is None, is not None, in:

OperatorExampleDescription
==if a == bif the value of variable A is identical to the value of B (same data-type and value)
<if a < bif the value of variable A is less than the value of B
>if a > bif the value of variable A is greater than the value of B
!=if a != bif the value of variable A is different (not identical) from the value of B
is not Noneif a is not NoneThis is self-explanatory, isn't it?
is Noneif a is NoneThis is self-explanatory, isn't it?
inif name in ['bob','maria','nancy']If the value of name is contained inside the list of names

AND & OR Operators in Python

Another 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:

OriginalEquivalent
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)

If...else in Python

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")

The switch statement in Python

Python does not have a switch statement.

Conclusion

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:

What to ware in 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