&& (Logical-and) In an If-Statement In Python

How To Use Logical And In Python

When you start programming you usually start with some low-level languages like C or C++. Then later when you move to different languages you just learn it relative to C. Python is a very simple language. The syntax of Python is not complicated at all like C or C++. But as we started with C or C++, we got habituated to those syntaxes. In this article, let’s explore what is Python equivalent to && and how to use it.

Before starting with the logical and operator, we have to understand what If statements are.

If Statement

If statements are a way to write conditional statements in code. In Python, we write conditional statements using the keyword if. The syntax for the if statement is very simple in Python.

if <condition>:
        #Code to run if the condition satisfies

We write the keyword ‘if’ and next to it we write the condition we want to check to run the following code block. We end the line with a colon and start with an indentation (leaving a tab in the start) from the next line specifying the following lines are to be executed if the condition is True. If the condition is False, the interpreter just skips over this code block.

Example for If statement in Python

name1 = 'Kundan'
if name1 == 'Kundan':
    print("Hello Kundan!")
if name1 == 'Rohan':
    print("Hello Rohan!")
Code And Output For If Statement
Code And Output For If Statement

In the above code, we initialized the variable name1 with ‘Kundan’. Then we wrote an ‘if’ statement stating that if name1 is equivalent to the string ‘Kundan’. As name1 stores the value ‘Kundan’, the if statement comes out to be True. So the interpreter executes the following indented line of code which says to print the string “Hello Kundan”. Now we did the same thing for ‘Rohan’. We wrote an if statement stating that if name1 is equivalent to the string ‘Rohan’. As name1 stores the value ‘Kundan’, this time, the if statement comes out to be False and the interpreter skips over the following indented block of code.

Related: If statement in Python.

Learning conditional statements is also necessary for while loops.

What is logical-and?

Now imagine you want to check if two conditions are True at the same time to execute a block of code. You can for sure write two separate if statements nesting one in the other but there’s an easy way to do it. Let’s try to understand this case with help of an example.

Suppose you have two variables, name1, and name2.

name1 = "Kundan"
name2 = "Rohan"

Now, you want the interpreter to check if name1 is “Kundan” and name2 is “Rohan” only then print “Hello Kundan And Rohan”. We concatenate these two separate conditions with and. In most languages, we use && to concatenate two separate conditions. But Python has this ‘and’ keyword for this purpose. You can simply use the and keyword to concatenate two separate conditions.

name1 = 'Kundan'
name2 = 'Rohan'
if name1 == 'Kundan' and name2 == 'Rohan':
    print("Hello Kundan and Rohan")
Code And Output For Logical And In If Statement
Code And Output For Logical And In If Statement

In the above code, we used the logical ‘and’ operator to check if both name1 == ‘Kundan’ and name2 == ‘Rohan’. If both conditions are true then execute the indented block. As both name1 is ‘Kundan’ and name2 is ‘Rohan’ the condition results in a True value. Therefore, the interpreter executes the next line saying print the string “Hello Kundan and Rohan”.

The truth table for logical-AND

The truth table is a way to represent the truth values of a logical expression. We can determine if the resultant value of an expression will be True or False using the truth table. A lot of people have trouble determining when the if statement will run and when it will not whenever logical-‘and’ comes to the scene. Therefore, it is important to study the truth table of logical-and.

TRUTH TABLE FOR LOGICAL AND

A is the first condition and B is the second condition. AB represents logical- and of both conditions. 0 represents a False value in the truth table and subsequently, 1 represents a True value.

In the above truth table, we can see when both A and B are true then R will be True and for all other cases, AB will be False. If you’re confused about why is it, let’s try to understand this with help of a simple example. Suppose Ram says, “If it rains tomorrow and there’s a holiday tomorrow, I won’t come to school”. The statement consists of two major parts: a condition which is “If it rains tomorrow and there’s a holiday tomorrow” and an outcome which is “Ram won’t go to school”. In this scenario, let’s check in which case Ram misses school tomorrow.

Let’s break the condition into two parts.

  • A: It rains tomorrow.
  • B: There’s a holiday tomorrow.

Ram means that if both the conditions are True, then only he won’t go to school. Else, he might. So for Ram to miss school tomorrow, both A and B need to be True else we can’t be sure if he will or he won’t go to school. So the value of AB is True only when A is True and B is True and for all the other cases it is False.

Logical-OR

Logical or just like logical and is a logical operator used to form complex logical conditional statements. The keyword for logical-or in Python is “or”. It’s important to understand logical-or as well with logical-and to get a complete grasp of the concept.

Let’s try to understand it with help of an example.

name1 = 'Kundan'
name2 = 'Rahul'
if name1 == 'Kundan' or name2 == 'Kundan':
    print("Hello Kundan!")
Code And Output For Logical Or
Code And Output For Logical Or

In the above code, we used the logical or operator to check if either name1 == ‘Kundan’ or name2 == ‘Kundan’. If any condition is true then execute the indented block. As name1 is ‘Kundan’ the condition results in a True value. Therefore, the interpreter executes the next line saying print the string “Hello Kundan!”.

Truth table for logical-OR

TRUTH TABLE FOR LOGICAL OR

Again, A is the first condition and B is the second condition. AB represents logical- and of both conditions. 0 represents a False value in the truth table and subsequently, 1 represents a True value.

In the above truth table, we can see when both A and B are true then R will be True and for all other cases, AB will be True. If you’re confused about why is it, let’s check a similar example as before. Suppose Ram says, “If it rains tomorrow or there’s a holiday tomorrow, I won’t come to school”. The statement consists of two major parts: a condition which is “If it rains tomorrow and there’s a holiday tomorrow” and outcome which is “Ram won’t go to school”. In this scenario, let’s check in which case Ram misses school tomorrow.

Again, let’s break the condition into two parts.

  • A: It rains tomorrow.
  • B: There’s a holiday tomorrow.

Here Ram means that if any one of the two conditions is True, then he won’t go to school. Else, he might. So for Ram to miss school tomorrow, either A or B needs to be True else we can’t be sure if he will or he won’t go to school. So the value AB is True when either A is True or B is True and it is False when both are False.

Conclusion

On the final note, logical-and is a very powerful tool and can be used to write complex conditional statements in a single line with the help of logical-or. Keep practicing it a lot. It will increase your programming efficiency if you use it properly.

References

Logical operators

Stack Overflow