Hewighihv
Hewighihv
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 float 9 .5 -
b) a + c Error
c) a + int(c) 8 int -
d) str(a) + c
53-string
e) a / b float 2 .
0 -
f)
# a // b float .
2
-
0 -
g)
# a * c 33333 string -
h) a * int(c) 15-iht
i) a * float(c) float 15 .
0 -
Problem 2. Given a = 5 and b = 10, evaluate the following expressions and state whether they
are True or False: (10 pts)
j
-T Y 3
-
L
Y
- - -
2
O 3
I Y
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”)
d) l[2][1] 40
e) l[2][-2:] [40 57 ,
f) l[1:4][0] z
[10 40 50] 00
1
.
,
20 , ,
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]
LEG copied_list = my_list [1 [2
. 3 + 10] 5 6]
3) E
.
, ,
,
,
copied_list[1].append(10)
print(my_list)
↑)
,
[1 [2 ,.
3 . + 2) 5 , 6] id [ 4)
,
[1 [V3 6)
.
opy-list
. 5
copied_list[1].append(10)
.
, ,
# Case 3
from copy import deepcopy
copy
my_list = [1, [2, 3, 4], 5, 6]
C
copied_list = deepcopy(my_list)
[ 1 ,
[ 2 ,
3 ,
47 ,
5, 6]
7(zz1 & 1zP
copied_list[1].append(10)
SR copy
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)
Problem 6. Given the string s = "Python Programming", what will be the result of the following
operations? (8 pts)
a) s.lower() python programming
b) s.upper() PYTHON PROGRAMMING
c) s.split(" ") [ Python Programming ( ,
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"? dI "Country" 7 "USA" :
it "name" in di
d) Retrieve all keys and values from the dictionary? . values ()
d [Alive York] :
, 30 , New
print(d["name"])
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:
Mutable
1. List [1 >
-
3] ,
2,
,
2, ,
:
,
=
Mutable 5. Set 91 33
>
-
.
2,
create a new
my_str = "Hello "
my_str += "World!" # Case 2
-
that is
string
-
What happens when you execute my_str += "World!"? Explain why this operation seems
of
the connection
to succeed even though strings are immutable.
myser & "World " !
*
Hello World ! Casa ~ doesn't mutate the
existing string
my str
=