Skip to content

Conditionals

In this lesson, we'll explore how to use conditional statements in Python to handle different scenarios. We'll start with a basic script that checks IP addresses against certain conditions and then progressively enhance it with new tasks and challenges.

Script: Conditional Logic with IP Addresses

This script demonstrates how to use if, elif, and else statements to check IP addresses against specific conditions.

Conditional Logic with IP Addresses
devices = [
    "192.168.1.12",
    "172.22.22.22",
    "10.9.8.7",
]

for ip in devices:
    if ip.startswith("192"):
        pass
    elif ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Explanation

  • Device List: A list of IP addresses to check.
  • Loop Through Devices: The script iterates over each IP address in the list.
  • Conditional Statements: Checks if the IP address starts with "192", ends with "2", or matches none of the conditions.

Tasks and Challenges

Task 1: Modify the elif to an if

Change the elif statement to an if statement and observe the behavior.

Change elif to if
for ip in devices:
    if ip.startswith("192"):
        pass
    if ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Task 2: Replace pass with continue

Replace the pass statement with a continue statement to skip the current iteration if the condition is met.

Replace pass with continue
for ip in devices:
    if ip.startswith("192"):
        continue
    if ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Task 3: Add a break Statement

Add a break statement after the print statement to exit the loop when a condition is met.

Add a break statement
for ip in devices:
    if ip.startswith("192"):
        continue
    if ip.endswith("2"):
        print(ip)
        break
    else:
        print("no match")

Task 4: Add a New Device

Add a new device to the list and see if it matches the conditions.

Add a new device
devices.append("172.16.0.1")

Task 5: Add a New Condition

Add a new condition to the if statement to match the new device.

Add a new condition
for ip in devices:
    if ip.startswith("192"):
        pass
    if ip.startswith("172"):
        print(f"Matched 172 network: {ip}")
    elif ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Task 6: Always Match Condition

Add a new condition that will always match.

Always match condition
for ip in devices:
    if ip.startswith("192"):
        continue
    if ip.startswith("172"):
        print(f"Matched 172 network: {ip}")
    if ip:
        print("This will always match")
        break
    elif ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Task 7: Never Match Condition

Add a new condition that will never match.

Never match condition
for ip in devices:
    if ip.startswith("192"):
        pass
    if ip.startswith("172"):
        print(f"Matched 172 network: {ip}")
    if ip.endswith("abracadabra"):
        print("This will never match")
    elif ip.endswith("2"):
        print(ip)
    else:
        print("no match")

Applying This in Context

How can you use this in the context of the Netmiko example? You can use conditional logic to handle different device types or configurations dynamically. E.g. executing specific commands based on the device model or providing different prompts for different devices. Another option is to provide default values, here is a small example:

device = {
    "device_type": "cisco_ios",
    "host": ""
}

hostname = input("Enter the device hostname: ")

if not hostname:
    device["host"] = "default-hostname"
else:
    device["host"] = hostname

ACL?

How can you use this in the context of the ACL example? Conditional logic can help in generating ACL entries based on specific criteria, making the script more flexible and powerful. E.g. matching IP addresses to specific networks.

Config Generation Script

How could this improve your config generation script? By using conditional logic, you can create more dynamic and adaptable configuration scripts that respond to different network scenarios and requirements. E.g. generating specific configurations based on device models or locations.

Summary

In this lesson, we explored how to enhance conditional logic in Python. By experimenting with different conditions and statements, you can create more flexible and powerful scripts.

Key Points

  • Conditional statements (if, elif, else) allow you to handle different scenarios in your code.
  • Using continue and break can control the flow of loops.
  • Adding new conditions helps in making your scripts more dynamic and adaptable.

Try It Yourself

Extend the script with additional conditions and see how it affects the output. Experiment with different IP addresses and conditions to understand the behavior of your code.

Congratulations on enhancing your Python skills with conditional logic! Keep practicing to become more proficient in writing efficient and maintainable code.