PYTHON TUTORIAL - How to Create a Dictionary with Python | Steem Skillshare

in Steem Skillshare3 years ago (edited)

Hello Steem Skillshare and SIZ,

Good day everyone, welcome to my blog. In this content, I will teach you to how to create dictionary of items in python and how to perform some actions on the dictionary.

So if you have interest in learning Python programming basics, just follow my blog always and you will learn it as wished!
So before I proceed with this class, I will first of all define what a dictionary is.



What is a dictionary?

A dictionary is an unordered collection of items which is changeable and indexed. Dictionaries in python are written with curly braces '{}'. Python dictionaries have keys and values. Like a normal English dictionary which has words and it's meaning, in this case, the "key" is the word while the "value" is the meaning. We can perform many actions on a dictionary such as access an item stored in the dictionary, looping through the dictionary, changing the value of a key, add items in the dictionary, removing and deleting items from the dictionary e.t.c.



Now that you have known what a dictionary is, let's go down to what we have in this class. Before you follow me in this tutorial make sure you have Visual Studio Code installed in your system. Visit here to download it. after successful download and installation, click here to install the python extension in the vscode.

If you have done this successfully, launch the Visual Studio Code let's start work immediately!



First of all create a folder in your local drive C: and name it Python Dictionary. Then open the folder by the clicking the 'open folder' link as shown in the screenshot below:

python_dict.PNG



After successfully opening the folder in Vs Code, create a new file with an extension name '.py' in the folder that you just opened and name it "dict.py", then click 'Enter' as shown below:

python dictionary file.PNG

Now we are set to start our coding.



Creating a Dictionary

Let's create our dictionary now, print it to the screen using the print() method and then run it on our terminal to see the output. Do same as shown below:

dict output.PNG

Note that if you want to run your python program, you have to move your mouse to the "Terminal tab" at the top of the Vs Code and click it, when the dropdown opens, you will see "New Terminal", click on it and a new terminal will open at the bottom of the Vs Code. Also Note that we use the "print()" method to print our output to the screen. This terminal is where we execute our python codes as the one shown above. If you want to run the code in the Terminal, just type "py dict.py" or "python dict.py" depending on the command the version of your Vs Code accepts.



Accessing Items in the Dictionary

You can access the items of a dictionary by referring to its key name, inside square brackets. Let's access the value of the car model and see the output...

dict access.PNG

In the code above, line 7, we use the code thisdict["model"] to access the value(i.e. BMW X5) of the key(i.e. model) and we have the following output in the terminal as shown above.



Loop Through the Dictionary

Let's loop through the dictionary to print all the elements or items stored in our dictionary variable. You can loop through a dictionary by using a 'for loop'. When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

loop dict.PNG

Note: The codes above, line 10, returns all the keys in the dictionary.



You can also use the values() function to return values of a dictionary:

dict values.PNG

Note: The code above, line 13, returns all the values of the keys in the dictionary. If you want to know more about Python "for loop" read this article.



Changing Values of a dictionary

Let's change the value of the year in the dictionary. You can change the value of a specific item by referring to its key name:

dict change value.PNG

Note: The code above, line 7, changes the year value from '2021' to '2020'. The output is shown in the terminal below in the vs code.



Adding Items

Let's add a new item in the list. Adding an item to the dictionary is done by using a new index key and assigning a value to it...

image.png

Note: The code above, line 7, adds a new key and value (i.e. 'color': 'blue') in the dictionary.



Removing Item From the Dictionary

Let's remove the key, "model", from the dictionary. There are several methods to remove items from a dictionary:

  • Using the pop() method

The pop() method removes the item with the specified key name. The code below, line 9, removes the key 'model' and it's value 'BMW X5' from the dictionary. The output is shown in the terminal below in the vs code.

image.png



  • Using the popitem() method

The popitem() method removes the last inserted item (in versions before python 3.7, a random item is removed instead). The code below, line 9, removes the last item in the dictionary i.e. the year and it's value.

image.png



  • Using the del keyword

The del keyword removes the item with the specified key name. The code below, line 11, deletes the specified key 'model' from the dictionary.

image.png



Conclusion

Python Programming Language is a proficient language which has diverse applications. It is a general-purpose programming language that can be applied in many areas ranging from Web development to Artificial Intelligence. It has powerful libraries used for achieving many things in different areas. You will have a great job opportunities if you learn this language very well. That's why it's very important that you follow my Blog to learn the basics of Python first, before going to complex.



Thank you very much for learning with me till the end. I believe you have something new today. If you have any question regarding to what I have just taught you, you can drop it in the comment section of this post!



Cc: @steem.skillshare
@milakz
@daytona475
@etainclub

Sort:  
 3 years ago 

Hello. The users are not allowed to publish same posts on Steem SkillShare and Steem Infinity Zone as you are exploiting the collaboration

 3 years ago 

Ok

 3 years ago (edited)

I have deleted one! Thanks for your notice

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.029
BTC 63235.53
ETH 2558.76
USDT 1.00
SBD 2.63