Table of Contents[Hide][Show]
Welcome to the seventh lecture in our Python crash course series.
We will learn about the logical operators and Comparison Operators within Python. We use these operators in situations dealing with multiple conditions.
Logical Operators
AND Operator
Here is an example. Let’s say we are building an application for processing loans. If an applicant has a high income and good credit, they’re eligible for a loan.
This means we are dealing with two conditions here. The first condition is having a high income AND the second condition is having good credit. Both conditions must be true for the applicant to be eligible for a loan.
This is where we use the logical “AND” operator. By the way, this is not specific to python programming language, pretty much any programming language that supports “if statements” also supports the logical operators.
Back to our program, let’s define two variables:
has_high_income = True
has_good_credit = True
Now, let’s write our “if statement”.
if has_high_income and has_good_credit:
print("Eligible for loan")
The “AND” operator only outputs True if both of the conditions are true. If one of the conditions is false, we’re not going to see any output.
The program will look like this.
OR Operator
The “OR” operator can be used in a similar way. It gives the output if any of the conditions are true. Let’s try the above program with an OR operator.
has_high_income = True
has_good_credit = False
if has_high_income or has_good_credit:
print("Eligible for loan")
Now it will return the output if any of the above conditions are true. i.e. the applicant is eligible for a loan if either the income is high or credit is good.
AND/OR operator can be used simultaneously in the same if statement.
For example,
This program introduces a third condition of owning immovable property. The applicant will only be eligible for the loan if he owns the immovable property and either has a high income or good credit. It means the applicant must satisfy any one condition from the first two and the third condition is compulsory.
NOT Operator
NOT operator basically inverses any boolean value we give it. True becomes False and vice versa.
Let’s learn it by implementing it. We are now going to change some conditions in our previous program. The conditions to be eligible for the loan are good credit and no criminal record.
has_good_credit = True
has_criminal_record = False
if has_good_credit and not has_criminal_record:
print("Eligible for loan")
Let’s see it in action:
Now, that you have a fair understanding of how the operators work, let’s get onto the next big thing i.e. comparison operators.
Comparison Operators
We use comparison operators in situations where we want to compare a variable with a value. These are less than (<), greater than (>), equal to (==) etc. For example, if the temperature is greater than 30, then we want to print it’s a hot day. Otherwise, it’s cold. To build these rules into our program, we need to use comparison operators.
Starting it by introducing a variable “temperature”.
temperature = 25
if temperature > 30:
print("It's a hot day")
else if temperature
print("It's not a hot day")
Exercise
Get ready for a brain teaser. It will also check your memory from the past couple of lectures.
You have probably seen that when you fill out a form online, the input fields have validation messages, for example, let’s say we have an input field for the user to enter their name. Now if the name is less than 4 characters, we want to display a validation error that the name must be at least three characters. Otherwise, if the name is more than 50 characters long then we want to display a different validation error that the name can be a maximum of 50 characters.
Spoilers alert! solution coming ahead.
Solution
name = "Ron"
if len(name) < 4:
print("Name must be at least 3 characters")
elif len(name) > 50:
print("Name must be a maximum of 50 characters")
Wrap Up
That was all about the operators in Python. Now we will build some interesting projects and games in Python.
Leave a Reply