0% found this document useful (0 votes)
11 views29 pages

Sets

The document provides an overview of sets and dictionaries in Python, detailing their characteristics, creation methods, and common operations. It explains how to manipulate sets and dictionaries, including adding, removing elements, and using various methods. Additionally, it includes examples of operations such as finding sums, maximums, minimums, and intersections of sets.

Uploaded by

gowthamibrn36
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)
11 views29 pages

Sets

The document provides an overview of sets and dictionaries in Python, detailing their characteristics, creation methods, and common operations. It explains how to manipulate sets and dictionaries, including adding, removing elements, and using various methods. Additionally, it includes examples of operations such as finding sums, maximums, minimums, and intersections of sets.

Uploaded by

gowthamibrn36
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/ 29

Sets

What is A Set ?
What is A Set ?
Sets in Python are an unordered collection of unique elements
Defined using curly braces {} or the set()
Mutable
Duplicate elements are automatically removed.
Creating Sets
Create a set by enclosing elements within curly
braces {}. Alternatively, you can use the set()
Adding Elements
add() method to add a single element to a set

Removing Elements
remove elements using the remove() or
discard() methods.
Set Operations
Union
Intersection
Difference
Symmetric difference
Set Operations

Union Intersection

Difference Symmetric difference


Set Operations
Set Membership and Length
You can check if an element is present in a set using the in
keyword. The len() function gives the number of elements in
the set.
Frozen Sets
Frozen Sets
A frozenset is an immutable version of a set

Set Comprehensions
Like lists and dictionaries, sets also support
comprehensions for concise set creation.
Set Methods
Some common set methods include clear(), copy(),
pop(), update(), etc.
Dictionary
Defnition
Defnition
A dictionary is an unordered collection of key-value pairs.
Are used to store data in the form of key-value pairs, where
each key is unique.
Creating Dictionaries
To create a dictionary, you use curly braces {} and specify
key-value pairs separated by colons :. Keys and values
can be of any data type.
Creating Dictionaries
Accessing Values
You can access the values in a dictionary using square
brackets [] with the key.
Adding Values
To add new key-value pairs or update existing ones in a
dictionary, you can use square brackets [] and the assignment
operator =.
Modifying Values
You can change the value associated with a key in a
dictionary.
Methods
Dictionaries have several useful methods,
keys()
values()
items()
get()
pop()
update()
Looping
You can use loops to iterate through the keys or values of a
dictionary
Comprehensions
Similar to lists, dictionaries also support comprehensions for
concise dictionary creation
Find the sum of all elements in a
given list of numbers.
Sample Input: [10, 20, 30, 40, 50]

Sample Output: Sum of elements = 150


Find the maximum and minimum
values in a list of numbers.
Sample Input: 15, 2, 7, 25, 10

Sample Output: Maximum = 25,


Minimum = 2
Count the number of occurrences
of a specific element in a list.

Sample Input: [1, 2, 3, 2, 1, 4, 2, 5]


2

Sample Output: Count of 2 = 3


Given two sets, find their intersection
(common elements) and union (all unique
elements combined).

Sample Input: Set A: {1, 2, 3, 4, 5}


Set B: {4, 5, 6, 7, 8}

Sample Output: Intersection: {4, 5}


Union: {1, 2, 3, 4, 5, 6, 7, 8}
Create dictionaries, access values,
update values, and iterate through
key-value pairs.
Sample Input:
my_dict = { 'name': 'John', 'age': 30, 'city': 'New York' }

Sample Output: name : John


age : 31
city : San Francisco

You might also like