Responsive Ad Slot

Latest

latest

Python Dictionary and Set 5 | Learn With Pirates

Tuesday 27 April 2021

/ by Captain Jay's

Python Dictionary and Set | Learn With Pirates

Chapter 5: Python Dictionary and Set's



Dictionary in Python

Dictionary is a collection of key and value pair in python.
The syntax for the dictionary:
 
Pirates_Dic = {
	"Name": "Pirates",
	"phone":"Android",
	"Os" : "Linux",

}
print(Pirates_Dic['Name'])
print(Pirates_Dic['Os'])
print(Pirates_Dic["phone"])

Output:
Pirates
Linux
Android

   
       
 

Properties of Dictionaries

It unordered
It mutable
It indexed
It, not cantine duplicate keys

Dictionaries Methods

       

# Dictionary Methods
Pirates_Dic = {
    "Name": "Pirates",
    "phone":"Android",
    "Os" : "Linux",
    "Random": {'home':'hotel'},
    1:2
}
print(list(Pirates_Dic.keys())) # Prints the keys of the dictionary #1
print(Pirates_Dic.values()) # Prints the keys of the dictionary #2
print(Pirates_Dic.items()) # Prints the (key, value) for all contents of the dictionary  #3
print(Pirates_Dic) #4
print(Pirates_Dic.get("Os")) # Prints value associated with key "Os" #5

#update Dic

updateDict = {
    "CyberMentor": "Friend",
    "Oday": "Friend",
    "LA": "Friend",
    "Members": "A Family"
}
Pirates_Dic.update(updateDict) # Updates the dictionary by adding key-value pairs from updateDict                                 
print(Pirates_Dic) #6
print(Pirates_Dic.get("LA")) # Prints value associated with key Updated value "LA" #7
                               

Output:
['phone', 'Random', 'Os', 'Name', 1] #1

['Android', {'home': 'hotel'}, 'Linux', 'Pirates', 2] #2

[('phone', 'Android'), ('Random', {'home': 'hotel'}), 
('Os', 'Linux'), ('Name', 'Pirates'), (1, 2)] #3

{'phone': 'Android', 'Random': {'home': 'hotel'}, 
'Os': 'Linux', 'Name': 'Pirates', 1: 2} #4

Linux #5
 
{1: 2, 'Name': 'Pirates', 'LA': 'Friend', 'Random': {'home': 'hotel'}, 'phone': 'Android', 'Oday': 'Friend', 'Members': 'A Family', 'CyberMentor': 'Friend', 'Os': 'Linux'} #6

Friend #7

       
 


Set in Python

Set is a collection of  element
If you are not families with the set take it as a data type that contains a unique value. 
       
#set in Python
a = {1, 3, 4, 5, 1}
print(type(a))
print(a)

Output
type 'set'
set([1, 3, 4, 5])           
  
 

Properties of Set

Set is unorder
Set are unindexed
Once you make a set there is no chance to manipulate them
The set can not con

Set Methods

       
#set Properties
# Creating an empty set
a = set()
print(type(a))

## Adding values to an empty set
a.add(4)
a.add(4)
a.add(5)
a.add(5) # Adding a value repeatedly does not changes a set
a.add((4, 5, 6))

## Accessing Elements
# a.add({4:5}) # Cannot add list or dictionary to sets
print(a)

## Length of the Set
print(len(a)) # Prints the length of this set

## Removal of an Item
a.remove(5) # Removes 5 fromt set a
# a.remove(15) # throws an error while trying to remove 15 (which is not present in the set)
print(a)

print(a.pop())
print(a)

Output
type 'set'>
set([(4, 5, 6), 4, 5])
3
set([(4, 5, 6), 4])
(4, 5, 6)
set([4])

 

Termination:

If you Like this tutorial please share with your friend and family. I make an amazing tutorial in this blog. If you have any queries and question ask me in the comment section below.

No comments

Post a Comment

Don't Miss
© all rights reserved
made with by templateszoo