Exercise One v2.0
Exercise One v2.0
Datatypes:
o Numbers, Strings, Lists, Files, Boolean
Logical Operations:
o Or, And, Is, Is not
Control statements:
o For loops
The type() function
Mathematical functions:
o Abs, Divmod, Pow, Round, Fraction
List functions:
o Len, Max, Min, Sorted(), Sort()
The range() function
Mathematical operators:
o +, -, *, **, /, //, %
Exercises
2) what is the value of ‘a’ and the output of print(type(a))? Import math module when needed
1. a = 10 + 1
2. a = 3 + 1.0
3. 4.0 = a
4. a = 4 + 3.0j
5. a = -3 + 10
6. a = -2.0 * 11
7. a = abs(- 3 * 11.0)
8. False = a
9. a = True
10. a = 9.0 / 3
11. a = 8 // 3
12. a=8%3
13. a = divmod(8, 3)
14. a=9*2
15. a = pow(8, 2)
16. a = 3 * 4 + 1.0 - 10 / 2 - 4**2
17. a = 10 / 0
18. a=0/0
19. a = pow(0, 0)
20. a = int(-5.333)
21. a = math.trunc(-5.333)
22. a = round(-5.333, 0)
23. a = math.floor(-5.333)
24. a = math.ceil(-5.333)
25. a = Fraction(10, -6)
3) What is multiple operator precedence in a Python line of code?
Evaluate ‘a’ in the following:
1. a = (2**1 / 2) * 1 + 1 – 2
2. a = 2**1 / 2 * 1 + 1 – 2
3. a = 2**1 / 2 * 1 - 2 + 1
4. a = 2**1 * 2 * 1 / 2 + 1
1. a = complex(1, -1)
2. b = a.conjugate()
3. c = b.real
4. d = a.imag
1. print(a == b)
2. print(a is b)
3. print(a is not b)
4. print(a != b)
9) Why are single(‘) and double(“) quotes present in python exclusively? Can you print the word (It’s) using single
quotes?
10) What will be the output after the following print statements?
1. print('str' 'is' 'string' 'type')
2. print('str', 'is', 'string', 'type')
3. S = 'str', 'is', 'string', 'type'; print(S, type(S))
11.1) Please change the word ‘string’ to ‘spring’ only using concatenation and slicing!
11.2) Please change the word ‘spamSPAM!’ to ‘spamRocks!’ using concatenation and slicing!
print('%(language)s has %(number)d distinct numeric types.' %{'language': "Python", "number": 3})
13) Can the string find method be used to search a list?
15) Given that n_list = [1, 2, 3, 4] and s_list = str(n_list), what is the output of the following print functions?
1. print(s_list, type(s_list))
2. m_list = list(s_list)
print(m_list, type(m_list))
17) Given a string S with the value "s,pa,m", name two ways to extract the two characters in the middle?
19) For S = ‘0123456789’, please write the output of the following operations!
1. print(S[0])
2. print(S[1])
3. print(S[3])
4. print(S[10])
5. print(S[-1])
6. print(S[len(S)-1])
7. print(S[len(S)-4])
8. print(S[-7])
9. print(S[len(S)-11])
10. print(S[:])
11. print(S[0:])
12. print(S[:len(S)])
13. print(S[0:len(S)])
14. print(S[0:4])
15. print(S[0:9])
16. print(S[0:10])
17. print(S[0:13])
18. print(S[0:-2])
19. print(S[:(len(S)-5)])
20. print(S[0:-9])
21. print(S[:(len(S)-11)])
22. print(S[:(len(S)-12)])
23. print(S[:(len(S)-14)])
24. print(S[0:-12])
25. print(S[0:len(S):1])
26. print(S[::1])
27. print(S[0:len(S):6])
28. print(S[::6])
29. print(S[0:len(S):8])
20) Given that s = ['I will love python', 'so much'], what is the output of the following print functions?
1. print(s[1][2])
2. s.append(‘, when I understand it’)
print(s)
3. s[0].append('!')
print(s)
4. sep = " "
a. s = sep.join(s)
b. print(s)
c. print((s + "{0}").format(", I was joking"))
d. print(s + "{0}".format(", I was joking"))
e. print(s + "{truth}".format(truth=", I was joking"))
f. print(s.find('v'))
g. print(s.index('v'))
h. print(s.find("love"))
i. print(s.index("python"))
j. print(s.partition("will"))
k. print(s.replace("will love", "will try to love"))
l. print(s.split(" "))
22) What is shared referencing & In-place change for a list type? Give examples of each! What will be the output
of the following?
23) Given that a = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] and b = (0, 2, 4, 6, 8, 10, 12, 14, 16, 18)
1. print(1 in a)
2. print(1 not in b)
3. print(a + [1])
4. print(a + 1)
5. print(2 * a)
6. print(a * -1)
7. print(a * [3])
8. print(a[5])
9. print(b[-1])
10. print(a[0:5])
11. print(b[:])
12. print(a[:-2])
13. print(a[4:])
14. print(b[-2:])
15. print(b[:2])
16. print(b[0:20:2])
17. print(b[0:-8:2])
18. print(len(b))
19. print(min(a))
20. print(max(b))
21. print(b.index(12))
22. print(a.count(18))
23. print(a = [[]] * 2)
24. print(a[0].append(4))
25. a = [1]
b=a
b.append(2)
print(a == b)
print(a is b)
26. a = [1, 2, 3]
b = a[:]
print(a is b)
print(a == b)
1. a[0] = 10
2. b[1] = -7
3. a[0: 4] = [10, 20, 30, 40]
4. a[0:10: 2] = [10, 20, 30, 40, 50]
5. a.append(21)
6. b.append(20)
7. a.clear()
8. a.copy()
9. a.extend([21, 23, 25])
10. a + [21, 23, 25]
11. a.insert(5, 19)
12. a.pop(1)
13. a.pop()
14. a.remove(3)
15. a.reverse()
16. del a[0: 5]
24) Is the following assignment possible: >> L = []; print(l[0] = 1? Please explain your stance? How would one
resolve any issue if any?
25) Given that c = [0, 3, 12, 27, 48, 75, 108, 147, 192, 243], write a list comprehension to compute c !
26) Given that c = [0, 2, 4, 5 , 3 , 9, 7 , 8, 6], what is the output of c.sort() and sorted(c)?
27) Given l = [0,1], what will be the value of the list l after the following statement has been executed?
1. l.extend(2)
2. l.extend(3.0)
3. l.extend([4])
4. l.extend(‘five’)
5. l.append(6)
6. l.append(7.0)
7. l.append([8])
8. l.append(‘nine’)
30) Given that a = [1, 2, 3] and b = a[:], what is the output of the following print functions?
1. print(b is a)
2. print(b == a)
31) Given l = list(range(0,10,1)), please write the output of the following operations!
1. print(l[-1])
2. print(l[len(L)-1])
3. print(l[len(L)-4])
4. print(l[-7])
5. print(l[len(L)-11])
6. print(l[:])
7. print(l[0:])
8. print(l[:len(L)])
9. print(l[0:len(L)])
10. print(l[0:9])
11. print(l[0:10])
12. print(l[0:13])
13. print(l[0:-2])
14. print(l[:(len(L)-5)])
15. print(l[0:-9])
16. print(l[:(len(L)-11)])
17. print(l[:(len(L)-12)])
18. print(l[:(len(L)-14)])
19. print(l[0:-12])
20. print(l[0:len(L):1])
21. print(l[::1])
22. print(l[0:len(L):6])
23. print(l[::6])
24. print(l[0:len(L):8])
25. print(l[0:len(L):10])
26. print(l[::11])
27. print(l[len(L):0:-1])
28. print(l[::-1])
29. print(l[len(L):0:-3])
30. print(l[::-5])
31. print(l[len(L):0:-10])
32. print(l[::-12])
file_name_1 = "bucket_list.txt"
file_name_2 = "bucket_list_p.txt"
goals = ["Finishing my python course", "Live to see a sequel to The Matrix", "traveling to the moon"]
32.1)
with open(file_name_1, "w") as file:
file.write(goals)
with open(file_name_1, "r") as file:
print(file.tell())
input_data = file.read()
print(input_data)
32.2)
with open(file_name_1, "w") as file:
file.writelines(goals)
with open(file_name_1, "r") as file:
input_data = file.read()
print(input_data)
32.3)
with open(file_name_1, "w") as file:
file.writelines("\n".join(goals))
with open(file_name_1, "r") as file:
input_data = file.read()
print(input_data)
32.4)
with open(file_name_1, "w") as file:
for i in range(3):
file.write(goals[i] + "\n")
with open(file_name_1, "r") as file:
input_data = file.read()
print(input_data)
32.5)
with open(file_name_1, "w") as file:
file.writelines("\n".join(goals))
with open(file_name_1, "r") as file:
input_data = file.readline()
print(input_data, file.tell())
input_data = file.read(2)
print(input_data, type(input_data))
input_data = file.read()
print(input_data)
32.6)
with open(file_name_1, "w") as file:
file.writelines("\n".join(goals))
with open(file_name_1, "r") as file:
file.seek(28, 0)
input_data = file.readline()
print(input_data, file.tell())
32.7)
with open(file_name_1, "w") as file:
file.writelines("\n".join(goals))
with open(file_name_1, "r") as file:
for line in file:
print(line)
32.8)
with open(file_name_1, "w") as file:
file.writelines("\n".join(goals))
with open(file_name_1, "r") as file:
for line in file:
print(line, end='')
32.9)
with open(file_name_2, "wb") as file:
for i in range(3):
pickle.dump(goals[i], file)
pickle.dump(goals, file)
print(file.closed)
with open(file_name_2, "rb") as file:
for i in range(5):
input_data = pickle.load(file)
print(input_data, type(input_data))
if i == 5:
print(type(input_data))
Multiple Choice Questions
1) Which of the following is an assignment operator in Python?
a. ==
b. ===
c. >>>
d. =
2) Which of the following is used to initialize multiple variable with a common value?
a. x = y : y = 33
b. x = y = z = 33
c. x = z ; y = z ; x = 33
d. x & y & z = 33
3) The output displayed by the print function will add this invisible character at the end of the line by default?
a. \t
b. \n
c. \s
d. \r
4) What will be the data type of x after the following statement, if we enter an input?
x = input('Enter a x = input('Enter a x = 48; y = str(x)
number: ') number: ')
given x = 18 y = int(x), given x =
50 a. Float
a. Float b. String
b. String a. Float c. List
c. List b. String d. Integer
d. Integer c. List
d. Integer
5) What will the value of the variables x, y, z after the following statement?
x, y, z = 3, 4, 5
a. All three will have the value of 3
b. All three will have the value of 345
c. x will have the value of 3, y will have the value 4 and z will have the value of 5
d. x and y will have arbitrary values, while z will have the value of 345
7.1) x = [5,4,3,2]
x.insert(1,0)
print(x)
a. [5, 1, 3, 2, 0]
b. [5, 0, 4, 3, 2]
c. [0, 5, 4, 3, 2]
d. [1, 5, 4, 3, 2]
7.2) x = [5,4,3,2]
x.remove (2)
print(x)
a. [5, 3, 2]
b. [5, 4, 3]
c. [5, 4, 2]
d. [3, 2]
7.3) x = [5, 4, 3, 2, 1]
print(x.pop(3))
a. 4
b. 3
c. 2
d. 1
7.4) x = [5, 4, 3, 2, 1]
print(x.index(1))
a. 4
b. 3
c. 2
d. 1
7.5) x = [5,4,3,2,1]
x.extend(x)
print(x)
a. [5, 4, 3, 2, 1]
b. []
c. [1, 2, 3, 4, 5]
d. [5, 4, 3, 2, 1, 5, 4, 3, 2, 1]
7.6) x = [5,4,3,2,1]
x.reverse()
print(x)
a. [0, 1, 2, 3, 4, 5]
b. [0, 5, 4, 3, 2, 1]
c. [5, 4, 3, 2, 1, 0]
d. [1, 2, 3, 4, 5]
7.7) x = ['25', 'Today', '53', 'Sunday', '15']
x.sort()
print(x)
a. ['Today', 'Sunday', '15', '25', '53']
b. ['Sunday', 'Today', '15', '25', '53']
c. ['15', '25', '53', 'Sunday', 'Today']
d. ['15', '25', '53', 'Today', 'Sunday']