Sunday, September 16, 2018

Python Ninja Bootcamp 8-Dictionary

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Dictionary.


"Dictionary is an unordered collection of items i.e key-value pairs ".


Let's look at Dictionary example-


Create Dictionary


Dictionary is defined by placing item i.e key-value pair, inside curly braces {} separated by a comma. Each key is separated from its value by a colon (:).

my_dictionary = { 'key1': 1 ,'key2': 2 ,'key3': 3 } # here key Key1, key2 & key3 is mapped with the value 1, 2 & 3 repectively
print(my_dictionary)
>> { 'key1' : 1, 'key2' : 2, 'key3' : 3 }


Access Dictionary Values


We can use the key inside the index operator [] to access a value in a dictionary. 

my_dictionary = { 'key1': 1 ,'key2': 2 ,'key3': 3 }
print( my_dictionary['key1'] ) # garbs the value mapped with key1
>>1

We have an option in python that list can hold different object type (integer, float, string etc).


my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
print( my_dictionary )
>>{ 'key1': 1, 'key2': 1.5 ,'key3': 'string' }

my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
print( my_dictionary['key3'] )
>> 'string' 


Since the value corresponding to key3 is a string object, so we can perform all thing we did in string lecture. 


print( my_dictionary['key3'][0] ) # grab the letter at index 0
>> 's'


print( my_dictionary['key3'][-1] ) # reverse the string
>> gnirts

print( my_dictionary['key3'].upper( ) ) # Convert the string to uppercase
>> STRING


Mutability


Dictionary is mutable, the values within it can be changed or replaced, unlike string or tuple.

We can use the assignment operator (=) to change an item or a range of items.

my_dictionary={ 'key1': 1, 'key2': 1.5,'key3': 'string'}
my_dictionary['key1'] = my_dictionary['key1'] + 99
print( my_dictionary['key1'] )
>> 100

print( my_dictonary )
>> { 'key1': 100, 'key2': 1.5,'key3': 'string'}

We can also use subtraction to change the 'key1' value 

my_dictionary['key1'] - = 50
print( my_dictionary['key1'] )
>> 50

print( my_dictionary )
>> { 'key1': 50, 'key2': 1.5,'key3': 'string'}

Another Method of Creating Dictionary 


We can also create a dictionary with an empty dictionary

# Create a empty dictionary
d = {}
print( d )
>> { }

# Insert the key-value pairs in the empty dictionary
d['student' ] = 'Tamta'
d['roll_no'] = 40
print( d )
>> { 'student' : 'Tamta' , 'roll_no' : 40 }



Nested dictionary


Dictionary could have another dictionary as a key-value pair. This is called a nested dictionary.

d = {'key' : {'nestedkey' : {'subnestedkey' : 'value' }}}
print( d )
>> {'key': {'nestedkey': {'subnestedkey': 'value'}}}

We can use keys to extract the value in the nested list.
Let's see how to grab the value.

# keep calling the keys
print( d['key']['nestedkey']['subnestedkey'] ) 
>> 'value'

Since the value is a string object, so we can perform all thing we did in string lecture. 

print( d['key']['nestedkey']['subnestedkey'].upper( ) ) 
>> VALUE



Built-in Function



Methods that are available with Dictionary.

d = { 'k1' : 1, 'k2': 2, 'k3': 3} 
print( d )
>> {  'k1' : 1, 'k2': 2, 'k3': 3 }

Note: If press TAB after d. in the Juypter notebook. It will show the list of functions available in the string.


print( d.keys() )  # Return a new view of the dictionary's keys.
>> dict_keys( ['k1', 'k2', 'k3'] )

print( d.values() ) # Return a new view of the dictionary's values.
>> dict_values( [1, 2, 3] )

print( d.items() ) # Return a new view of the dictionary's key-value pair.
>>dict_items( [('k1', 1), ('k2', 2), ('k3', 3)] )

d.pop( 'k1' )  # remove specified key and return the corresponding value
print( d )
>>{ 'k2': 2, 'k3' : 3}


d.clear() # remove all the items from the dictionary
print(d)
>>{ }

Practice other built-in functions available in the list.





EDITS ARE WELCOMED!!

In the next Blog, we will discuss Tuple 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-9-tuple.html


......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 



Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment