Understand how to correctly use IF with multiple conditions in Python and avoid getting a result you don't expect!
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
To receive the file(s) used in the class by email, fill in :
You will learn the fastest growing programming language in the world to create incredible automations, develop websites, perform data analysis, work with Data Science, Artificial Intelligence, even if you have never had any contact with Programming in your life.
Start now
Right arrow
Python icon used as backgroundThree images of Python course classes
IF with Multiple Conditions in Python – Don’t fall for this mistake!
In today's class I want to explain to you how to use IF with multiple line data conditions in Python correctly and avoid making errors or having unexpected results in your code.
When we work with conditionals in Python and want to make comparisons within it to check the information, we need to do it correctly. Bad syntax or an incorrect statement can lead to results that you didn't want.
To clarify everything about how to use IF with multiple conditions in Python, I prepared this class with practical examples so that you can understand once and for all how to use it correctly in your codes. So, download the available material and come with me!
Understanding the Error – Unexpected Results
To start this lesson, take a look at the code below and think about which result you expect it to print on the screen: “Access Granted” or “Access Denied” .
If we execute this code, we will get the message “Access Granted” as a result .
Running the code
If you thought the result should be “Access Denied” , don’t worry; this is a very common error, and I’ll explain why this code doesn’t work as you expected.
At first glance, it looks like we are comparing whether the variable name is equal to John, or equal to Paul, or equal to Marcus. However, that is not what is happening.
In practice, the code checks whether the variable name is equal to John, or whether the string “Paulo” is true, or whether the string “Marcus” is true. It is as if it were written as follows:
IF with Multiple Conditions in Python – Don’t fall for this mistake!
-
- Posts: 52
- Joined: Sun Dec 15, 2024 5:27 am