28.1.2 Lab - Construct a Basic Python Script (1)
28.1.2 Lab - Construct a Basic Python Script (1)
Objectives
Part 1: Explore the Python Interpreter
Part 2: Explore Data Types, Variables, and Conversions
Part 3: Explore Lists and Dictionaries
Part 4: Explore User Input
Part 5: Explore If Functions and Loops
Part 6: Explore File Access
Background / Scenario
In this lab, you will learn basic programming concepts using the Python programming language. You will
create a variety of Python scripts. The lab culminates in a challenge activity in which you use many of the
skills learned in this lab to construct a Python script.
Required Resources
1 PC (Choice of operating system with Cisco Networking Academy CCNP in a virtual machine client)
Internet access (Optional)
Instructions
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 15 www.netacad.com
Lab - Construct a Basic Python Script
Enter some math operations using the Python syntax, as shown below.
>>> 2+3
5
>>> 10-4
6
>>> 2*4
8
>>> 20/5
4.0
>>> 3**2
9
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 15 www.netacad.com
Lab - Construct a Basic Python Script
IDLE Shell
IDLE Editor
The IDLE Shell provides an interactive interpreter with colorizing of code input, output, and error messages. It
also includes a popup that provides syntactical help for the command you are currently using, as shown in the
figure.
The IDLE Editor provides a text editor with code colorization and syntactical help for writing python scripts.
To open the IDLE Editor, in the IDLE Shell, click File > New File, as shown in the figure.
The IDLE Editor includes the ability to immediately test a script in the shell by using the Run Module (F5)
command, as shown in the figure.
Step 6: Use IDLE to write, save, and run your first program.
Complete the following steps in IDLE:
a. In IDLE shell, click File > New File (Ctrl+N) to open an Untitled script file.
b. Save the file as 01_hello-world.py.
c. Enter the following in the script: print("Hello World!").
d. Save the script; click File > Save (Ctrl+S).
e. Run the script; click Run > Run Module (F5).
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 15 www.netacad.com
Lab - Construct a Basic Python Script
>>> type(98)
<class 'int'>
>>> type(98.6)
<class 'float'>
>>> type("Hi!")
<class 'str'>
>>> type(True)
<class 'bool'>
Operator Meaning
In the IDLE shell, try out the different Boolean operators. The following are a few examples.
>>> 1<2
True
>>> 1>2
False
>>> 1==1
True
>>> 1!=1
False
>>> 1>=1
True
>>> 1<=1
True
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 15 www.netacad.com
Lab - Construct a Basic Python Script
>>> str3="Academy"
>>> space=" "
>>> print(str1+space+str2+space+str3)
Cisco Networking Academy
Challenge: Try writing a print() statement with a space between the words without using a variable to create
the space.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 15 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 15 www.netacad.com
Lab - Construct a Basic Python Script
<class 'dict'>
b. Unlike list items, objects inside a dictionary cannot be referenced by their sequence number. Instead, you
reference a dictionary object using its key, as shown in the following example:
1) The key is enclosed with brackets [ ].
2) Keys that are strings can be referenced using single or double quotes, as shown for R1 and S1 in
Example 2.
3) Add a key/value pair by setting the new key equal to a value.
4) Use a key in dictionary statement to verify if a key exists in the dictionary.
>>> ipAddress
{'R1': '10.1.1.1', 'R2': '10.2.2.1', 'R3': '10.3.3.1'}
>>> ipAddress["R1"]
'10.1.1.1’
>>> ipAddress["S1"]="10.1.1.10"
>>> ipAddress
{'R1': '10.1.1.1', 'R2': '10.2.2.1', 'R3': '10.3.3.1', 'S1': '10.1.1.10'}
>>> "R3" in ipAddress
True
c. Values in a key/value pair can be any other data type including lists and dictionaries. For example, if R3
has more than one IP address, how would you represent that inside the ipAddress dictionary? Create a
list for the value of the R3 key, as shown in the following example:
>>> ipAddress["R3"]=["10.3.3.1","10.3.3.2","10.3.3.3"]
>>> ipAddress
{'S1': '10.1.1.10', 'R2': '10.2.2.1', 'R1': '10.1.1.1', 'R3': ['10.3.3.1',
'10.3.3.2', '10.3.3.3']}
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 15 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 15 www.netacad.com
Lab - Construct a Basic Python Script
3) Run the script and troubleshoot any errors. Your output should look like the following example:
========== RESTART: /home/Student/04_if-vlan.py ==========
The native VLAN and the data VLAN are different.
b. Modify the variables so that nativeVLAN and dataVLAN have the same value. Save and run the script
again. Your output should look like the following example:
========== RESTART: /home/Student/04_if-vlan.py ==========
The native VLAN and the data VLAN are the same.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 15 www.netacad.com
Lab - Construct a Basic Python Script
R1
R2
R3
S1
S2
b. What if you only want to list the items that begin with the letter R? An if statement can be embedded in a
for loop to achieve this. Enter the following example in your interactive interpreter:
>>> for item in devices:
if "R" in item:
print(item)
R1
R2
R3
c. You can also use a combination of the for loop and if statement to create a new list. The following shows
how to use the append() method to create a new list called switches. In your interactive interpreter,
create an empty list variable for switches. Then iterate through devices to find and add any device that
begin with an “S” to the switches list.
>>> switches=[]
>>> for item in devices:
if "S" in item:
switches.append(item)
>>> switches
['S1', 'S2']
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 15 www.netacad.com
Lab - Construct a Basic Python Script
Note: If you need to interrupt an endless loop, you can press Ctrl-C, Ctrl-F6, or Shell > Restart Shell from
the Shell.
a. Create this script for your files:
1) Open a blank script and save it as 06_while-loop.py.
2) Create a program with a while loop that counts to a user’s supplied number.
(i) Convert the string to an integer: x = int(x).
(ii) Set a variable to start the count: y = 1.
(iii) While y <= x, print the value of y and increment y by 1.
x=input("Enter a number to count to: ")
x=int(x)
y=1
while y<=x:
print(y)
y=y+1
b. Run the script to get output similar to the following example:
========== RESTART: /home/Student/06_while-loop.py ==========
Enter a number to count to: 10
1
2
3
4
5
6
7
8
9
10
c. Instead of using while y <= x, we can modify the while loop to use a Boolean check and break to stop
the loop when the check evaluates as false.
x=input("Enter a number to count to: ")
x=int(x)
y=1
while True:
print(y)
y=y+1
if y>x:
break
Modify the 06_while-loop.py script as shown above and run it. You should not have any errors and your
output should look similar to the output in Step 4b.
Step 5: Create a while loop that checks for a user quit command.
What if we want the program to run as many times as the user wants until the user quits the program? To do
this, we can embed the program in a while loop that checks if the user enters a quit command, such as q or
quit.
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 15 www.netacad.com
Lab - Construct a Basic Python Script
x=int(x)
y=1
while True:
print(y)
y=y+1
if y>x:
break
b. Your output should look similar to the following test in which the user entered two different values before
quitting the program. The user then restarted the program and tested quitting with q.
========= RESTART: /home/Student/06_while-loop.py =========
Enter a number to count to: 3
1
2
3
Enter a number to count to: 5
1
2
3
4
5
Enter a number to count to: quit
>>>
========= RESTART: /home/Student/06_while-loop.py =========
Enter a number to count to: q
>>>
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 15 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 15 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 15 www.netacad.com
Lab - Construct a Basic Python Script
devices.append(item.strip())
file.close()
print(devices)
b. Run your script. Your output should look like the following example:
========== RESTART: /home/Student/07_file-access.py ==========
['Cisco 819 Router', 'Cisco 881 Router', 'Cisco 888 Router', 'Cisco 1100
Router', 'Cisco 4321 Router', 'Cisco 4331 Router', 'Cisco 4351 Router', 'Cisco
2960 Catalyst Switch', 'Cisco 3850 Catalyst Switch', 'Cisco 7700 Nexus
Switch', 'Cisco Meraki MS220-8 Cloud Managed Switch', 'Cisco Meraki MX64W
Security Appliance', 'Cisco Meraki MX84 Security Appliance', 'Cisco Meraki
MC74 VoIP Phone', 'Cisco 3860 Catalyst Switch']
Step 4: CHALLENGE: Create a script to allow a user to add devices to an external file.
What if you want to add more devices to the devices.txt file? You can open the file in append mode and then
ask the user to provide the name of the new devices.
a. Complete the following steps to create a script:
1) Open a new file and save it as 07_file-access_activity.py.
2) For the open() function use the mode a, which will allow you to append an item to the devices.txt
file.
3) Inside a while True: loop, embed an input() function command that asks the user for the new device.
4) Set the value of the user's input to a variable named newItem.
5) Use an if statement that breaks the loop if the user types exit and prints the statement "All done!".
6) Use the command file.write(newItem + “\n”) to add the new user provided device. The “\n” will add
a new line to the file for the next entry.
b. Run and troubleshoot your script until you get output similar to the following example:
========== RESTART: /home/Student/07_file-access.py ==========
Enter device name: Cisco 1941 Router
Enter device name: Cisco 2950 Catalyst Switch
Enter device name: exit
All done!
>>>
end of document
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 15 www.netacad.com