Welcome to the blog !!! Let's begin our journey of learning python by understanding some simple basic concepts.
In this tutorial you’ll learn basics of the Python programming language. The second part covers dictionaries, sets.
The tutorial uses Python IDLE for Python development environment. If you have not installed Python yet take a look at Install Python 3.X On Windows – Python Installation Guide 💻
Dictionaries
The next important data structure which is supported by Python is a
dictionary. In comparison to a list each element within a dictionary is
made up of two parts: key and value. To define a list we’re using the
following syntax:
If we now want to access a specific value from the dictionary we can use
the corresponding key for selection. The name of the key is passed into
the brackets syntax in the following way:
The value of a key within a dictionary can be of any type, e.g. we can use a list as a value. The following example shows how to define a dictionary with one key. The value of that key is a list with three numeric elements:
We can also retrieve the list from the dictionary and assign this list at the same time to a new variable which is called list1:
As seen before we can now use the index selector to access items from the list.
As seen with lists before, it’s also possible to include a dictionary within a dictionary. In the following example a dictionary is key1 is defined and the value of this key is set to another dictionary:
Accessing the inside dictionary is straight forward:Of course you can access a specific element within the inside
dictionary. Here you need to use brackets two times. First to access the
value of key1 (which is the inside dictionary) and then access the value of key2 from the inside dictionary:
Sets
Last but not least, let’s take a look at sets, another data structure
which is part of Python. Sets are defined by using curly braces:
The result set contains only unique values, all duplicates are removed automatically.
Off course, sets can be stored in variables.Adding another element to the set s1 can be done by calling the add method and passing in the new element as a parameter.Now the set looks like the following:
Tags:
Python