28.1.2 Lab - Construct A Basic Python Script - ILM
28.1.2 Lab - Construct A Basic Python Script - ILM
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
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 16 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 16 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 16 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 16 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.
>>> print(str1+" "+str2+" "+str3)
Cisco Networking Academy
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 16 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 16 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 16 www.netacad.com
Lab - Construct a Basic Python Script
The print statements each have syntactical errors. The following are the correct print statements.
print(country)
print(capitals)
print(capitals["South Africa"][1])
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 16 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 16 www.netacad.com
Lab - Construct a Basic Python Script
b. Run multiple times to test each statement. Troubleshoot any errors. Your output should look similar to the
following:
=========== RESTART: /home/Student/05_if-acl.py ===========
What is the IPv4 ACL number? 10
This is a standard IPv4 ACL.
>>>
=========== RESTART: /home/Student/05_if-acl.py ===========
What is the IPv4 ACL number? 100
This is an extended IPv4 ACL.
>>>
=========== RESTART: /home/Student/05_if-acl.py ===========
What is the IPv4 ACL number? 2000
This is not a standard or extended IPv4 ACL.
>>>
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,
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 16 www.netacad.com
Lab - Construct a Basic Python Script
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 11 of 16 www.netacad.com
Lab - Construct a Basic Python Script
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.
a. In your 06_while-loop.py script, make the following changes.
1) Add another while loop to the beginning of the script which will check for a quit command.
2) Add an if function to the while loop to check for q or quit.
while True:
x=input("Enter a number to count to: (Enter q to quit.)")
if x == 'q' or x == 'quit':
break
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
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 16 www.netacad.com
Lab - Construct a Basic Python Script
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 13 of 16 www.netacad.com
Lab - Construct a Basic Python Script
3) Create a script to read and print the contents of devices.txt. After printing the contents of the file, use
the close() function to remove it from the computer's memory. The close() function should not be
indented, as shown.
Note: A variable named file is created to hold the contents of the file. However, that variable can be
called anything the programmer chooses.
file=open("devices.txt","r")
for item in file:
print(item)
file.close()
b. Run the script and troubleshoot, if necessary. Your output should look like the following:
========== RESTART: /home/Student/07_file-access1.py ==========
Cisco 819 Router
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 16 www.netacad.com
Lab - Construct a Basic Python Script
file=open("devices.txt","r")
for item in file:
print(item.strip())
file.close()
b. Run your script. The output should not display blank lines between the device names.
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 ==========
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 16 www.netacad.com
Lab - Construct a Basic Python Script
2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 16 www.netacad.com