Day5 Python
Day5 Python
Syntax:
a = {
"key": "value",
"harry": "code",
"marks": "100",
"list": [1, 2, 9]
}
DICTIONARY METHODS
Consider the following dictionary.
a={"name":"harry"
"from":"india"
"marks":[92,98,96]}
• a.items(): Returns a list of (key,value)tuples.
• a.keys(): Returns a list containing dictionary's keys.
• a.update({"friends":}): Updates the dictionary with supplied key-value pairs.
• a.get("name"): Returns the value of the specified keys (and value is returned
eg."harry" is returned here).
SETS IN PYTHON.
Set is a collection of non-repetitive elements.
20
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 => Element’s 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.
OPERATIONS ON SETS
Consider the following set:
s = {1,8,2,3}
21
CHAPTER 5 – PRACTICE SET
1. Write a program to create a dictionary of Hindi words with values as their English
translation. Provide user with an option to look it up!
2. Write a program to input eight numbers from the user and display all the unique
numbers (once).
3. Can we have a set with 18 (int) and '18' (str) as a value in it?
4. What will be the length of following set s:
s = set()
s.add(20)
s.add(20.0)
s.add('20') # length of s after these operations?
5. s = {}
What is the type of 's'?
6. Create an empty dictionary. Allow 4 friends to enter their favorite language as
value and use key as their names. Assume that the names are unique.
7. If the names of 2 friends are same; what will happen to the program in problem
6?
8. If languages of two friends are same; what will happen to the program in problem
6?
9. Can you change the values inside a list which is contained in set S?
s = {8, 7, 12, "Harry", [1,2]}
22