The role of logical operators in control flow in Python

·

5 min read

Logical operators are an essential part of programming in Python, allowing developers to make decisions based on the values of different variables or conditions. In Python, the logical operators include and, or, and not. These operators can be used to evaluate multiple conditions and determine if a certain expression is true or false. By combining logical operators with conditional statements, developers can create complex programs that can perform a wide range of tasks. In this article, we will dive into the world of logical operators in Python, exploring how they work, how to use them effectively, and providing real-world examples of how they can be used in practice.

Truthy and Falsy values

In Python, individual values can evaluate to either True or False. Based on the kind of boolean they evaluate, we can then make decisions in our program. Values are classified by following these basic rules:

  • Values that evaluate to False are called Falsy

  • Values that evaluate to True are called Truthy

Falsy values

In Python, there are quite a few values that evaluate to False, however, I am going to focus more on the most common ones.

These values are:

  • Empty strings: ""

  • Integer zero: 0

  • Float zero: 0.0

  • False

Let's analyze an example of a program containing a Falsy value:

a = 0
if a:
    print("a is Truthy")
else: 
    print("a is Falsy")
# Output => a is Falsy

In this program, we stored the value 0 in the variable a, which is a Falsy value. When the if statement checks the value of a, the return value is False, therefore the code in the else block is executed.

Truthy values

In Python, the truthy values include:

  • Non-empty strings

  • Numeric values that are different from 0

  • True

Here is an example:

a = 5
if a:
    print("a is Truthy")
else:
    print("a is Falsy")
# Output => a is Truthy

In this example, we stored the value 5 in the variable a. Since a contains a numeric value that is different from 0, therefore it is a Truthy value. When the if statement checks the value of a, the return value is True, therefore the code in the if block is executed.

bool()

In Python, there is a built-in function that checks whether a value is Truthy or Falsy. To do this, you just need to pass the value as an argument:

bool(5) # Output => True
bool(0.0) # Output => False
bool("John") # Output => True
bool("") # Output => False

and operator

The and operator returns True only if both the left operand and the right operand evaluate to True. Here is the syntax:

a and b

Oftentimes you will find the and operator in conditional statements, to perform decisions in the program. The and operator returns a boolean value following the scheme below:

aba and b
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

The and operator comes useful in conditional statements. Here is an example of how you could implement the and operator in your programs:

temperature = 25
weather = "sunny"
if temperature > 20 and weather == "sunny":
    print("I am going out")
else:
    print("I am staying at home")
# Output => I am going out

In the example above, the left operand consists in checking whether the value stored inside the temperature variable is greater than 20, while the right operand consists in checking whether the value stored inside the weather variable matches the word sunny. In this case, both operands evaluate to True, therefore the code in the if block is executed.

or operator

The or operator returns True if at least one operand evaluates to True. Here is the syntax:

a or b

Oftentimes you will find the or operator in conditional statements, to perform decisions in the program. The or operator returns a boolean value following the scheme below:

aba or b
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

The or operator comes useful in conditional statements. Here is an example of how you could implement the or operator in your programs:

temperature = 25
weather = "cloudy"
if temperature > 25 or weather == "sunny":
    print("I am going out")
else:
    print("I am staying home")
# Output => I am going out

In the example above, the left operand consists in checking whether the value stored inside the temperature variable is greater than 20, while the right operand consists in checking whether the value stored inside the weather variable matches the word sunny. In this case, the left operand evaluates to True, meanwhile, the right operand evaluates to False. However, the or operator returns True if either of the operands is True, therefore the code in the if block is executed.

not operator

The not operator works with one operand. The not operator returns False if the operand evaluates to True and returns True if the operand evaluates to False.

not a

Oftentimes you will find the not operator in conditional statements, to perform decisions in the program. The not operator returns a boolean value following the scheme below:

anot a
TrueFalse
FalseTrue

The not operator comes useful in conditional statements. Here is an example of how you could implement the not operator in your programs:

a = 0
if not a:
    print("a is Falsy")
else:
    print("a is Truthy")
# Output => a is Falsy

In the example above, the a variable is a Falsy value, since it contains the value 0. When the if statement checks the condition, however, the not operator returns True. The code in the if statement is therefore executed.

Conclusion

In conclusion, logical operators are an essential tool for working with conditional statements and control flow in Python. By using operators such as and, or, and not, developers can create complex expressions that evaluate multiple conditions and determine the appropriate course of action.