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

Homework #1: June 15, 2024

Uploaded by

meowaowu0428
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 views3 pages

Homework #1: June 15, 2024

Uploaded by

meowaowu0428
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/ 3

Python for Data Analysis Workshop

Instructor: Benson Chiu


June 15, 2024
Homework #1

Problem 1. If a = 5, b = 2.5, and c = "3", what will be the result and the data type of
the result for each of the following expressions? (For invalid expressions, just simply write down
“Error”) (18 pts)

a) a + b
b) a + c
c) a + int(c)
d) str(a) + c
e) a / b
f) a // b
g) a * c
h) a * int(c)
i) a * float(c)

Problem 2. Given a = 5 and b = 10, evaluate the following expressions and state whether they
are True or False: (10 pts)

a) a < b and b > 15


b) a == 5 or b == 5
c) not (a > b)
d) a < 10 and (b == 10 or b > 20)
e) (not (a == 5)) and (b != 10 or a > 3)

Problem 3. (12 pts) Given the list l = [10, 20, [30, 40, 50], 60, 70], what will be the
result of the following operations? (For invalid operations, just simply write down “Error”)

a) l[1:4]
b) l[-3:]
c) l[::2]
d) l[2][1]
e) l[2][-2:]
f) l[1:4][0]

1
2

Problem 4. Consider the following three cases where we have a list containing a nested list. In
each case, a copy of the list is made using different methods, and then an element is appended to
the nested list in the copied list. (10 pts)

# Case 1
my_list = [1, [2, 3, 4], 5, 6]
copied_list = my_list
copied_list[1].append(10)
print(my_list)

# Case 2
my_list = [1, [2, 3, 4], 5, 6]
copied_list = my_list.copy()
copied_list[1].append(10)
print(my_list)

# Case 3
from copy import deepcopy
my_list = [1, [2, 3, 4], 5, 6]
copied_list = deepcopy(my_list)
copied_list[1].append(10)
print(my_list)

a) Compare and contrast the results of my_list after executing the code in each case.
b) Explain why the results differ for each case.

Problem 5. You are given a series of list operations. Your task is to determine the state of the
list after each operation and predict the final output. (14 pts)

my_list = [10, 20, 30, 40, 50]


my_list.append(60)
my_list.insert(1, 15)
my_list.remove(30)
popped_element = my_list.pop()
my_list.extend([70, 80])
my_list.sort()
my_list.reverse()

Write the content of my_list after each of the following operations:


a) After appending 60.
b) After inserting 15 at the second position.
c) After removing the first occurrence of 30.
d) After popping the last element.
e) After extending with [70, 80].
f) After sorting the list.
g) After reversing the list.
3

Problem 6. Given the string s = "Python Programming", what will be the result of the following
operations? (8 pts)
a) s.lower()
b) s.upper()
c) s.split(" ")
d) " ".join(s.split(" "))

Problem 7. For the string s = "Data Science": (6 pts)


a) What will be the output of s[5:12] and s[-6:-1]?
b) How can we reverse the string using slicing?

Problem 8. Given the dictionary d = {"name": "Alice", "age": 25, "city": "New York"},
how would you: (10 pts)
a) Add a new key-value pair "country": "USA"?
b) Update the value of "age" to 26?
c) Remove the key "city"?
d) Retrieve all keys and values from the dictionary?
e) Check if the key "name" exists in the dictionary and print its value?

Problem 9. In Python, different data types have different mutability characteristics. Some data
types are mutable, meaning their contents can be changed after they are created, while others are
immutable, meaning their contents cannot be changed once they are created. (12 pts)
a) For each of the following data types, identify whether they are mutable or immutable:
1. List
2. Tuple
3. Dictionary
4. String
5. Set
6. Integer
b) Consider the following Python codes:

my_str = "Hello "


my_str[-1] = '!' # Case 1

What happens when you try to execute my_str[-1] = '!'? Explain why this operation
does not succeed.

my_str = "Hello "


my_str += "World!" # Case 2

What happens when you execute my_str += "World!"? Explain why this operation seems
to succeed even though strings are immutable.

You might also like