SETS & DICTIONARY

 # Sets in python Date 01_07_2019

b = {}  # it is an empty set but it is not empty set is an empty Dictionary
print(type(b))  #class  <'dict'>

a = { 1,   2,   3,   4,   5 }

print(type(a))  # <Class 'set'>
print(a)    

a = { 1,   2,   3,   4,   5, 1 }
print(a)  # it ignore one 1 bc, set definition "Set is a collection of non repetitive elements"

# if we want to write an empty set use {}

#########################################
# Empty set

a = {}

print(a)
print (type(a)) # it is a dictionary type not a set

# an empty set can create using syntex
c =set()
print(type(c)) # to add some thing on set c we use .add function
c.add(3),
#c.add(9 ,5, 7) we cant add more then one element like this
c.add(5),
c.add(7),

print(c)

##############################################

# set methods

c =set()
print(type(c)) # to add something on set 'c' we use .add function
c.add(3),
#c.add(9 ,5, 7) we cant add more then one element like this ie cant add any list [3,4,5] ,
c.add(5), # list is changeable but set is no so we cant add list, but can add tuple c.add((4, 6, 8))
c.add(7),
print (c)  ## Accessing Element from set
c.add(7), # set is not allow repetition so only one 7 is produce as response {3,5,7}
c.add(7),  
c.add((4, 6, 8)) #tuple adds as a single element,,,, can we add dictionary
#c.add({2:6})  # unshushable type ie cant add dictionary "hasable means unalterable" , we can change its element
print(len(c))  #there is only 4 elements in this ,len() length of set

c.remove(5) # it remove 5, ie we can remove any element from set ut can not add
#c.remove(220) # throws an error trying to remove which is not present in this set
print(c)
# if we use A.pop() it remove randomly any element and return same which would be delete

print (c.pop()) # we use this to pick any arbitrary number from set and remove from set

############################### dictionary

myDict = {
    "fast": "In a quick Manner",
    "Score": [3, 5, 8, 4], #between a pair separate by a comma,

    "Sush": "A Physics and Mathematics Teacher , high school studies
is his channel where you can access all math's class for free",
 "Dict2": {'harry':'player'},

        '1' :  5  # 1 is key  & 5 is value

    }

print(myDict['fast']) # in this DIct I can access the value hold by a key , here key is "fast'
print(myDict['Score']) # in this DIct I can access the value hold by a key , here key is "fast'
print(myDict['Sush']) # in this DIct I can access the value hold by a key , here key is "fast'
print(myDict['Dict2']['harry']) #define inside a key-value
print(myDict['1'])

#################################3

#methods to chane and update and search inside dicionary

myDict = {"fast" : "in a quick manner",
   "sush" : "a teacher" ,
 "laptop" : "an electronic device",
 "Dict2" : {'sush' : 'student'},
  "1" : "5"}    
    #Dictionary Methods  
 


print(myDict.keys()) # print dictionary
print(list(myDict.keys())) # print dictionary in list form
print(type(myDict.keys())) # print dictionary type typecast
print(myDict.values()) # print dictionary
print(list(myDict.values())) # print dictionary
print(myDict.items()) # print dictionary  it is also a kind of list with (key-values) pair for all contents
# first print my dict then update it ie add a new kvpair

print(myDict) # print dictionary

updateDict ={  "red" : "color",
"english": "language"
} # new (key-values) pair to add in the existing Dict
myDict.update(updateDict)
print(myDict) # print dictionary

print(myDict.get('sush')) #both gives same result if the key is present in dict otherwise
print(myDict["sush"])   # if key is not present [] it shows an error but (.get()) provide a none type data

print(myDict.get('rush')) #  returns none as rush is not present
print(myDict['rush'])     # keyError : 'rush'


# to know more go to google and search Dictionary methods on Python

########################################## examples:

#''' a program to create a dictionary if Hindi words with value
#as their English translation. Provide user with an option to look it up'''

myDict = {
    "kalam" : "pen",
    "chawal" : 'rice',
    "Jaal" : "net",
    "Kitab" : "book",
    "Paani" : " Water",
    "Barf" : "ice",
    "Hawa": "Air",
    "khushi" : "Happy",
}

print(type(myDict)) # to verify whether myDict syntax is correct or not
print(myDict)


print("options are", myDict.keys()) # first see the key list

a = input("enter the hindi word  >")
#print("The meaning of your word in English is : ", myDict[a])
# if key is not present in Dict it throw an error to avoid error we use __.get() function it returns "None"

print("The meaning of your word in English is : ", myDict.get(a) )

#######################################01_07_2019
#A program to input eight numbers from the user and display all the unique numbers (once), "unique number" means A "set"

Num1 = input(" Enter number 1  >")
Num2 = input(" Enter number 2   >")
Num3 = input(" Enter number 3  >") ## input is use to call a data from user,
Num4 = input(" Enter number 4  >")
Num5 = input(" Enter number 5   >")
Num6 = input(" Enter number 6   >")
Num7 = input(" Enter number 7   >")
Num8 = input(" Enter number 8   >")

a = {Num1, Num2, Num3, Num4, Num5,Num6, Num7, Num8}

print(a)

##########################################

# can we have a set with 15int and 15str as a value in it

a = { 15, "15", 15.5} # solved  15 and "15"
print(a) # it print all three cc first is int, second is str, third is float. all three inputs are are unique

####################################3

# what will be the length of set s

s= set()  # set() is an empty set but s={} is an empty Dictionary
print(len(s)) # zero length

s.add(20)  # it is an int
print(len(s))
s.add("20")  # it is a string
print(len(s))
s.add(20.0) # 20 and 20.0 are same  so count is  both are int
print(len(s))

#############################
# creat an empty dictionary. Allow 4friends to enter their Favorite Language
##as value and use key as their names. assume that the names are unique.


from flask import has_app_context


favLang = {}

a = input(" enter your favorite language Sush   \n   >")
b = input(" enter your favorite language Nagar  \n    >")
c = input(" enter your favorite language Dinu   \n    >")
d = input(" enter your favorite language Pochi  \n   >")

favLang['Sush'] =  a
favLang['Nagar'] = b
favLang['Dinu'] =  c
favLang['Pochi'] =  d


print(favLang) # returnd as keys-value pair

#if two names are same what happen.

favLang['Sush'] =  a
favLang['Nagar'] = b
favLang['Dinu'] =  c
favLang['Sush'] =  d
print(favLang)  # it returns last updated value entered corresponding

# if two persons have same Language

favLang['Sush'] =  a
favLang['Nagar'] = b
favLang['Dinu'] =  c
favLang['Pochi'] =  d
print(favLang)
 
 #returns same \\ Keys must be unique but not the values
 # (0, 0,4, ) this is tuple  #in set we can not enter any list we can add tuple, but cant change any element of set
 #[2,3,4]  is a list


################################################## good Day

Comments

Popular posts from this blog

Game_01_Snake_Water_Gun_or_Rock_Paper_Scissor

String Function

Conditional Expression