• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Blog

Python Day 4: Dictionaries

September 23, 2018 By Rowell Leave a Comment

Dictionaries

Indexes for dictionaries are called keys. Keys are associated with a value called a key-value pair. A dictionary is unordered. Compared to a List, there is no index starting at 0.

Dictionaries are typed with braces [python]{}[/python]. To assign a dictionary to the client1 variable:

[python]client1 = {'rssi': -52, 'channel': 36, 'noise': -96, 'tx rate': 600, 'mcs': 9}[/python]

The keys within this dictionary are rssi, channel, noise, tx rate, and mcs. The values for those keys are -52, 36, 96, 600, and 9, respectively.

To access a value through a key it is performed by:

[python]
client1[‘channel’]
36
[/python]

Trying to call a key that doesn’t exist in the dictionary will result in a KeyError error message.

[python]
client1['bssid']
Traceback (most recent call last):
File "", line 1, in 
KeyError: 'bssid'
[/python]

With methods, values can be returned from a dictionary. Let’s say I want to print out the key and value of client1. I’ll use a for-loop to iterate through each of the key-value pairs and print them out using the items() method.

[python]
for i in client1.items():
print(i)
('rssi', -52)
('channel', 36)
('noise', -96)
('tx rate', 600)
('mcs', 9)
[/python]

If we just needed to print out the keys we would use the keys() method. Again, I’ll use the for-loop to iterate through the keys and print them out:

[python]
for k in client1.keys():
print(k)
rssi
channel
noise
tx rate
mcs
[/python]

If we wanted just the values we can do the same for-loop but call the values() method:

[python]
for v in client1.values():
print(v)
-52
36
-96
600
9
[/python]

Python Day 3: List Methods

September 11, 2018 By Rowell Leave a Comment

The previous day went over the basics of the Python List. On this day I go a little further into List Methods.

List Methods allow you to work with the items within a List. With methods such as removing and sorting. Useful methods when working with a list at any capacity. 

Removing Values

There’s going to be a time where you need to remove a value from a list. It’s possible to do it with the [python] del [/python] Statement.

[python]
wifi = ['rssi', 'snr', 'spatial streams']
wifi
['rssi', 'snr', 'spatial streams']
del wifi[0]
wifi
['snr', 'spatial streams']
[/python]

But it is possible to do it with the remove method as well:

[python]
wifi = ['rssi', 'snr', 'spatial streams']
wifi.remove('rssi')
wifi
['snr', 'spatial streams']
[/python]

The difference is the remove method uses parenthesis with the actual name of the item within the list. It’s easier to call out the actual name than the index number within the list.

But what happens if the item isn’t in the list? A ValueError occurs:

[python]
wifi.remove('antenna')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
[/python]

Sorting Values

Let’s say I have a list of Tweeps and I want to sort it alphabetically.

[python]
tweeps = ['françois', 'robert', 'sam', 'keith', 'alex']
tweeps
['françois', 'robert', 'sam', 'keith', 'alex']
tweeps.sort()
tweeps
['alex', 'françois', 'keith', 'robert', 'sam']
[/python]

Good stuff. It also works with numbers.

[python]
ages = [54, 92, 65, 27, 3]
ages.sort()
ages
[3, 27, 54, 65, 92]
[/python]

What a list is not capable of sorting is a mix of integers and strings. You end up with a TypeError:

[python]
mix = ['spam', 'rice', 3, 5, 'red']
mix.sort()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'int' and 'str'
[/python]

I’m sure in networking there will be many reasons to use a list. I can just see it happening but I don’t have any specific use case as of yet. It’s still early for me to figure out what I’d be using a list for. Maybe a list of IP addresses or interfaces of a switch or router.

Python Day 2: Lists

September 10, 2018 By Rowell Leave a Comment

Lists look like they’ll be useful when I get to a point where I can write scripts. Python Lists can hold multiple values in an ordered sequence.

Within the list are items which are ordered by an index. The index starts with 0. Each item is separated by a comma and any strings must have quotes around them. 

To assign a list to a variable and retrieve an item from the list the syntax looks like:

[python]
groceries = ['oranges', 'spinach', 'eggs', 'milk']
groceries[0]
'oranges'
[/python]

As I said earlier, the first value in the list starts with 0. It’s possible to go in reverse by using negative indexes:

[python]
groceries = ['oranges', 'spinach', 'eggs', 'milk']
groceries[-1]
'milk'
[/python]

What if you wanted to access multiple values within a list? That’s done by slicing. A slice starts with the first integer, a colon, and the second integer is where the slice ends, but not including that item. Take a look:

[python]
groceries[0:3]
['oranges', 'spinach', 'eggs']
[/python]

Modifying items within a list is possible. Let’s say you wanted to change oranges to apples in the groceries list. We need to modify index 0 within the list.

[python]
groceries[0]
'oranges'
groceries[0] = 'apples'
groceries[0]
'apples'
[/python]

Removing items from a Python list is fairly simple as well by using the del statement. We must reference the index of the item we want to remove.:

[python]
groceries
['apples', 'spinach', 'eggs', 'milk']
del groceries[0]
groceries
['spinach', 'eggs', 'milk']
[/python]

The items in the list move up in their index values which means spinach is now at index 0.

Adding items to the Python list is done by appending it using what’s called a method. Methods are a little different when it comes to lists because we call on the method on a value:

[python]
groceries
['spinach', 'eggs', 'milk']
groceries.append('yogurt')
groceries
['spinach', 'eggs', 'milk', 'yogurt']
[/python]

By appending to the list, the new item we are adding gets added to the end of the list. 

It is possible to add items to the list at a specific index. The index value must be passed, with the new item, using the insert method:

[python]
groceries
['spinach', 'eggs', 'milk', 'yogurt']
groceries.insert(0, 'pears')
groceries
['pears', 'spinach', 'eggs', 'milk', 'yogurt']
[/python]

There’s quite a lot you can do with lists. They are very flexible and can be modified with ease. That’s all I’ve done with Day 2 of my Python learning exercise. The next Day will include more Python List work. 

On to more practicing…

  • « Go to Previous Page
  • Go to page 1
  • Interim pages omitted …
  • Go to page 27
  • Go to page 28
  • Go to page 29
  • Go to page 30
  • Go to page 31
  • Go to Next Page »

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.