Conditionals¶
The if statement¶
Create a file called conditionals.py:
Show the syntax:
Add the following piece of code to the file:
Execute the file:
Change the hostname to r2 and execute the file again:
Modify the if statement and change the hostname to R1:
Execute the file again:
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:
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:
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:
Change platform to nxos:
Execute the file:
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:
Change interface to Vlan100:
Execute the file:
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:
Execute the file:
Change interface to Management0:
Execute the file:
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:
Change model to 7000:
Change platform to ios:
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:
Execute the file:
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:
Change code:
variable = {'platform': 'nxos'}
if variable.get('model'):
print('The result is True')
else:
print('The result is False')
Execute the file:
Change code:
variable = {'platform': 'nxos'}
if variable.get('model', '9000'):
print('The result is True')
else:
print('The result is False')
Execute the file: