0% found this document useful (0 votes)
20 views13 pages

Exercise One v2.0

Python exercises

Uploaded by

Ashwin Hariharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

Exercise One v2.0

Python exercises

Uploaded by

Ashwin Hariharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Topics

 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

1) Broadly, classify and sub-classify Python’s built-in data types?

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

4) What are the rules to declare a variable in Python?

5) what is the value of a, b, c, d?

1. a = complex(1, -1)
2. b = a.conjugate()
3. c = b.real
4. d = a.imag

6) what is the output of the following?

1. print(False and 1.0)


2. print(0.0 or True)
3. print(50.0 or False)
4. print(not 50)
5. print(not 0.0)
6. print(not 50 and False or not 0.0)
7. print(4.0 < 1)
8. print(4 <= 4.0)
9. print(5 > 2)
10. print(5 >= 5.1)
11. print(1 + 1j < 1 + 2j)
12. print(not 0.0 and 1.0 == not True or 1.0)
13. print(1 == 2 < 3)

7) Given that a = 5.0 and b = 5, what is the output of the following?

1. print(a == b)
2. print(a is b)
3. print(a is not b)
4. print(a != b)

8) What are the key features of a string object?

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!

12) What is the output of the following print function?

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?

14) Can a string slice expression be used on 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))

16) How might you go about changing a string in Python?

17) Given a string S with the value "s,pa,m", name two ways to extract the two characters in the middle?

18) How many characters are there in the string "a\nb\x1f\000d"?

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

21) What are the key feature of a list type?

22) What is shared referencing & In-place change for a list type? Give examples of each! What will be the output
of the following?

1. L1 = [1,2,3] ; L2 = L1 ; L1 = [1] ; print(L1, L2)


2. L1 = [1,2,3] ; L2 = L1 ; L1[1] = 1 ; print(L1, L2)

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)

23.1) Generate ‘a’ with range

23.2) what is the output of the following?

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)

23.3) what is the value of a and b after the following operations?

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 a list L = [0,1,2,3,4,5,6,7,8,9]


1. Use indexing to replace an item
2. Use slicing to replace an item
3. Use slicing to delete an item
4. Use slicing to insert an item
5. Use slicing to insert an item at the beginning and end of the list

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

Please explain the cause of any issue if any?

28) What will be the output of the following codes?


1. L1 = [1]
L2 = L1 + [2]
L3 = L1.append(2)
print(L1, L2, L3)
print(L2 == L3)
2. L1 = [1]
L2 = L1 + [2]
L1.append(2)
L3 = L 1
print(L1, L2, L3)
print(L2 == L3)

29) what is the output of the following print functions?


1. print(range(0, 2, 0.1))
2. print(range(-1, -5, 2))
3. print(range(-1, -5, -2))
4. print(range(0, 5, 1) == [0, 1, 2, 3, 4])
5. print(range(0, 5, 1) is [0, 1, 2, 3, 4])

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

32) Given the following variables declarations:

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

Find the output of the following print functions

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

6) What will be the output after the following statement?


x = [‘Sunday’, ‘ Monday’, ‘Tuesday’]
y = x[0] + x[1]
print(y)
a. Sunday Monday
b. MondayTuesday
c. SundayTuesday
d. SundayMonday
7) What will be the output after the following statement?

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']

7.8) x = [25, 'Today', 53, 'Sunday', 15]


x.reverse()
print(x)
a. ['Today', 'Sunday', 15, 25, 53]
b. [15, 'Sunday', 53, 'Today', 25]
c. [15, 25, 53, 'Sunday', 'Today']
d. [15, 25, 53, 'Today', 'Sunday']

7.9) x = [25, 35, 53, 25, 52, 35, 25]


print(x.count(25))
a. 25
b. 3
c. 53
d. 35

8) What does the following statement do?

8.1) x = open('python.csv', 'r')


a. Opens an existing text file named python.cvs to write
b. Opens an existing text file named python.csv to append
c. Opens an existing text file named python.csv to read
d. Opens a new file named python.csv to read

8.2) x = open('python.csv', 'w')


a. Opens or creates a text file named python.csv to write
b. Opens or creates a text file named python.csv to append
c. Opens or creates a text file named python.csv to read
d. Opens a new file named python.csv to write

8.3) x = open('python.csv', 'a')


a. Opens or creates a text file named python.csv to write
b. Opens or creates a text file named python.csv to append
c. Opens or creates a text file named python.csv to read
d. Opens a new file named python.csv to append

8.4) x = open('python.txt', 'r+')


a. Opens a text file named python.txt to read from or write to
b. Opens a text file named python.txt to read
c. Opens a text file named python.txt to write
d. Opens a new file named python.txt to append
8.5) x = open('python.txt', 'w+')
a. Opens a text file named python.txt to read
b. Opens a text file named python.txt to write to or read from
c. Opens a text file named python.txt to write
d. Opens a new file named python.txt to append

8.6) x = open('python.txt', 'a+')


a. Opens a text file named python.txt to read
b. Opens a text file named python.txt to read and write
c. Opens a text file named python.txt to write to
d. Opens or creates a text file named python.txt to read from or write to at the end of the file

8.7) x = open('python.bat', 'rb')


a. Opens an existing text file named python.bat to write
b. Opens an existing binary file named python.bat to write
c. Opens an existing binary file named python.bat to append
d. Opens an existing binary file named python.bat to read

8.8) x = open('python.bat', 'wb')


a. Opens or creates a binary file named python.bat to write
b. Opens or creates a binary file named python.bat to append
c. Opens or creates a binary file named python.bat to read
d. Opens a new file named python.bat to write

8.9) x = open('python.bat', 'ab')


a. Opens or creates a binary file named python.bat to write
b. Opens or creates a binary file named python.bat to append
c. Opens or creates a binary file named python.bat to read
d. Opens a new file named python.bat to append

8.10) x = open('python.txt', 'r')


print(x.name)
a. python
b. python.txt opened
c. python.txt or FileNotFoundError
d. python r

8.11) x = open('python.csv', 'w')


print(x.mode)
a. python write
b. python.txt
c. r
d. w
8.12) x = open('python.csv', 'w')
print(x.closed)
a. open
b. closed
c. True
d. False

8.13) x = open('python.csv', 'w')


x.close()
print(x.closed)
a. open
b. closed
c. True
d. False

8.14) x = open('python.csv', 'w')


print(x.readable())
a. readable
b. writable
c. True
d. False

8.15) x = open('python.csv', 'w')


print(x.writable())
a. readable
b. writable
c. True
d. False

8.16) x = open('python.csv', 'a')


print(x.writable())
a. readable
b. writable
c. True
d. False

You might also like