0% found this document useful (0 votes)
10 views10 pages

Python Session 9

The document provides an overview of data structures in Python, including lists, tuples, sets, and dictionaries. It explains the characteristics, syntax, and methods associated with each structure, highlighting their mutability and use cases. Additionally, it covers operations for sets and the functionality of dictionaries in managing key-value pairs.

Uploaded by

dahaf29062
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)
10 views10 pages

Python Session 9

The document provides an overview of data structures in Python, including lists, tuples, sets, and dictionaries. It explains the characteristics, syntax, and methods associated with each structure, highlighting their mutability and use cases. Additionally, it covers operations for sets and the functionality of dictionaries in managing key-value pairs.

Uploaded by

dahaf29062
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/ 10

Python (2 Months Bootcamp)

For Data Science


Session 9

©EmpowerMe Tech
Recap
Data Structures

✓ List

©EmpowerMe Tech
What is a Tuple

A tuple is like a list, but you can't change it.


It's immutable.

Feature List Tuple


Syntax [] ()
Mutable Yes No
Performance Slower Faster
Fixed/constant
Use case Changing data
data

©EmpowerMe Tech
List (mutable) Tuple (immutable)

fruits_list = ["apple", "banana", "cherry"] fruits_tuple = ("apple", "banana", "cherry")


print("Original List:", fruits_list) print("Original Tuple:", fruits_tuple)

# Let's change the second item # Let's try to change the second item
fruits_list[1] = "mango" fruits_tuple[1] = "mango" # This will cause an error
print("Updated List:", fruits_list)

©EmpowerMe Tech
What is a Set?

A set is a collection of unique and unordered items.

Syntax

my_set = {1, 2, 3, 3, 2, 1}
print(my_set) # Output: {1, 2, 3}

©EmpowerMe Tech
Set Methods (for working with one set)

Method Description Example


add(item) Adds an item my_set.add(5)
Removes item (error if not
remove(item) my_set.remove(2)
found)
Removes item (no error if
discard(item) my_set.discard(100)
not found)
pop() Removes a random item my_set.pop()
clear() Empties the entire set my_set.clear()
len(set) Returns number of elements len(my_set)

Example

my_set = {10, 20, 30}


my_set.add(40)
// {20, 30, 40}
my_set.remove(10)
print(my_set)
©EmpowerMe Tech
Set Operations (between two sets)

Union (| or union) Difference (- or difference)


Combines two sets (removes duplicates). Items in A but not in B.

A = {1, 2, 3} print(A - B) # OR // {1, 2}


B = {3, 4, 5} print(A.difference(B))
print(A | B) # OR // {1, 2, 3, 4, 5}
print(A.union(B))
Symmetric Difference (^ or
symmetric_difference)
Intersection (& or intersection)
Common elements in both sets. Items in A or B, but not both.

print(A & B) # OR // {3}


print(A ^ B) # OR
print(A.intersection(B))
print(A.symmetric_difference(B))

// {1, 2, 4, 5}

©EmpowerMe Tech
Dictionaries in Python
A dictionary is a collection of key-value pairs. Keys are like
labels; values are the data

Syntax

student = {
"name": "Areeba",
"age": 21,
"course": "Python Bootcamp"
}

©EmpowerMe Tech
Dictionary Methods

Method What It Does


Returns the value safely (or
get(key)
None)
keys() Returns all keys
values() Returns all values
items() Returns list of (key, value) pairs
Updates dictionary with another
update(dict2)
one
pop(key) Removes a key
clear() Removes all items

©EmpowerMe Tech
Data Structure Use Case Real-World Example
Ordered, changeable
List Shopping cart, to-do list
data
Ordered, unchangeable Days of the week, menu
Tuple
data options
Unordered, unique Unique email signups,
Set
values banned users
Key-value structured Student info, product
Dictionary
data details, user profiles

You might also like