Dictionary is a collection of Key value pairs.
Syntax :
a = { "key" : "value",
"cstechiie" : "code',
"marks" : "100",
"list" : [1,2,9]}
a[ "key"] = prints its "value"
a["list"] = prints [1,2,9]
Properties of Python Dictionaries :
1. It is unordered
2. It is mutable
3. It is indexed
4. cannot contain duplicate keys
Dictionary Methods :
Consider the following dictionary;
a={"name" : "name",
"from" : "India",
"marks" : [92,96,98]}
1. a.items( ) : returns a list of (key, value) tuples.
2. a.keys( ) : returns a list containing dictionary keys
3. a.update({"friend" : "sam"}) : updates the dictionary wih supplied key value pairs
4. a.get("name") : returns the value of the specified keys and value is returned
**More methods ae available on docs.python.org
Sets in Python : Set is a collection of non repetitive elements.
s = set ( ) : no repetitive allowed
s.add(1)
s.add(2) : set = {1,2}
If you are a programming beginner without much knowledge of mathematical operations on sets, you can simply look at sets in python as data types containing unique values.
Properties of Sets :
1. sets are unordered : Elements order doesn't matter
2. sets are unindexed : cannot access elements by index
3. there is no way to change items in sets
4. sets cannot contain duplicate values
2. sets are unindexed : cannot access elements by index
3. there is no way to change items in sets
4. sets cannot contain duplicate values
operations on sets :
consider the following set : s = {1, 8, 2, 3}
1. lens(s) : returns the length of the set i.e 4
2. s.remove(8) : updates the set "S" and remove 8 from s.
3. s.pop ( ) : removes an arbitrary element from the set and returns the
element removed
4. s.clear ( ) : Empties the set s
5. s.union({8,11}) : returns a new set with all items from both sets i.e
{1,8,2,3,11}
{1,8,2,3,11}
6. s.intersection ({8,13}) : returns a set which contains only items in both sets i.e {8}
![]() |
Intersection |
Programs :
1. #Dictionary Syntax
myDict = {
"Fast": "In a Quick Manner",
"cstechiie": "A Coder",
"Marks": [1, 2, 5],
"anotherdict": {'cstechiie': 'Player'}
}
# print(myDict['Fast'])
# print(myDict['cstechiie'])
myDict['Marks'] = [45, 78]
print(myDict['Marks'])
print(myDict['anotherdict']['cstechiie'])
2. #Dictionary Methods
myDict = {
"fast": "In a Quick Manner",
"cstechiie": "A Coder",
"marks": [1, 2, 5],
"anotherdict": {'cstechie': 'Player'},
1: 2
}
# Dictionary Methods
print(list(myDict.keys())) # Prints the keys of the dictionary
print(myDict.values()) # Prints the keys of the dictionary
print(myDict.items()) # Prints the (key, value) for all contents of the dictionary
print(myDict)
updateDict = {
"Lovish": "Friend",
"Divya": "Friend",
"Shubham": "Friend",
"harry": "A Dancer"
}
myDict.update(updateDict) # Updates the dictionary by adding key-value pairs from updateDict
print(myDict)
print(myDict.get("cstechiie")) # Prints value associated with key "harry"
print(myDict["cstechiie"]) # Prints value associated with key "harry"
# The difference between .get and [] sytax in Dictionaries?
print(myDict.get("cstechiie2")) # Returns None as harry2 is not present in the dictionary
print(myDict["cstechiie2"]) # throws an error as harry2 is not present in the dictionary
3. #Sets
a = {1, 3, 4, 5, 1}
print(type(a))
print(a)
4. #Set Methods
# Creating an empty set
b = set()
print(type(b))
## Adding values to an empty set
b.add(4)
b.add(4)
b.add(5)
b.add(5) # Adding a value repeatedly does not changes a set
b.add((4, 5, 6))
## Accessing Elements
# b.add({4:5}) # Cannot add list or dictionary to sets
print(b)
## Length of the Set
print(len(b)) # Prints the length of this set
## Removal of an Item
b.remove(5) # Removes 5 fromt set b
# b.remove(15) # throws an error while trying to remove 15 (which is not present in the set)
print(b)
print(b.pop())
print(b)