0% found this document useful (0 votes)
2 views

Day5 Python

Chapter 5 covers dictionaries and sets in Python, detailing their properties, methods, and operations. Dictionaries are collections of key-value pairs that are unordered, mutable, and indexed, while sets are collections of unique, unordered elements. The chapter also includes practice exercises to reinforce understanding of these concepts.

Uploaded by

vikramsen7974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Day5 Python

Chapter 5 covers dictionaries and sets in Python, detailing their properties, methods, and operations. Dictionaries are collections of key-value pairs that are unordered, mutable, and indexed, while sets are collections of unique, unordered elements. The chapter also includes practice exercises to reinforce understanding of these concepts.

Uploaded by

vikramsen7974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CHAPTER 5 – DICTIONARY & SETS

Dictionary is a collection of keys-value pairs.

Syntax:

a = {
"key": "value",
"harry": "code",
"marks": "100",
"list": [1, 2, 9]
}

print(a["key"]) # Output: "value"


print(a["list"]) # Output: [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":"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).

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}

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}

• len(s): Returns 4, the length of the set


• s.remove(8): Updates the set s and removes 8 from s.
• s.pop(): Removes an arbitrary element from the set and return the element
removed.
• s.clear():empties the set s.
• s.union({8,11}): Returns a new set with all items from both sets. {1,8,2,3,11}.
• s.intersection({8,11}): Return a set which contains only item in both sets {8}.

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

You might also like