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

Hewighihv

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

Hewighihv

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 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)

# a) a < b and b > 15


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

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”)

a) l[1:4] [ 20 530 40 50] 60] , .


, ,

b) l[-3:] i[30 to 10) 70] ,


.
,
00
,

c) l[::2] = [30 40 50] 10] 10 ,


.
.
,

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)

# Case 2 F-179 list fElid


* -
shallow
my_list = [1, [2, 3, 4], 5, 6]
[1 [rit] 6)
copy copied_list = my_list.copy()
5
my-list
.
,

↑)
,

[1 [2 ,.
3 . + 2) 5 , 6] id [ 4)
,

[1 [V3 6)
.

opy-list
. 5

copied_list[1].append(10)
.
, ,

print(my_list) Fl@XnFiXAA-Rd list ,

# 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)

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. [ 10 ,
0 ,60]30 ,
40 , 50 .

b) After inserting 15 at the second position. [10 .


15
, 2 ,
30 .
40 50 . ,
00]
c) After removing the first occurrence of 30. [10 40 .
15 , 0
, ,
90
.
60]
d) After popping the last element. [10 15 40 5) .
,
2 . ,

e) After extending with [70, 80]. [ 15 40 50 10 8) 10 .


,
20 , ,
,
.

f) After sorting the list. [10 40 58 10 0]


.
15
, 20 .
, . .

g) After reversing the list. 18 70 40 , .


50 . ,
0
.
15
,
10]
3

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 ( ,

d) " ".join(s.split(" ")) Python Programming

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


-

a) What will be the output of s[5:12] and s[-6:-1]? Science ,


cienc

b) How can we reverse the string using slicing? S[ 1] ::

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" :

b) Update the value of "age" to 26? d[ "age") 2 =

c) Remove the key "city"? del di "city" d) d keys1) I age city .


=
name , ,

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,

Immutable 2. Tuple > (1 3 4)


-

,
2, ,

Mutable 3. Dictionary 9'a' 1 'b' 23 >


-

:
,
=

Immutable 4. String "Hello">


-

Mutable 5. Set 91 33
>
-
.
2,

Immutable 6. Integer 5 >


-

b) Consider the following Python codes:

my_str = "Hello "


string is
my_str[-1] = '!' # Case 1
"immutable" What happens when you try to execute my_str[-1] = '!'? Explain why this operation
Str
does not succeed.
my -

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
=

You might also like