Data Science ASSIGNMENT - 2 E0122048
Data Science ASSIGNMENT - 2 E0122048
The one Python data structure that you’ll use the most in your code is the Python list. So, it is
essen al to prac ce problems that require using it.
Exercise #1
Problem: Create a list of numbers and find the sum of all elements.
numbers = [1, 2, 3, 4, 5]
total = sum(numbers)
print(total)
Exercise #2
Problem: Remove duplicates from a list and make a unique Python list.
lst = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(lst))
print(unique_list)
Exercise #3
lst = []
print(len(lst) == 0)
Exercise #4
lst = [1, 2, 3, 4, 5]
reversed_list = lst[::-1]
print(reversed_list)
Exercise #5
Solu on:
It is a data structure that has its concept borrowed from the mathema cal term called the
set. Python set has similar proper es as they are in Maths. So, we can confidently say this about
the sets in Python:
Sets in mathema cs are collec ons of dis nct elements without any specific order. Similarly,
in Python, a set is an unordered collec on of unique elements.
Exercise #6
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1.union(set2))
print(set1.intersection(set2))
Exercise #7
set1 = {1, 2}
set2 = {1, 2, 3, 4}
print(set1.issubset(set2))
Exercise #8
s = {1, 2, 3, 4}
s.discard(3)
print(s)
Exercise #9
Context: Understand how to find elements that exist in one set but not in another.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
print(set1.difference(set2))
Exercise #10
set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(len(set1.intersection(set2)) > 0)
Dic onary Opera ons:
Learning dic onaries in Python is crucial because they help you quickly find informa on using
keys, handle different types of data, and mimic real-life connec ons. Dic onaries are widely
used in Python, making them an essen al tool for problem-solving in various applica ons.
Dic onaries in Python act like real-world dic onaries. They store informa on as key-value pairs,
allowing quick and efficient access to data. Think of a dic onary as a dynamic tool for organizing
and managing informa on in Python. It’s a versa le and essen al feature, making tasks like
retrieval, inser on, and dele on of data a breeze.
Exercise #11
Problem: Create a dic onary and access its values using keys.
Exercise #12
Exercise #13
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
merged = {**d1, **d2}
print(merged)
Exercise #14
del d['city']
print(d)
Exercise #15
Solu on:
Tuples in Python are like unchangeable lists. They allow you to store a collec on of items, and
once created, their values cannot be modified. Tuples are handy for situa ons where you want
to ensure data integrity or create a set of values that should stay constant throughout your
program. They’re lightweight, easy to use, and offer a straigh orward way to structure data in
Python.
Learning Python tuples is helpful because they keep data safe from accidental changes, can be
faster in some cases, and work well with func ons that provide mul ple results. They’re handy
when you want stability in your data or when dealing with func ons that use tuples.
Exercise #16
t1 = (1, 2, 3)
t2 = (4, 5)
concatenated = t1 + t2
print(concatenated)
Exercise #17
Context: Learn how to retrieve elements from a tuple using nega ve indices.
Exercise #18
t = (1, 2, 3, 4, 5)
print(len(t))
Exercise #19
print('b' in t)
print('x' in t)
Exercise #20
t = (1, 2, 3, 4)
lst = list(t)
print(lst)
These exercises cover a range of opera ons on Python data structures, providing a solid
founda on for working with lists, sets, dic onaries, and tuples. Feel free to explore and modify
them to deepen your understanding of Python’s data manipula on capabili es.
Here is the next set of exercises con nuing from Exercise #21 to Exercise #50:
Exercise #21
Context: Learn how to count the number of mes a specific element appears in a tuple.
t = (1, 2, 2, 3, 2)
print(t.count(2))
Exercise #22
Problem: Create a tuple and find the minimum and maximum values.
t = (5, 3, 9, 1, 7)
print(min(t))
print(max(t))
Exercise #23
t = (4, 4, 4, 4)
print(len(set(t)) == 1)
Exercise #24
Exercise #25
List Comprehension:
List comprehension in Python is a concise and expressive way to create lists. It allows you to
generate a new list by applying an expression to each item in an exis ng iterable (like a list or
range). This powerful feature enhances code readability and simplifies the process of crea ng
lists, making it a valuable skill for efficient and clean Python programming.
Learning list comprehension in Python is important because it helps you write shorter, clearer,
and more efficient code for crea ng and modifying lists. It’s like a shortcut that makes your
Python programs neater and easier to understand.
Exercise #26
Exercise #27
print(odds)
Exercise #28
Context: Prac ce crea ng tuples and combining them in a list using list comprehension.
print(tuples)
Exercise #29
print(excluded_evens)
Dic onary comprehension in Python is a quick and neat way to create dic onaries. It lets you
build a new dic onary by specifying key-value pairs using a simple expression applied to each
item in an exis ng collec on. It’s like a shortcut to make your Python code for crea ng
dic onaries shorter and easier to understand.
Learning dic onary comprehension in Python is important because it also helps you write
shorter and clearer code for crea ng dic onaries. It makes your Python programs more efficient
and follows the way Python likes things to be done. It’s like a handy tool to make your code
neater and smarter.
Exercise #31
Problem: Create a dic onary with keys as numbers and values as their squares using dic onary
comprehension.
print(dict31)
Exercise #32
Problem: Filter a dic onary to exclude keys divisible by 3 using dic onary comprehension.
print(dict32)
Exercise #33
Problem: Swap keys and values in a dic onary using dic onary comprehension.
print(dict33)
Exercise #34
Context: Prac ce combining the contents of two dic onaries using comprehension.
d1 = {1: 1, 2: 4}
d2 = {3: 9, 4: 16}
dict34 = {k: v for d in (d1, d2) for k, v in d.items()}
print(dict34)
Exercise #35
Problem: Create a dic onary excluding keys with values less than 3 using dic onary
comprehension.
Set Comprehension:
Set comprehension in Python is a quick way to create sets. It lets you make a new set by using a
simple expression for each item in an exis ng collec on. It’s like a shortcut to create sets easily
and neatly in Python.
Doing exercises on set comprehension in Python is good because it helps you write code that’s
short and easy to understand. It’s like a handy tool to create unique sets clearly and prac cally,
making your Python programming experience smoother.
Exercise #36
print(squares)
Exercise #37
print(filtered)
Exercise #38
Problem: Create a set of common elements between two sets using set comprehension.
Context:
Learn how to find the intersec on of two sets using set comprehension.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
Exercise #39
Context: Prac ce extrac ng lengths of words and crea ng a set using comprehension.
Problem: Create a set of vowels from a given string using set comprehension.
Context: Understand how to extract specific characters from a string using comprehension.
Doing advanced data structure exercises in Python is great because it helps your coding to get
be er by solving trickier problems. It’s like adding cool tools to your coding toolbox, making
your programming skills stronger and more useful for real-life situa ons.
Exercise #41
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
transposed = [[row[i] for row in matrix] for i in range(len(matrix[0]))]
print(transposed)
Exercise #42
Context: Understand how to pair elements from two lists using zip and list comprehension.
list1 = [1, 2, 3]
Context: Prac ce crea ng a new list with cumula ve sums using list comprehension.
lst = [1, 2, 3, 4, 5]
Exercise #44
Exercise #45
Problem: Implement a filter to exclude nega ve numbers from a list using list comprehension.
Solving exercises on advanced dic onary opera ons in Python is useful because it makes your
code work be er, improves your problem-solving skills, and helps you handle real-world data
situa ons more effec vely. It’s like adding powerful tools to your coding skills.
Exercise #46
Problem: Merge two dic onaries and sum values for common keys.
Context: Prac ce merging dic onaries and performing opera ons on common keys.
Exercise #47
Problem: Group a list of tuples by the first element using dic onary comprehension.
Context: Understand how to group elements in a list of tuples using dic onary comprehension.
Exercise #48
Problem: Extract unique elements and their counts from a list using dic onary comprehension.
Context: Prac ce crea ng a dic onary with unique elements and their counts using
comprehension.
Problem: Find common keys in two dic onaries using dic onary comprehension.
Context: Learn how to find common keys in two dic onaries using comprehension.
Exercise #50
Problem: Create a dic onary from two lists using dic onary comprehension.
Context: Prac ce crea ng a dic onary from two parallel lists using comprehension.