1.3.3 Lab - Python Programming Review
1.3.3 Lab - Python Programming Review
Instructor Note: Red font color or gray highlights indicate text that appears in the instructor copy only.
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
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 15 www.netacad.com
Lab - Python Programming Review
Python 3.8.2
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.
>>>
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.
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 15 www.netacad.com
Lab - Python Programming Review
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 15 www.netacad.com
Lab - Python Programming Review
In this course you will typically run your scripts directly inside VS Code.
Operator Meaning
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 15 www.netacad.com
Lab - Python Programming Review
>>> 1<1
False
>>> 1==1
True
>>> 1>=1
True
>>> 1<=1
True
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))
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 15 www.netacad.com
Lab - Python Programming Review
The value of x is 3
>>> type(x)
<class 'int'>
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.'
>>>
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 15 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.
>>> 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
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 15 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!
>>>
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 15 www.netacad.com
Lab - Python Programming Review
a. Open a blank script and save it as if-vlan.py. Type the following script into the file.
nativeVLAN = 1
dataVLAN = 100
if nativeVLAN == dataVLAN:
print("The native VLAN and the data VLAN are the same.")
else:
print("The native VLAN and the data VLAN are different.")
Note: In Python, use four spaces to indent. If you save your file in VS Code, this four space indention will
be automatic. When beginning the else statement, make sure to backspace to the left margin.
b. Save the script and run it. Your output should look like the following example.
The native VLAN and the data VLAN are different.
c. 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.
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 15 www.netacad.com
Lab - Python Programming Review
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 15 www.netacad.com
Lab - Python Programming Review
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 15 www.netacad.com
Lab - Python Programming Review
x=int(x)
y=1
while True:
print(y)
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$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 15 www.netacad.com
Lab - Python Programming Review
The name parameter is the name of the file to be opened. If the file is in a different directory than your script,
you will also need to provide path information. For our purposes, we are only interested in three mode
parameters:
o r - read the file (default mode if mode is omitted).
o w - write over the file, replacing the content of the file.
o a - append to the file.
a. Open a blank script and save it as file-access.py.
b. Create the following program to read and print the content of the devices.txt which is in the same
directory as your program. After printing the contents of the file, use the close() function to remove it from
the computer's memory.
file=open("devices.txt","r")
for item in file:
print(item)
file.close()
Note: The contents of the file are set to a variable named file. However, that variable can be called
anything the programmer chooses.
c. 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
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 15 www.netacad.com
Lab - Python Programming Review
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 15 www.netacad.com
Lab - Python Programming Review
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$
© 2020 - 2020 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 15 www.netacad.com