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

Lab# 11 and 12 Set

The document provides an overview of Python sets, which are unordered, unchangeable collections that do not allow duplicate values. It explains how to access, add, and remove items from sets, as well as the use of the len() function to determine the number of items. Additionally, it highlights that sets can contain different data types and can be manipulated using various methods such as add(), update(), remove(), and pop().

Uploaded by

rabiaalam276
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 views6 pages

Lab# 11 and 12 Set

The document provides an overview of Python sets, which are unordered, unchangeable collections that do not allow duplicate values. It explains how to access, add, and remove items from sets, as well as the use of the len() function to determine the number of items. Additionally, it highlights that sets can contain different data types and can be manipulated using various methods such as add(), update(), remove(), and pop().

Uploaded by

rabiaalam276
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/ 6

LAB WORK: SET

DEPARTMENT OF COMPUTER SCIENCE


GCWUF
Python Set
✓ Sets are used to store multiple items in a single variable.
✓ Set is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Tuple, and Dictionary, all with different qualities and usage.
✓ A set is a collection that is unordered, unchangeable, and unindexed.
✓ Sets are written with curly brackets

Set Items
✓ Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
✓ Unordered means that the items in a set do not have a defined order.
✓ Set items can appear in a different order every time you use them, and cannot be referred to by
index or key.
Unchangeable
✓ Set items are unchangeable, meaning that we cannot change the items after the set has been
created.
Duplicates Not Allowed
✓ Sets cannot have two items with the same value.

✓ The values True and 1 are considered the same value in sets, and are treated as duplicates.

✓ The values False and 0 are considered the same value in sets, and are treated as duplicates
Get the Length of a Set

✓ To determine how many items a set has, use the len() function.

Set Items - Data Types

✓ Set items can be of any data type

✓ A set can contain different data types.

type()

✓ From Python's perspective, sets are defined as objects with the data type 'set'
Python - Access Set Items
Access Items

✓ You cannot access items in a set by referring to an index or a key.


✓ But you can loop through the set items using a for loop, or ask if a specified value is present
in a set, by using the in keyword.

Python - Add Set Items


Add Items

✓ Once a set is created, you cannot change its items, but you can add new items.
✓ To add one item to a set use the add() method.

Add Sets

✓ To add items from another set into the current set, use the update() method.
Add Any Iterable

✓ The object in the update() method does not have to be a set, it can be any iterable object
(tuples, lists, dictionaries etc.)

Python - Remove Set Items

Remove Item

✓ To remove an item in a set, use the remove(), or the discard() method.

✓ You can also use the pop() method to remove an item, but this method will remove a
random item, so you cannot be sure what item that gets removed.
✓ The return value of the pop() method is the removed item

You might also like