PYTHON PROGRAMMING CLASS (THE BASICS) || DAY 4 - PYTHON DICTIONARIES COLLECTION | WEEK 2

in CampusConnect2 years ago (edited)

python day 4 banner.PNG

Good day everyone!
Hope you're doing great!! Today is the fourth day of the Python Programming class week 2. If you have followed my week 1 till the end, you must have acquired some basic Python programming skills that can help you start your programming career. However, if you did not follow my week 1 classes, please visit by blog now @anyiglobal to see all my week 1 classes.

I brought out this idea of teaching python on steemit because of the need for python programming skills in the world of technology today. This is the new trend in tech now because python can be used for various purposes in many applications ranging from web development, automation, artificial intelligence, software testing to many others. I decided to start this programming class on steemit communities because the communities comprise mainly of people who are still eager to learn new skills. Developers and non-developers alike can use this python.

Note: The English language used in this class is literally understandable by a layman. So let's begin...


PYTHON DICTIONARIES COLLECTION

Python dictionary is a type of collections in Python that is unordered, changeable, and indexed. Python dictionary also falls under Sequence data type as well as Lists, Tuples, and Sets. Python dictionaries are enclosed in curly braces. They have keys and value, and the keys can be used to access the values.

For Example,
Let's create a dictionary of some steemit users and their respective reputation:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
print(names)

Output
{'Anyiglobal': 63, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67}

python dictionary.PNG
Python Dictionary

From the code in the example above, each name of the steemit users are the keys whereas the respective reputations are the values of each key.

NB: Copy the code and try it out in your text editor.


We will detail dictionaries collection in this class by discussing the following:

  • How to access dictionary items.
  • How to change values in a dictionary.
  • Checking if an item exists in a dictionary.
  • Checking the length of a dictionary.
  • Adding items to the dictionary.
  • Removing items from the dictionary.
  • How to copy a dictionary
  • Using the dict() constructor to create a dictionary.

ACCESSING ITEMS IN A DICTIONARY
We can access a value in a dictionary by referring to it's key inside a square bracket "[]".

For Example,

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
print(names["Simonnwigwe"])

Output
71

python dictionary access item.PNG
Access item in a dictionary

If you observe in the code above, we accessed the value (reputation) of Simonnwigwe by referring to it's key which is "Simonnwigwe".


There's also another method we can use to access the value in a dictionary, the method is "get()" method:

For Example,

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
print(names.get("Anyiglobal"))

Output
63

NB: Copy the codes above and try it out in your text editor. Please make sure you practice as that is the only way to learn programming very fast.


CHANGING ITEM VALUE IN A DICTIONARY
In Python, we can change a specific value of an item in a dictionary by referring to it's key and then assigning the value you want to use to replace the existing one.

For Example,
Let's change the reputation value of "Anyiglobal" with another reputation value of "65".

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
names["Anyiglobal"] = 65
print(names)

Output
{'Anyiglobal': 65, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67}

python dict change value.PNG
Changing dictionary value

Looking at the screenshot above, we will see that we successfully changed the reputation value of Anyiglobal from 63 to 65.

NB: Copy the code in the screenshot above and try it out in your text editor.


LOOPING THROUGH A DICTIONARY
We use for loop to iterate through a dictionary to return the keys of the items stored in the dictionary collection. We can also loop through a dictionary to return the values of items stored in the dictionary which we will see later below.

For Example,
Using "for loop":

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
for x in names:
  print(x)

Output
Anyiglobal
Starrchris
Dayographix
Simonnwigwe
jueco

python dict loop.PNG
Looping through a dictionary

From the screenshot above, we used the "for loop" to iterate through the dictionary to get the keys of the items stored in the dictionary.


Using "values()" method to return the values of items stored in a dictionary:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
for x in names.values():
  print(x)

Output
63
65
62
71
67

python dict return values.PNG
Looping through a dictionary to return it's values

Looking at the code above, you can observe that we used the "values()" method to return the values of items stored in a dictionary.


We can as well loop through a dictionary to get both the keys and the values in the dictionary:

For Example,

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
for x, y in names.items():
  print(x, y)

python dict key value.PNG
Returning key and value in a dictionary collection

NB: Copy the code above and try it out in your text editor.


CHECK IF KEYS EXIST IN DICTIONARIES COLLECTION
To check if a specified key exists/present in a dictionary we use the "in" keyword. Recall that the "in" keyword is a membership operator as discussed in one of our classes previously.

For Example,
Let's check if the key "Starrchris" is present in the dictionary below:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
if "Starrchris" in names:
  print("Yes, Starrchris is present in the names dictionary")

Output
Yes, the key Starrchris in present in the dictionary of names.

python dict key existence.PNG
Checking if a key exist in a dictionary

As seen in the screenshot above, we used the in keyword and checked if the key "Starrchris" exists in a dictionary.


CHECKING LENGTH OF A DICTIONARY
We use the "len()" method to know the number of key-value pairs in a dictionary.

For Example,

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
print(len(names))

Output
5

python dict lenght.PNG
Checking the length of a dictionary

From the code in the example above, we used the len() method to get the length of the dictionary which is 5.


ADDING ITEMS TO A DICTIONARY
Adding items to the dictionary is simply done by using a new key index, and then assigning a new value to it.

For Example,
Let's add a new key name "Arinzegold12" and his reputation value of "63" to the dictionary.

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
}
names["Arinzegold12"] = 63
print(names)

Output
{'Anyiglobal': 63, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67, 'Arinzegold12': 63}

python dict add item.PNG
Adding new item to a dictionary

We used the code above to add a new item to the dictionary of steemit usernames.


REMOVING ITEMS FROM THE DICTIONARY
As we learned in our previous classes on Lists collection, Tuples, and Sets collection, we taught about how to remove items from a collection using remove() method, pop() method, del keyword, and clear() method. However, removing items from a dictionary is closely related to the methods used in other collections with just a slight difference which we will see below.

In dictionary, we use the following methods and keyword to remove items from a dictionary:

  • pop() method - Removes item with a specified key name.
  • popitem() method - Removes the last inserted item. But in Python version 3.7, a random item is removed rather.
  • del keyword - Removes item with a specified key name. It can as well used to delete the entire dictionary.
  • clear() method - It empties the dictionary.

Using the pop() method:
Lets remove "Arinzegold12" from the dictionary ...

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
  "Arinzegold12": 63
}
names.pop("Arinzegold12")
print(names)

Output
{'Anyiglobal': 63, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67}

python dict remove with pop.PNG


Using the "popitem()" method to remove the last inserted item:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
  "Arinzegold12": 63
}
names.popitem()
print(names)

Output
{'Anyiglobal': 63, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67}

From the output in our example above, we can see that the method removed the last inserted item "Arinzegold12" from the dictionary.


Using the del Keyword to delete the dictionary entirely:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
  "Arinzegold12": 63
}
del names
print(names)

Output
NameError: name 'names' is not defined

Note that the code above thrown a name error because the dictionary name do not exist anymore after deleting it.


Using the clear() method:

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
  "Arinzegold12": 63
}
names.clear()
print(names)

Output
{}

As we can see above, the clear() method cleared the items in the dictionary and the output returned an empty dictionary.

NB: Please make sure you copy the code and try it out in your text editor. Copy the codes and try the following methods in your text editor to see how it works!


COPYING A DICTIONARY
To copy a dictionary variable into another variable, we use the "copy()" method. we can simply do the following below:

For Example,

names = {
  "Anyiglobal": 63,
  "Starrchris": 65,
  "Dayographix": 62,
  "Simonnwigwe": 71,
  "jueco": 67
  "Arinzegold12": 63
}
usernames = names.copy()
print(usernames)

Output
{'Anyiglobal': 63, 'Starrchris': 65, 'Dayographix': 62, 'Simonnwigwe': 71, 'jueco': 67, 'Arinzegold12': 63}

python dict copy.PNG
Copying a dictionary

NB: Please make sure to copy the code and try it out in your text editor.


PYTHON DICT() CONSTRUCTOR
We can make a new dictionary by using the dict() constructor. Special things to note when making a dictionary using constructor method include the following:

  • The keys are not string literals i.e. they're not enclosed in a double quotation mark.
  • The key and the value is not separated by ":" but rather separated with "=" sign.

For Example

usernames = dict(Anyiglobal = 63, Starrchris = 65, Dayographix = 62, Simonnwigwe = 71, jueco = 67, Arinzegold12 = 63)
print(usernames)

python dict constructor.PNG
Python dictionary constructor

From the code in the example above, we used the dict() method to convert the usernames to a dictionary collection.

NB: Please do well to copy the codes and try it out in your text editor.



Conclusion

Python dictionaries collection has several other methods we can use to manipulate dictionary items. Please make sure to make more research on Python dictionaries collection to have a broader knowledge on the topic. As a programmer, it is pertinent to know how to manipulate dictionary items during programming. We discussed the following in this class; Python dictionaries collection, how to access items in a dictionary, looping through a dictionary, how to change item value in a dictionary, checking whether an item exists in a dictionary, how to add items to a dictionary, how to items from a dictionary, checking the length of a dictionary, how to remove an item from a dictionary, how to copy a dictionary, and how to use a dict() constructor to create a new dictionary. Thanks for being part of this class, hope you learnt something new today!



This is the end of this particular class. I believe that if you followed this class till the end, you must have grabbed one or more information from the class. Please make sure to practice along with your laptop and text editor to grab every bit of the information passed.

Please do well to follow this blog, and also don't forget to resteem this post so that it can reach larger number of steemians who wants to learn this skill.


Am glad you participated in this class! I believe you have learnt something new today.

I am @anyiglobal, a Computer Scientist, Software Engineer and a Blogger!



Cc:
@campusconnectng, @whitestallion, @dayographix, @kouba01, @pelon53, @reminiscence01, @steemchiller, @justyy

Sort:  

Thank you for contributing to #LearnWithSteem theme. This post has been upvoted by @Reminiscence01 using @steemcurator09 account. We encourage you to keep publishing quality and original content in the Steemit ecosystem to earn support for your content.

Club Status: #Club5050

Sevengers Comment GIF.gif

Regards,
Team #Sevengers

Wow thank you for giving us a quality post, continue to give us a quality post i appreciate your work

 2 years ago 

Wow! It's my pleasure that you found my content quality! Thanks for commending my efforts!!! Please help me to resteem the class so that it will be reachable to larger number of steemians who are interested! That's my humble request!

Your post is manually rewarded by the @nftmc Community Curation Trail.


Join the NFTMC community to get rewarded.

USE TAG - #nftmc

Curation Trail- @nftmc
Discord- https://discord.gg/5P57gwYYcT
Twitter- https://mobile.twitter.com/NFTMC3

 2 years ago 

Am happy to see this, this is just like SQL, but a higher level. This will help me learn more about programming.Thanks for sharing

 2 years ago 

Ok. Am here to keep teaching you Python Programming Language! Am glad you participated in this class!!

 2 years ago 

Thank you

 2 years ago 

Hello @anyiglobal, thanks for publishing this post in Campus Connect community, Your post has been supported using Campus Connect Curation account.

Keep publishing original and quality posts in Campus Connect community.

image.png

 2 years ago 

Thanks! Anticipate for my class tomorrow

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 64349.20
ETH 2673.53
USDT 1.00
SBD 2.83