0% found this document useful (0 votes)
7 views18 pages

Set

The document provides an overview of sets and tuples in Python, detailing their declaration, modification methods, and operations. It explains key methods for manipulating sets, such as .add(), .remove(), and .union(), as well as the immutable nature of tuples. Additionally, it highlights the differences between lists and tuples, emphasizing that lists are mutable while tuples are not.

Uploaded by

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

Set

The document provides an overview of sets and tuples in Python, detailing their declaration, modification methods, and operations. It explains key methods for manipulating sets, such as .add(), .remove(), and .union(), as well as the immutable nature of tuples. Additionally, it highlights the differences between lists and tuples, emphasizing that lists are mutable while tuples are not.

Uploaded by

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

TOPIC:

SET
1. Set declaration

2. Change the set

3. Operation on sets
1. Set declaration
Sets in Python have some properties that you
need to remember:
•The elements in a set are not ordered.
•These elements are unique, no repetitions
allowed.
•Set can be changed (add or remove elements)
but the elements of the set must be immutable (ie
the memory capacity can be determined right
when declared).
Example:
2. Change the set
-The .add() method Example:
Method used to add an element
to a set.

-The .remove() method


Remove an element from a Example:
set.
2. Change the set
-The .discard() method
Like the .remove() method it removes an element from a collection,
however if the element does not exist it does not throw any error.

Example:
2. Change the set
-The .pop() method Example:
Remove a random
element from the set.

The .clear() method Example:


Remove all elements
from a set, then the set
is called the empty set.
2. Change the set
The .update() method
The .add() method above can
only add 1 element to a set Example:
with 1 statement, to add
multiple elements, we
use .update(). Note, the input
of .update() can be a Set, a
List or a Tuple.
3. Operation on sets
Example:
-Union: the union of two sets gives
the result of all the elements in the
two sets, note that any repeated
element will appear only once in the
result set. In Python, to perform
union, we use the .union() method.

-Difference: The difference


of a set A minus a set B Example:
gives the result of all the
elements that belong to A
but not to B. Use
the .difference() method to
subtract two sets
3. Operation on sets
- Symmetric difference of two sets:
The symmetric difference of two sets
A and B is the set of elements that
belong to both A and B but not to
both sets A and B.
The .symmetric_difference() method
returns the symmetric difference of
two sets.

- symmetric_difference_update()
This method is a combination
of .symmetric_difference()
and .update(). It performs symmetric
subtraction of the two previous sets,
and updates the result to the target
set.
3. Operation on sets
- “Frozen” Set: Python
provides a function
called frozenset(),
which returns an
immutable set. If you
use the methods .add(),
.remove(), .update()…
it will give an error.
- “Freezing” a set will
make it look like a
Tuple structure in
Python.
TUPLE
1. Tuple declaration

2. Change and use some


functions in a tuple

3. Relationship
1. Tuple declaration
To write a Tuple, you put the elements Example:
in parentheses (), everything in the
parentheses is the elements of the
Tuple, separated by commas
NOTE:
2. Change and use some functions in a tuple
- Add data to Example:
tuple :

- Use count
funcion in Example:
tuple:
2. Change and use some functions in a tuple
- Use len() funcion in Example:
tuple:To determine
how many items a
tuple has, use the
len() function

- Note the double


round-brackets:
Example:
3. Relationship
Use list() + sum() to find the sum of a Tuple
in Python.
Example:
Compare list and tuple
The most basic difference between list and tuple is: List data
can be changed, tuple data cannot be changed.

- This is a typical example:

You might also like