Python

Python Conditions

Operators

Before exploring Python conditions, it is important to comprehend the concept of operators. Operators are mathematical operations that perform interact with variables and values. They work well with conditional statements as well as loops (loops are covered in the following pages).

This page covers the most common operations, and they are:

  • Arithmetic operators

These are operators such as addition (+), subtraction (), multiplication (*), and division (/)

# Addition
x = 2
y = 6

print(x + y)
# Output 8

# Subtraction
x = 6
y = 2

print(x - y)
# Output 4

# Multiplication
x = 6
y = 4

print(x * y)
# Output 24

# Division
x = 12
y = 3

print(x / y)
# Output 4
  • Comparison operators

These are operators such as equal to (==), not equal to (!=), greater than (>), and less than (<).

# Equal to
x = 5
y = 5

print(x == y)
# Output True, as 5 equals 5

# Not equal to
x = 3
y = 8

print(x != y)
# Output True, as 3 does not equal 8

# Greater than
x = 7
y = 10

print(x > y)
# Output False, as 7 is not greater than 10

# Less than
x = 6
y = 9

print(x < y)
# Output True, as 6 is less than 9
python operators

Python conditions

Conditional statements are vital within the Python framework. They work well with operators.

Conditions are written with the following keywords:

  • if
  • if, else
  • if, elif, else

As the word suggests, Python code executes in the case a condition is met. Indentation is a must in conditional statements – please refer to the examples below. Colons are also necessary after each conditional statement.

If statements

In the instance of using only “if”, code executes an output in the case of truth. This means if the condition and the equation are both true, and vice versa, then Python executes.

x = 5
y = 1
if x > y:
    print(True)
# Output True

If else statements

The “else” statement follows the “if”, and if the first condition does not meet the requirement, “else” executes. In other words, “else” will execute anything that “if” does not. Refer to the example below.

x = 5
y = 1
if x < y:
    print("x is less than y")
else:
    print("x is greater than y")

# "if" will not execute as x is not less than y. Therefore, "else" executes.

If elif else statements

The “elif” statement adds more conditions. For instance, when “if” does not meet the condition and does not execute, the program checks the next condition, “elif”. In the case “elif” is also not acceptable, “else” executes.

The program starts from the beginning with “if” and follows down. Python does not limit the number of conditions, which means multiple “elif” statements can be added. Refer to the following example.

“Elif” is short for “else if”.

x = 5
y = 1

if x < y:
    print("x is less than y")
elif x == y:
    print("x is equal to y")
else:
    print("x is greater than y")

# The program will check the "if" statement, then "elif", and finally "else". X is not less than y, and x is not equal to y, therefore "else" executes.
python if-elif-else statement

Next: Python Loops

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.