Python If-Else Statements


In Python, if/else statements are used to control the flow of a program based on certain conditions.

  • enables execution of different blocks of code depending on whether a given condition is true or false

Syntax:

if condition:
    # code to execute if the condition is true
else:
    # code to execute if the condition is false

Example

# Example: Check device status
 
device_status = "online"
 
# Check if the device is online
if device_status == "online":
    print("Device is online. Proceed with configuration.")
else:
    print("Device is offline. Unable to perform configuration.")
 
# Output will be "Device is online. Proceed with configuration."

Branches

A branch is a set of statements that are only executed when certain conditions are met.

  • in flowcharts, a decision creates two branches
    • if the decision is true, then first branch executes
    • if the decision is false, the second branch executes
    • once the decision is made, the branches rejoin

Pseudocode

Pseudocode is a high-level description of an algorithm or a program written in a way that is easy for humans to understand.

Creating If Statements

Conditions and If Statements

Conditions in Python are expressions that evaluate to either True or False.

  • e.g.,
    • x == y (Is x equal to y?)
    • x < y (Is x less than y?)
    • x > y (Is x greater than y?)
    • x != y (Is x not equal to y?)

 If statements are used to conditionally execute a block of code based on the evaluation of a specified condition.

if condition:
    # Code to be executed if the condition is true

elif Keyword

In Python, elif is a keyword that stands for “else if”.

  • allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True

else Keyword

else is a keyword that is used in control flow structures (ifforwhile) to specify a block of code that should be executed if the condition(s) in the control structure is not met.

x = 20
 
if x < 10:
    print("x is less than 10")
elif x < 30:
    print("x is less than 30 but not less than 10")
else:
    print("x is 30 or more")

Conditional Expressions

and Logical Operator

The and keyword in Python is a logical operator used in conditional statements.

  • returns True if both the conditions are True, else False
ip_address = "192.168.1.1"
first_octet = int(ip_address.split(".")[0])
 
if first_octet >= 1 and first_octet <= 126:
    print(f"{ip_address} is a Class A IP address.")
else:
    print(f"{ip_address} is not a Class A IP address.")

or Logical Operator

The or keyword in Python is a logical operator used in conditional statements.

  • returns True if one or both are True, else False
ip_address = "192.168.1.1"
first_octet = int(ip_address.split(".")[0])
 
if first_octet == 10 or first_octet == 172 or first_octet == 192:
    print(f"{ip_address} is a private IP address.")
else:
    print(f"{ip_address} is not a private IP address.")

not Logical Operator

The not keyword in Python is a logical operator used in conditional statements.

  • returns True if the condition is False and False if condition is True
  • reverses the value of the condition
ip_address = "192.168.1.1"
first_octet = int(ip_address.split(".")[0])
 
if not first_octet == 127:
    print(f"{ip_address} is not a loopback IP address.")
else:
    print(f"{ip_address} is a loopback IP address.")

Shorthand if/else Statement

A shorthand if statement is a way to write an if/else statement in a single line in Python.

  • aka one-liner if statement or a ternary operator
  • syntax: value_if_true if condition else value_if_false
# Assume there is a function `is_device_up(device_ip)` that returns True if the device is up and False if it's down.
 
device_ip = "192.168.1.1"  # IP address of the network device
 
status = "up" if is_device_up(device_ip) else "down"
print(f"Device {device_ip} is {status}.")