[go: up one dir, main page]

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

Set and Dictonary

The document provides an overview of sets and dictionaries in Python, defining them as collections of unique elements and key-value pairs, respectively. It includes syntax for creating, accessing, updating, and deleting elements in both data structures, along with examples and intermediate questions for practice. The information is structured to facilitate understanding of how to effectively use sets and dictionaries in programming.

Uploaded by

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

Set and Dictonary

The document provides an overview of sets and dictionaries in Python, defining them as collections of unique elements and key-value pairs, respectively. It includes syntax for creating, accessing, updating, and deleting elements in both data structures, along with examples and intermediate questions for practice. The information is structured to facilitate understanding of how to effectively use sets and dictionaries in programming.

Uploaded by

since2024gaming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SET

1. Define
- A set is a collection of unique and unordered elements.

2. Explain What and Why SET

- What: A set stores multiple items in a single variable. It does


not allow duplicate values.
- Why: Sets are used when you want to store unique
elements and perform operations like union, intersection, etc.

3. Syntax
python
my_set = {element1, element2, element3,..........element n}

4. Create Set
- Use curly braces {} or the set() function.
python
my_set = {1, 2, 3} Using curly braces
my_set = set([1, 2, 3]) Using set() function

5. Access Set
- Sets are unordered, so you cannot access elements by
index. Use loops to access elements.
python
for item in my_set:
print(item)
6. Update Set
- Add elements using add() or update multiple elements using
update().
python
my_set.add(4) Adds a single element
my_set.update([5, 6]) Adds multiple elements

7. Delete Set
- Remove elements using remove(), discard(), or pop(). Clear
the set using clear() or delete it using del.
python
1.​my_set.remove(2) Removes a specific element
2.​my_set.discard(3) Removes an element (no error if not
found)
3.​my_set.pop() Removes a random element
4.​my_set.clear() Removes all elements
5.​del my_set Deletes the entire set
5 Solvable Examples (Set)

1. Create a set of fruits: {"apple", "banana", "cherry"} and add


"orange" to it.
2. Remove "banana" from the set {"apple", "banana",
"cherry"}.
3. Check if "apple" is present in the set {"apple", "banana",
"cherry"}.
4. Find the union of two sets: {1, 2, 3} and {3, 4, 5}.
5. Clear all elements from the set {10, 20, 30}.
DICTIONARY

1. Define
- A dictionary is a collection of key-value pairs.

2. Explain What and Why Dictionary


- What: A dictionary stores data in the form of key: value
pairs. Keys must be unique.
- Why: Dictionaries are used to store and retrieve data
efficiently using keys.

3. Syntax
python
my_dict = {"key1": "value1", "key2": "value2"}

4. Create Dictionary
- Use curly braces {} with key: value pairs or the dict()
function.
python
my_dict = {"name": "Alice", "age": 25} Using curly braces
my_dict = dict(name="Alice", age=25) Using dict() function

5. Access Dictionary
- Access values using keys.
python
print(my_dict["name"]) Output: Alice
6. Update Dictionary
- Add or modify key-value pairs.
python
my_dict["age"] = 30 Updates the value of "age"
my_dict["city"] = "New York" Adds a new key-value pair

Example:
student = {'name': 'John', 'age': 20, 'grade': 'A'}
student['age'] = 21 # updating the value of 'age'
print("Updated dictionary:", student)

Output: Updated dictionary: {'name': 'John', 'age': 21}


In [6]:

7. Delete Dictionary
- Remove key-value pairs using pop(), popitem(), or del. Clear
the dictionary using clear() or delete it using del.
python
1.​my_dict.pop("age") Removes the key "age"
2.​my_dict.popitem() Removes the last inserted key-value
pair
3.​del my_dict["name"] Removes the key "name"
4.​my_dict.clear() Removes all key-value pairs
5.​del my_dict Deletes the entire dictionary
5 Solvable Examples (Dictionary)

1. Create a dictionary with keys "name", "age", and "city".


2. Access the value of the key "age" in the dictionary {"name":
"Alice", "age": 25}.
3. Add a new key-value pair "country": "USA" to the dictionary
{"name": "Alice", "age": 25}.
4. Remove the key "age" from the dictionary {"name": "Alice",
"age": 25}.
5. Clear all key-value pairs from the dictionary {"name":
"Alice", "age": 25}.
Intermediate Questions

Set
1. Find the intersection of two sets: {1, 2, 3} and {3, 4, 5}.
2. Check if {1, 2} is a subset of {1, 2, 3, 4}.
3. Remove a random element from the set {10, 20, 30, 40}.
4. Add multiple elements [5, 6, 7] to the set {1, 2, 3}.
5. Convert a list [1, 2, 2, 3, 4] into a set and print it.

Dictionary
1. Merge two dictionaries: {"a": 1, "b": 2} and {"c": 3, "d": 4}.
2. Check if the key "name" exists in the dictionary {"name":
"Alice", "age": 25}.
3. Update the value of the key "age" to 30 in the dictionary
{"name": "Alice", "age": 25}.
4. Remove the last inserted key-value pair from the dictionary
{"name": "Alice", "age": 25, "city": "New York"}e.
5. Create a dictionary where keys are numbers from 1 to 5
and values are their squares.

You might also like