Python Operators for Decision-Making

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will practice using different types of operators in Python. Specifically, you will work with comparison operators, Boolean operators, and mixing operators. By the end of this lab, you will be able to use these operators to make decisions and perform calculations in your Python code.

Achievements

  • Python Comparison Operators
  • Python Boolean Operators
  • Python Mixing Operators

Comparison Operators

In this step, you will learn how to use comparison operators in Python. Comparison operators allow you to compare two values and determine whether they are equal, greater than, or less than each other.
To get started, open the terminal and Python interpreter:

python3

Then, try using the following comparison operators:

## Equal to
print(1 == 1)  ## True
print(1 == 2)  ## False

## Not equal to
print(1 != 1)  ## False
print(1 != 2)  ## True

## Greater than
print(1 > 2)  ## False
print(2 > 1)  ## True

## Less than
print(1 < 2)  ## True
print(2 < 1)  ## False

## Greater than or equal to
print(1 >= 2)  ## False
print(2 >= 1)  ## True
print(1 >= 1)  ## True

## Less than or equal to
print(1 <= 2)  ## True
print(2 <= 1)  ## False
print(1 <= 1)  ## True

You can use comparison operators to compare integers, floats, and strings. For example:

print(1.0 == 1)  ## True
print(1.5 > 1)  ## True
print("a" < "b")  ## True

Try using comparison operators to compare different values in Python. Can you determine which values are equal, greater than, or less than each other?

Boolean Operators

In this step, you will learn how to use Boolean operators in Python. Boolean operators allow you to combine multiple comparisons using logical and, or, and not.
To get started, try using the following Boolean operators:

## Logical and
print(True and True)  ## True
print(True and False)  ## False
print(False and True)  ## False
print(False and False)  ## False

## Logical or
print(True or True)  ## True
print(True or False)  ## True
print(False or True)  ## True
print(False or False)  ## False

## Logical not
print(not True)  ## False
print(not False)  ## True

You can use Boolean operators to combine multiple comparisons and create more complex conditions. For example:

## Check if x is greater than y and z is less than y
x = 5
y = 10
z = 15
print(x > y and z < y)  ## False

## Check if x is greater than y or z is less than y
print(x > y or z < y)  ## True

## Check if x is not equal to y
print(x != y)  ## True

Try using Boolean operators to combine multiple comparisons in Python. Can you create complex conditions that evaluate to True or False?

Mixing Operators

In this step, you will learn how to use mixing operators in Python. Mixing operators allow you to combine comparison and Boolean operators to create even more complex conditions.
To get started, try using the following mixing operators:

## Check if x is greater than y and less than or equal to z
x = 5
y = 10
z = 15
print(y < x <= z)  ## True

## Check if x is not greater than y and not less than or equal to z
print(not (y < x <= z))  ## False

You can use mixing operators to create conditions that are more difficult to understand at first glance. It is important to use parentheses to clearly indicate the order of operations.

## Check if x is greater than y and (z is less than y or z is equal to x)
x = 5
y = 10
z = 15
print(x > y and (z < y or z == x))  ## False

Try using mixing operators to create complex conditions in Python. Can you use comparison and Boolean operators together to create conditions that accurately reflect your desired logic?

Walrus Operator

The walrus operator, :=, is a new feature introduced in Python 3.8 and it allows you to assign values to variables within an expression. The operator works by using the same syntax as the assignment operator, =, but it appears on the left-hand side of an expression.

Here are a few code examples to show you how to use the walrus operator.

Example 1

## Finding the first even number in a list using the walrus operator
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for num in numbers:
    if (even := num % 2 == 0):
        print(f"The first even number is: {even}")
        break

## Output:
## The first even number is: True

This example demonstrates how to use the walrus operator to find the first even number in a list. The list numbers contains integers from 1 to 10. In the for loop, we iterate through the numbers list and use the walrus operator := to assign the result of the expression num % 2 == 0 to the variable even. If even is True, it means that num is an even number. In this case, the if statement is satisfied and the first even number is printed along with a message. The break statement is used to stop the loop when the first even number is found.

Example 2

## Using the walrus operator in a while loop
counter = 0
while (counter := counter + 1) <= 10:
    print(counter)

## Output:
## 1
## 2
## â€Ķâ€Ķ
## 10

In this example, we use the walrus operator in a while loop. The variable counter is initialized to 0 and the while loop continues to run as long as counter is less than or equal to 10. In each iteration of the loop, the walrus operator is used to update the value of counter by incrementing it by 1. The updated value of counter is then used in the next iteration of the loop. The loop prints the value of counter in each iteration, resulting in the numbers 1 to 10 being printed.

Example 3

## Using the walrus operator in a list comprehension
squared_numbers = [num * num for num in range(10) if (even := num % 2 == 0)]
print(squared_numbers)

## Output:
## [0, 4, 16, 36, 64]

In this example, we use the walrus operator in a list comprehension. The list comprehension is used to create a list of the squares of the even numbers from 0 to 9. The list comprehension consists of two parts: an expression num * num and a conditional clause if (even := num % 2 == 0). The expression num * num calculates the square of the current number in the loop num. The conditional clause uses the walrus operator to assign the result of the expression num % 2 == 0 to the variable even. If even is True, it means that num is an even number, and its square is included in the list squared_numbers. The list comprehension generates the list [0, 4, 16, 36, 64].

It's important to note that the walrus operator is intended for use in specific cases where you need to assign a value within an expression and use that same value multiple times. It is not meant to replace traditional assignment statements, and it's recommended to use it sparingly to improve code readability.

Summary

In this lab, you learned how to use comparison operators, Boolean operators, and mixing operators in Python. You can use these operators to make decisions and perform calculations in your code. Practice using these operators to become more proficient with them.

Other Python Tutorials you may like