Skip to content

Conditionals

The if statement

Create a file called conditionals.py:

touch conditionals.py

Show the syntax:

if EXPRESSION:
   code <- four white spaces

Add the following piece of code to the file:

hostname = 'r1'

if hostname == 'r1':
    print('Hostname correct')

print('Done')

Execute the file:

python conditionals.py

Change the hostname to r2 and execute the file again:

hostname = 'r2'
python conditionals.py

Modify the if statement and change the hostname to R1:

hostname = 'R1'

if hostname.lower() == 'r1':
    print('Hostname correct')

print('Done')

Execute the file again:

python conditionals.py

Add another variable and add another expression to the if statement:

hostname = 'R1'
platform = 'ios'

if hostname.lower() == 'r1' and platform == 'ios':
    print('Hostname and platform are correct')

print('Done')

Execute the file again:

python conditionals.py

Change the platform to nxos:

hostname = 'R1'
platform = 'nxos'

if hostname.lower() == 'r1' and platform == 'ios':
    print('Hostname and platform are correct')

print('Done')

Execute the file again:

python conditionals.py

The if-else statement

Update the if statement with else, also change platform to ios:

hostname = 'R1'
platform = 'ios'

if hostname.lower() == 'r1' and platform == 'ios':
    print('Hostname and platform are correct')
else:
    print('Parameters are not correct')

print('Done')

Execute the file:

python conditionals.py

Change platform to nxos:

platform = 'nxos'

Execute the file:

python conditionals.py

Comment everything:

# hostname = 'R1'
# platform = 'nxos'
#
# if hostname.lower() == 'r1' and platform == 'ios':
#     print('Hostname and platform are correct')
# else:
#     print('Parameters are not correct')
#
# print('Done')

The if-elif-else statement

Add the following to conditionals.py:

interface = 'Ethernet1/1'

if interface.lower().startswith('et'):
    itype = 'ethernet'
elif interface.lower().startswith('vl'):
    itype = 'vlan'
else:
    itype = 'unknown'

print(itype)

Execute the file:

python conditionals.py

Change interface to Vlan100:

interface = 'Vlan100'

Execute the file:

python conditionals.py

Update code with more elif statements:

interface = 'Vlan100'

if interface.lower().startswith('et'):
    itype = 'ethernet'
elif interface.lower().startswith('vl'):
    itype = 'vlan'
elif interface.lower().startswith('lo'):
    itype = 'loopback'
elif interface.lower().startswith('po'):
    itype = 'portchannel'
else:
    itype = 'unknown'

print(itype)

Change interface to Loopback100:

interface = 'Loopback100'

Execute the file:

python conditionals.py

Change interface to Management0:

interface = 'Management0'

Execute the file:

python conditionals.py

Comment everything:

# interface = 'Management0'
#
# if interface.lower().startswith('et'):
#     itype = 'ethernet'
# elif interface.lower().startswith('vl'):
#     itype = 'vlan'
# elif interface.lower().startswith('lo'):
#     itype = 'loopback'
# elif interface.lower().startswith('po'):
#     itype = 'portchannel'
# else:
#     itype = 'unknown'
#
# print(itype)

Nested conditionals

Add the following piece of code to conditionals.py:

vendor = 'cisco'
platform = 'nexus'
model = '9000'

if vendor == 'cisco':
    print('Vendor: {}'.format(vendor))
    if platform == 'nexus':
        print('Platform: {}'.format(platform))
        if model == '9000':
            print('Model: {}'.format(model))
        else:
            print('unknown model')
    else:
        print('unknown platform')
else:
    print('unknown vendor')

Execute the file:

python conditionals.py

Change model to 7000:

model = '7000'
Execute the file:
python conditionals.py

Change platform to ios:

platform = 'ios'
Execute the file:
python conditionals.py

Comment everything:

# vendor = 'cisco'
# platform = 'ios'
# model = '7000'
#
# if vendor == 'cisco':
#     print('Vendor: {}'.format(vendor))
#     if platform == 'nexus':
#         print('Platform: {}'.format(platform))
#         if model == '9000':
#             print('Model: {}'.format(model))
#         else:
#             print('unknown model')
#     else:
#         print('unknown platform')
# else:
#     print('unknown vendor')

Empty variables

Add the following:

variable = True

if variable:
    print('The result is True')
else:
    print('The result is False')

Execute the file:

python conditionals.py

Change variable to (ask them if they know the solution): * None * 'mystring' * '' * [1,2,3] * [] * {}

Change code:

variable = {'platform': 'nxos'}

if variable.get('platform'):
    print('The result is True')
else:
    print('The result is False')

Execute the file:

python conditionals.py

Change code:

variable = {'platform': 'nxos'}

if variable.get('model'):
    print('The result is True')
else:
    print('The result is False')

Execute the file:

python conditionals.py

Change code:

variable = {'platform': 'nxos'}

if variable.get('model', '9000'):
    print('The result is True')
else:
    print('The result is False')

Execute the file:

python conditionals.py