0% found this document useful (0 votes)
2 views7 pages

Module 5

This document provides a concise overview of key concepts related to Sets and Dictionaries in Python, including their creation, operations, and methods. It highlights important points for exam preparation, such as the characteristics of Sets, various set operations, and how to manipulate Dictionaries. Additionally, it includes a list of frequently asked questions and a one-minute revision section summarizing the essential topics.

Uploaded by

avp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Module 5

This document provides a concise overview of key concepts related to Sets and Dictionaries in Python, including their creation, operations, and methods. It highlights important points for exam preparation, such as the characteristics of Sets, various set operations, and how to manipulate Dictionaries. Additionally, it includes a list of frequently asked questions and a one-minute revision section summarizing the essential topics.

Uploaded by

avp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programming – Module 5

Very Important Short Points for Exam


Based on the attached PDF.
1. Sets in Python
 A Set is an unordered, mutable collection of unique elements.
 Sets do not allow duplicate values.
 Created using {} or set().
 Sets are mainly used for removing duplicates and mathematical set
operations.

2. Creating Sets
my_set = {1,2,3}
my_set = set([1,2,3])
 Elements are unordered.
 Indexing is not supported.

3. Set Operations
 Union (|) → Combines two sets.
 Intersection (&) → Common elements.
 Difference (-) → Elements in first set only.
 Symmetric Difference (^) → Elements in either set but not both.

4. Set Methods
 add() → Add one element.
 update() → Add multiple elements.
 remove() → Remove an element (raises error if absent).
 discard() → Remove an element (no error if absent).
 pop() → Removes and returns an arbitrary element.

5. Accessing Sets
 Cannot use indexing.
 Access elements using a for loop.
 Membership test using in operator.

6. Dictionary
 Dictionary stores data as Key : Value pairs.
 Keys must be unique and immutable.
 Dictionaries are mutable.
 From Python 3.7, dictionaries preserve insertion order.

7. Creating Dictionary
student = {"name":"Rahul","age":20}
or
student = dict(name="Rahul", age=20)

8. Accessing Dictionary Elements


 dict[key]
 get(key) → Safer method (returns default/None if key not found).

9. Modifying Dictionary
 Add → dict[key]=value
 Update → Assign new value to existing key.
 Delete → del
 pop() → Remove and return value.
 popitem() → Remove last key-value pair.
 clear() → Remove all items.

10. Dictionary Methods


 keys()
 values()
 items()
 get()
 pop()
 popitem()
 clear()

11. Looping Through Dictionary


 for key in dict
 for value in [Link]()
 for key, value in [Link]()

12. Nested Dictionary


 Dictionary inside another dictionary.
 Used for hierarchical data.
 Access using chained keys.
Example:
student["marks"]["math"]

13. Dictionary Comprehension


Syntax:
{key:value for key,value in iterable if condition}
Used to create dictionaries in one line.

14. Dictionary Merging


merged = {**dict1, **dict2}

Top 30 FAQ for Exam


1. What is a Set?
An unordered, mutable collection of unique elements.
2. Do sets allow duplicate values?
No.
3. Can sets be indexed?
No.
4. How do you create a set?
Using {} or set().
5. Which operator performs union?
|
6. Which operator performs intersection?
&
7. Which operator performs difference?
-
8. Which operator performs symmetric difference?
^
9. Which method adds one element to a set?
add()
10. Which method adds multiple elements?
update()
11. Difference between remove() and discard()?
 remove() raises an error if the element is absent.
 discard() does not raise an error.
12. Which method removes an arbitrary element?
pop()
13. Which operator checks membership in a set?
in
14. What is a Dictionary?
A mutable collection of key-value pairs.
15. What must be unique in a dictionary?
Keys.
16. Can dictionary values be duplicated?
Yes.
17. How do you create a dictionary?
Using {} or dict().
18. How do you access dictionary values?
dict[key] or get().
19. Why is get() preferred?
It avoids KeyError by returning a default value if the key is missing.
20. How do you add or update a key-value pair?
dict[key] = value
21. Which statement deletes a dictionary item?
del
22. Which method removes and returns a value?
pop()
23. Which method removes the last inserted key-value pair?
popitem()
24. Which method removes all dictionary items?
clear()
25. Which method returns all keys?
keys()
26. Which method returns all values?
values()
27. Which method returns key-value pairs?
items()
28. What is a Nested Dictionary?
A dictionary containing another dictionary.
29. What is Dictionary Comprehension?
A concise way to create dictionaries using a single expression.
30. How do you merge two dictionaries?
Using:
{**dict1, **dict2}

One-Minute Revision
 Set → Unordered, Mutable, Unique
 No duplicates
 No indexing
 {} / set()
 |
 &
 -
 ^
 add()
 update()
 remove()
 discard()
 pop()
 in
 Dictionary → Key : Value
 Keys → Unique & Immutable
 Values → Can repeat
 get()
 keys()
 values()
 items()
 del
 pop()
 popitem()
 clear()
 Nested Dictionary
 Dictionary Comprehension
 Dictionary Merging {**dict1, **dict2}
These are the highest-probability topics for Module 5 and cover the most likely
MCQs, short-answer questions, and 5-mark university exam questions.

You might also like