Introduction to Python
Component of Python
Dictionary is that collection data type of Python which is unordered , changeable and indexed and these are also written by curly brackets {} , having keys and its values .
Examples:-
Create a dictionary
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
""" keys here are fruits , Vegetables & Color whereas Apple, Carrot and Red are values of this keys """
print(thisdict)
So here in above program we had given the command of creating dictionary data to our Python interpreter. Let’s see .
So our ‘Dictionary’ is been ready.
Here in the Dictionary data type ,we can access keys in dictionary by referring to the key value inside square bracket[] .
Let’s understand this by an example :-
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
print(thisdict["Vegetable"])
So here in the above program we just want to access the value ‘Carrot’ which is the value of ‘Vegetable’ . So we just type down the ‘Vegetable’ key to access ‘Carrot’ .
So let’s see what output it gives :-
So we just get the ‘Carrot’ value which we want just by passing the key.
We can change the dictionary items just by referencing the keys and giving it the desired value.
Let’s understand this by the following example :-
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
thisdict["Fruits"]= "Banana"
print(thisdict)
So here in the above Program we just gave command to our Python interpreter that we want to change the value of ‘Fruits’ from ‘Apple’ to ‘Banana’ .
So let’s see what output it gives :-
So here it just changes the ‘Apple’ to ‘Banana’
Unlike other collection data type here also loops is done by using ‘for’ loop but only the keys values we can get in return , although there are methods too how we can get the values in return .
Let’s understand this by an example :-
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
for X in thisdict:
print(X)
So this is the method basically to give command to our Python interpreter that we want to print all the keys , one by one .
So let’s see what output it gives :-
Here we get all the keys present in our program , but if we want to get values in return than there is a different way
Let’s understand this by an example :-
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
for X in thisdict:
print(thisdict[X])
So this is the method basically to give command to our Python interpreter that we want to print all the values , one by one .
So let’s see what output it gives :-
So here we get the values in return.
( More about Loops we will talk further on the looping section. )
We can also check keys in our dictionary by using the ‘in’ keyword.
Let us understand this by the following example :-
thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}
if "Fruits" in thisdict:
print ("Yes it is in dictionary")
So our ‘Fruits’ is in the dictionary and it gives us the correct output.
( We will read in detail regarding ‘if’ statement . )
As we know that methods are basically the function which we want to perform in our coding . In Python there are some inbuilt methods are there for dictionary too.
So let’s see some of them :-
The len() method - The len() method - It just returns the length of the Dictionary , considering key and its value as one .
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
print(len(thisdict))
So here we just get the length of the set based on their index number.
Adding items on dictionary
Adding an element in dictionary we have to use new index key and assigning it a value.
Let’s understand this by the example :-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
thisdict["Number"]= 500
print(thisdict))
So here through add() method we add new keys to our dictionary.
Removing item of dictionary
There are several types of method to removing the dictionary:-
the pop method remove any item or key with the specified key name.
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
thisdict.pop("Fruits")
print(thisdict)
Here we had just removed the ‘Fruits’ key plus its value too from our dictionary by pop() method.
the popitem() method removes the last element or key of our dictionary.
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
thisdict.popitem()
print(thisdict)
So here our last key and its value is vanish just by using popitem() method .
the del() keyword removes the item specified key name.
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
del thisdict["Fruits"]
print(thisdict)
So here we just deleted the key ‘Fruits’ and it value too.
del() keyword removes the whole dictionary too.
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
del thisdict
print(thisdict)
So here we just deleted the whole dictionary.
clear () keyword empties the dictionary.
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
thisdict.clear()
print(thisdict)
Here whole dictionary is empty or invisible by using clear() method.
We can copy the dictionary by giving references , and there are two methods of copying the dictionary.
copy the dictionary through copy () methods .
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
mydict=thisdict.copy()
print(thisdict)
Here we have copied thisdict keys and values into mydict, and when we print mydict , it give us all the elements which are present in thisdict
copy the dictionary through dict() methods .
example:-
thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}
mydict=dict(thisdict)
print(thisdict)
So through dict() method we copied our thisdict library to mydict.
The dict() constructor , dict() can used to make new dictionary.
thisdict =dict(Fruits= "Apple", Vegetable= "Carrot", Color= "Red") keywords are not string literals
# use of equals rather than colon for the assignment
print(thisdict)
So here through set() constructor our set is ready.
Some methods of dictionary and their functionality
Python has set of built-in method that you can use the dictionary
Method | Description |
---|---|
clear() | Remove all element to dictionary |
copy() | Return copy on the dictionary |
fromkeys() | Create dictionary from given sequence |
get() | Return values of the keys |
items() | Returns view list of dictionary (key.values) pair |
key() | Return the containing keys list of dictionary |
pops() | Remove the element with specified keys |
popitem () | Remove the last inserted key-value pair |
setdefault() | Insert key with values if key not present |
update() | Updates the dictionary |
values() | Returns all view values in dictionary |
Check all these methods for good practice.
python training insitute| Best IT Training classes in Gurgaon | python training