1.3.3 Lab - Python Programming Review
1.3.3 Lab - Python Programming Review
2
09/04/21
CPE 028-CPE41S2 Engr. Alonica
Villanueva
Objectives
Part 1: Launch the DEVASC VM
Part 2: Start Python and VS Code
Part 3: Review Data Types and Variables
Part 4: Review Lists and Dictionaries
Part 5: Review the Input Function
Part 6: Review If, For, and While Functions
Part 7: Review Methods for File Access
Background / Scenario
In this lab, you review basic Python programming skills including data types, variables, lists, dictionaries, user
input, if statements, for and while loops, and file access. This lab is not meant as a substitute for prior
programming experience and does not necessarily cover all the Python skills you will need for this course.
However, this lab should serve as a good measure of your Python programming skills and help direct you to
where you may need more review.
Note: This is a reminder. Be sure you observe correct Python indention conventions when writing your
scripts. If you need a tutorial, search the internet for “Python indention rules”.
Required Resources
1 PC with operating system of your choice
Virtual Box or VMWare
DEVASC Virtual Machine
Instructions
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 31 www.netacad.com
Lab - Python Programming Review
Note: You would need to change it to python2 -V if a different device you are using is running version 2.
However, as of January 1, 2020, Python 2 is no longer supported. Therefore, Python 2 is not supported in
this lab or this course.
Note: At the time this lab was written, Python 3.8.2 was the latest version. Although you can update your
Python install with the sudo apt-get install python3 command, this lab and the rest of the labs in this
course are based on Python 3.8.2.
b. To start Python, type python3. The three angle brackets (>>>) indicate that you are in Python's
interactive interpreter.
devasc@labvm~$ python3
Python 3.8.2 (default, Mar 13 2020, 10:14:16)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 31 www.netacad.com
Lab - Python Programming Review
Enter a few math operations using the Python syntax, as shown in the examples.
>>> 2+3
5
>>> 10-4
6
>>> 2*4
8
>>> 20/5
4.0
>>> 3**2
9
b. Recall that Python uses the standard order of operations commonly known as PEMDAS. Mathematical
expressions are evaluated in the following order.
Parentheses
Exponents
Multiplication and Division
Addition and Subtraction
Try entering an expression with a complex order of operations in the interactive interpreter.
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 31 www.netacad.com
Lab - Python Programming Review
'Hello World!'
b. The print command can be also be used directly in the interactive interpreter.
>>> print("Hello World!")
Hello World!
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 31 www.netacad.com
Lab - Python Programming Review
c. In your new file, type the print command from the previous step.
d. Save the script as hello-world.py in the labs/devnet-src/python folder. Be sure you add the extension
.py for Python file.
e. To run the script, click Run > Run Without Debugging. A terminal window opens inside VS Code, runs
the code to launch an instance of Python, runs ysour script, then exits out of Python back to your Linux
command line.
devasc@labvm:~/labs/devnet-src/python$ env DEBUGPY_LAUNCHER_PORT=36095
/usr/bin/python3 /home/devasc/.vscode/extensions/ms-python.python-
2020.4.76186/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher
/home/devasc/labs/devnet-src/python/hello-world.py
Hello World!
devasc@labvm:~/labs/devnet-src/python$
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 31 www.netacad.com
Lab - Python Programming Review
f. Now that you have a command line open inside VS Code, you can manually launch Python and quickly
run your script with the following command.
devasc@labvm:~/labs/devnet-src/python$ python3 hello-world.py
Hello World!
devasc@labvm:~/labs/devnet-src/python$
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 31 www.netacad.com
Lab - Python Programming Review
g. You can also open a terminal window outside of VS Code and enter the same command making sure to
provide path information.
devasc@labvm:~$ python3 ~/labs/devnet-src/python/hello-world.py
Hello World!
devasc@labvm:~$
In this course you will typically run your scripts directly inside VS Code.
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 31 www.netacad.com
Lab - Python Programming Review
1<2
Operator Meaning
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 31 www.netacad.com
Lab - Python Programming Review
>>> 1<=1
True
x=2
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 31 www.netacad.com
Lab - Python Programming Review
b. To print the variables without using a variable to create the space, separate the variables with a comma.
>>> print(str1,str2,str3)
Cisco Networking Academy
b. Use the str() function to convert the integer data type to a string data type.
>>> print("The value of x is " + str(x))
The value of x is 3
>>> type(x)
<class 'int'>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 31 www.netacad.com
Lab - Python Programming Review
c. Notice that the data type for the variable x is still an integer. To convert the data type, reassign the
variable to the new data type.
>>> x=str(x)
>>> type(x)
<class 'str'>
d. You may want to display a float to a specific number of decimal places instead of the full number. To do
this, you can use f-strings and the "{:.2f}".format function.
Note: Search the internet to learn more about f-strings and the format function.
>>> num = 22/7
>>> f"The value of num is {num}"
'The value of num is 3.142857142857143'
>>> pi = "{:.2f}".format(num)
>>> f"The value of pi is {pi}."
'The value of pi is 3.14.'
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 31 www.netacad.com
Lab - Python Programming Review
b. Unlike lists, objectives inside a dictionary cannot be referenced by their sequence number. Instead, you
reference a dictionary object using its key.
o The key is enclosed with brackets [ ].
o Keys that are strings can be referenced using single or double quotes.
o Use a key in the dictionary statement to verify if a key exists in the dictionary.
o Add a key/value pair by setting the new key equal to a value.
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 31 www.netacad.com
Lab - Python Programming Review
>>> 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'}
>>>
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.
>>> 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']}
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 31 www.netacad.com
Lab - Python Programming Review
Step 1: Create a variable to store user input and then display the value.
Most programs require some type of input either from a database, another computer, mouse clicks, or
keyboard input. For keyboard input, use the input() function which includes an optional parameter to provide
a prompt string. If the input function is called, the program will stop until the user provides input and hits the
Enter key. Assign the input() function to a variable that asks the user for input and then print the value of the
user’s input.
>>> firstName = input("What is your first name? ")
What is your first name? User_Name
>>> print("Hello " + firstName +"!")
Hello User_Name!
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 31 www.netacad.com
Lab - Python Programming Review
c.
ng example.
The native VLAN and the data VLAN are different.
d. 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.
The native VLAN and the data VLAN are the same.
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 31 www.netacad.com
Lab - Python Programming Review
...
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. Continuing with the same Python instance, enter the following in the interactive
interpreter.
Note: Be sure you enter four spaces to indent the if function and the eight spaces to indent the print()
function. Press the Enter key twice to exit and execute the for loop.
>>> for item in devices:
... if "R" in item:
... print(item)
...
R1
R2
R3
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 31 www.netacad.com
Lab - Python Programming Review
c. You can also use a combination of the for loop and if statement to create a new list. Enter the following
example to see how to use the append() method to create a new list called switches. Be sure to follow
the indention requirements.
>>> switches=[]
>>> for item in devices:
... if "S" in item:
... switches.append(item)
...
>>> switches
['S1', 'S2']
>>>
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 21 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 22 of 31 www.netacad.com
Lab - Python Programming Review
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. Modify the while-loop.py script as shown in the following:
x=input("Enter a number to count to: ")
x=int(x)
y=1
while True:
print(y)
y=y+1
if y>x:
break
d. Save and run your script. You should get the same output as in Step 4b above.
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 23 of 31 www.netacad.com
Lab - Python Programming Review
x=int(x)
y=1
while True:
print(y)
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 24 of 31 www.netacad.com
Lab - Python Programming Review
y=y+1
if y>x:
break
b. Save and run your script. Your output should look similar to the following in which the user entered two
different values before quitting the program.
devasc@labvm:~/labs/devnet-src/python$ python3 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
devasc@labvm:~/labs/devnet-src/python$
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 25 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 26 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 27 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 28 of 31 www.netacad.com
Lab - Python Programming Review
The devices.txt file is a list of Cisco devices that can easily be copied into a Python list using the following
steps:
o Create an empty list.
o Use the append parameter to copy the file content to the new list.
o Print the list.
a. Modify your file-access.py as shown in the following
devices=[]
file=open("devices.txt","r")
for item in file:
item=item.strip()
devices.append(item)
file.close()
print(devices)
b. Save and run your program. You should get output similar to the following.
devasc@labvm:~/labs/devnet-src/python$ python3 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']
devasc@labvm:~/labs/devnet-src/python$
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 29 of 31 www.netacad.com
Lab - Python Programming Review
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 30 of 31 www.netacad.com
Lab - Python Programming Review
End of document
- 2021 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 31 of 31 www.netacad.com