Dictionary is a collection of key-value pairs.
Syntax:
''' a = {“key”: “value”, “marks” : “100”, “list”: [1,2,9]}
a[“key”] # Prints value
a[“list”] # Prints [1,2,9] '''
Properties of Python Dictionaries
- It is unordered
- It is mutable
- It is indexed
- Cannot contain duplicate keys
Dictionary Methods:
Consider the following dictionary,
a = {“name”: “Sri”,
“from”: “India”,
“marks”: [92,98,96]}
items() : returns a list of (key,value) tuple.
keys() : returns a list containing dictionary’s keys.
update({“friend”: “Sam”}) : updates the dictionary with supplied key-value pairs.
get(“name”) : returns the value of the specified keys (and value is returned e.g. “Sri” is returned here)
More methods are available on docs.python.org
Sets in Python:
Set is a collection of non-repetitive elements.
S= Set() # No repetition allowed!
S.add(1)
S.add(2)
# or Set = {1,2}
Properties of Sets:
- Sets are unordered # Elements order doesn’t matter
- Sets are unindexed # Cannot access elements by index
- There is no way to change items in sets
- Sets cannot contain duplicate values
Operations on Sets:
Consider the following set:
S = {1,8,2,3}
Len(s) : Returns 4, the length of the set
remove(8) : Updates the set S and removes 8 from S
pop() : Removes an arbitrary element from the set and returns the element removed.
clear() : Empties the set S
union({8, 11}) : Returns a new set with all items from both sets. #{1,8,2,3,11}
intersection({8, 11}) : Returns a set which contains only items in both sets. #{8}
Practice:
Write a program to create a dictionary of Bengali words with values as their English translation. Provide the user with an option to look it up!
Write a program to input eight numbers from the user and display all the unique numbers (once).
Answer: Use set
Can we have a set with 18(int) and “18”(str) as a value in it?
Answer: yes, because both have different types and value.
What will be the length of the following set S:
S = Set()
S.add(20)
S.add(20.0)
S.add(“20”)
What will be the length of S after the above operations?
Answer: 2
S = {}, what is the type of S?
Answer: Empty Dictionary
Can you change the values inside a list which is contained in set S
S = {8, 7, 12, “Sri”, [1, 2]}
Answer: No,
No comments:
Post a Comment