1.3.3 Lab - Python Programming Review Ok
1.3.3 Lab - Python Programming Review Ok
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 1 of 21 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.
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
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 2 of 21 www.netacad.com
Lab - Python Programming Review
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 3 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 4 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 5 of 21 www.netacad.com
Lab - Python Programming Review
Operator Meaning
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 6 of 21 www.netacad.com
Lab - Python Programming Review
>>> "Cisco"*x
'CiscoCiscoCisco'
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
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 7 of 21 www.netacad.com
Lab - Python Programming Review
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'>
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 8 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 9 of 21 www.netacad.com
Lab - Python Programming Review
'S2'
>>> hostnames[0]="RTR1"
>>> hostnames
['RTR1', 'R2', 'R3', 'S1', 'S2']
>>> del hostnames[3]
>>> hostnames
['RTR1', 'R2', 'R3', 'S2']
>>>
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"
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 10 of 21 www.netacad.com
Lab - Python Programming Review
>>> ipAddress
{'R1': '10.1.1.1', 'R2': '10.2.2.1', 'R3': '10.3.3.1', 'S1': '10.1.1.10'}
>>>
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']}
>>>
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 11 of 21 www.netacad.com
Lab - Python Programming Review
d. Your script should run without any errors, as shown in the following output.
devasc@labvm:~/labs/devnet-src$ python3 person-info.py
What is your first name? Bob
What is your last name? Smith
What is your location? London
What is your age? 36
Hi Bob Smith! Your location is London and you are 36 years old.
devasc@labvm:~/labs/devnet-src$ ^C
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 12 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 13 of 21 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
>>>
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']
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 14 of 21 www.netacad.com
Lab - Python Programming Review
>>>
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 15 of 21 www.netacad.com
Lab - Python Programming Review
devasc@labvm:~/labs/devnet-src/python$
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.
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 16 of 21 www.netacad.com
Lab - Python Programming Review
while True:
x=input("Enter a number to count to: ")
if x == 'q' or x == 'quit':
break
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 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 17 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 18 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 19 of 21 www.netacad.com
Lab - Python Programming Review
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 20 of 21 www.netacad.com
Lab - Python Programming Review
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.
7) Close the file to release it from computer memory.
Run and troubleshoot your script until you get output similar to the following.
devasc@labvm:~/labs/devnet-src/python$ python3 file-access-input.py
Enter device name: Cisco 1941 Router
Enter device name: Cisco 2950 Catalyst Switch
Enter device name: exit
All done!
devasc@labvm:~/labs/devnet-src/python
© 2020 - 2024 Cisco and/or its affiliates. All rights reserved. Cisco Public Page 21 of 21 www.netacad.com