Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

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.

Search from all Lessons


LoginGet Started
← Back to Lessons
  • Logical Operators

  • If...else

  • Conditions

    Python

Edit on Github

Logical conditions in Python explained

Introduction to conditionals 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

Conditionals

👉 The switch statement 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 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!")

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

The flu algorithm

If you want to represent this algorithm in Python, it will look something like this:

Loading...

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.

Logical Operators in Python

Most of 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 equal to the value of b
<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 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:

Loading...

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 not 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("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".

Loading...

Change the value of the value variables to see what how it works with the different values.

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 for picking what to wear on Valentine's Day:

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