#5 Python Tutorial for Beginners

 Hello Guys ,  

   In previous tutorial you learned about List , List slicing , List function , Tuples and Tuples functions in python.

   In this tutorial you will learn below points:

  • Dictionary
  • Nested Dictionary
  • Dictionary Method
  • Set
  • Set Method
Let's get started.

Dictionary

  • Dictionary is a collection of key-value pairs.
  • Dictionary is unordered.
  • Dictionary is mutable.
  • Dictionary is indexed.
  • Dictionary can not contain duplicate keys.
  • We can access specific value using specific key.

Example :

# Dictionary 
'''
   Dictionary is collection of key-value pairs
'''

MyDictionary = {
    "Ps" : "Inf0" ,
     1 : "psh" ,
    "language" : "python"
}

print(MyDictionary)

'''
  We can retrieve specific value using key.
  Please define key in these [] brackets.  
'''
print(MyDictionary["language"])

Output :

{'Ps''Inf0'1'psh''language''python'}
python


Nested Dictionary

  • We create dictionary in another dictionary called nested dictionary.
Example :

# Nested Dictionary

'''
   We create dictionary in another dictionary
     called Nested Dictionary.
'''

MyDictionary = {
    "Ps" : "Inf0" ,
     1 : "psh" ,
    "language" : "python" ,

    "anotherDictionary" : { "Blog" : "Ps_Inf0"}
}

print(MyDictionary)

Output :

{'Ps''Inf0'1'psh''language''python'
        'anotherDictionary': {'Blog''Ps_Inf0'}}

Dictionary Method

  • items() method : items() method is used to display all key and values in pair.
Example :

MyDictionary = {
    "Ps" : "Inf0" ,
     1 : "psh" ,
    "language" : "python"
}

# items() method is used to display all key and values in pair.
print(MyDictionary.items())

Output :

dict_items([('Ps''Inf0'), (1'psh'), ('language''python')])

  • keys() method : keys() method is used to display all keys.
Example :

MyDictionary = {
    "Ps" : "Inf0" ,
     1 : "psh" ,
    "language" : "python"
}
# keys() method is used to display all keys
print(MyDictionary.keys())

Output :

dict_keys(['Ps'1'language'])

  • update() method : update() method is used to add key values in dictionary.
Example :

MyDictionary = {
    "Ps" : "Inf0" ,
     1 : "psh" ,
    "language" : "python"
}

# update() method is used to add key values in dictionary.
MyDictionary.update({"Hello" : "World"})
print(MyDictionary)

Output :

{'Ps''Inf0'1'psh''language''python''Hello''World'}


Set

  • Set is collection of non repetitive elements.
  • Set are unordered.
  • Set are immutable means we can not change value after creation of set.
  • Set are unindexed.
  • Set can not contain duplicate values.
Example :

# set
'''
  We can create set using curly brackets {}.
'''
number = {1 , 2 , 3 , 4 , 5}
print(number)

'''
  We can also check type of set.
'''
print(type(number))

'''
  We can create empty set using set() function and
  check the type of set.
'''
# Empty set
no = set()
print(type(no))

Output :

{12345}
<class 'set'>
<class 'set'>

Set Method

  • set() method : set () method is used to create empty set.
Example :

# Using set() method we create empty set
no = set()
print(type(no))

Output :

<class 'set'>
  • add() method : add() method is used to add value in set.
Example :

# add() method is used to add value in set
no.add(1)
no.add(2)
no.add(3)
no.add(4)
no.add(5)
print(no)

Output :

{12345}
  • len() method : len() method is used to find length of the set.
Example :

number = {1 , 2 , 3 , 4 , 5}
print(len(number))

Output :

5
  • remove() method : remove() method is used to remove specific value from set.
Example :

# remove() method is used to remove specific value from set
number = {1 , 2 , 3 , 4 , 5}
no.remove(4)
print(no)

Output :

{1235}
  • pop() method : pop() method is used to remove element.
Example :

# pop() method is used to remove an element
print(no.pop())
print(no)

Output :
1
{235}



If you liked this post do comment ,share and promote the post 🙏 . Please stay with us and support.  🙏


Post a Comment

0 Comments