Without a list, a script needs one variable for each port:
port_1 = "Gi0/1" port_2 = "Gi0/2" port_3 = "Gi0/3"A list holds all these values in one variable. You create a list like this:
![In ports = ["Gi0/1", "Gi0/2", "Gi0/3"], "Gi0/1" is an item, one value of the list, a comma goes between two items, and the brackets open and close the list.](https://dphfzu3i54bzu.cloudfront.net/media/10257/python-list-parts.png)
Figure 1 - The Parts of a List
To show a list on the screen, you use
print()like with a variable:ports = ["Gi0/1", "Gi0/2", "Gi0/3"] print(ports) # Output: ['Gi0/1', 'Gi0/2', 'Gi0/3']In the lab, create a list named
vlansthat holds 10, 20 and 30.
Print it and click Run to get the flag.Answer the question below
Enter the flag you obtained.
Each item of a list has a position called an index.
Python starts counting at 0, and a negative index counts from the end:![Above the line ports = ["Gi0/1", "Gi0/2", "Gi0/3"], the indexes from the start are 0, 1 and 2, and under it the indexes from the end are -3, -2 and -1.](https://dphfzu3i54bzu.cloudfront.net/media/10316/python-list-indexes.png)
Figure 2 - Each Item Has an Index
To read one item, you write its index between brackets after the name of the list:
ports = ["Gi0/1", "Gi0/2", "Gi0/3"] print(ports[0]) # Output: Gi0/1 print(ports[-1]) # Output: Gi0/3In the lab, print the second switch of the list to get the flag.
Answer the question below
Enter the flag you obtained.
To change a list, you use a method.
You write the name of the list, a dot, then the method like this:![The list ports = ["Gi0/1", "Gi0/2", "Gi0/24"] loses "Gi0/24" with ports.remove("Gi0/24"), then gets "Gi0/3" at the end with ports.append("Gi0/3").](https://dphfzu3i54bzu.cloudfront.net/media/10317/python-list-append-remove.png)
Figure 3 - remove() Then append()
In a script, you write each method on its own line:
ports = ["Gi0/1", "Gi0/2", "Gi0/24"] ports.remove("Gi0/24") ports.append("Gi0/3") print(ports) # Output: ['Gi0/1', 'Gi0/2', 'Gi0/3']In the lab, use the methods to remove VLAN 99 from the list and add VLAN 30 to get the flag.
Answer the question below
Enter the flag you obtained.
To count the items of a list, you use
len().
To know if an item is in a list, you usein, and Python answers True or False:
Figure 4 - len() Counts and in Looks for an Item
You put them inside
print()to see the answer:ports = ["Gi0/1", "Gi0/2", "Gi0/3"] print(len(ports)) # Output: 3 print("Gi0/2" in ports) # Output: TrueIn the lab, print the number of uplinks in the list
uplinks.
Then printDo not touch this portonly when the variableportis in the list to get the flag.Answer the question below
Enter the flag you obtained.