1. Which of the following is correct about Python?
2. Analyse the following code that uses operators. What is the output for lines 4,5,6?
3. In Python ________ are immutable while a list is mutable. It means once you create ________you cannot edit or make changes in the values while in a list you can.
4. What is the output of the following code?
5. What is the output of len([1, 2, 3])?
6. What is the output of the following code? (refer particularly to line 5?what does this code do?)
def mysteryfunction(numbers):
x = 0
for num in numbers:
x+=num
print("x:", x)
mysteryfunction((1,2,3,4))
10 > It simply adds up all the numbers that are passed to the function, in this case: 1,2,3,4
5 > It increments (using +1) the last number passed to the function. E.g. 4+1 = 5
0 > It disregards the variables passed to it, and sets x to 0, returning 0
8 > It takes the last parameter that is passed to the function and doubles it (4 x 2 = 8)
7. What is occuring on line 2 of the following code?
8. The following code should output 0,1,2 but as it stands, it does not produce an output. What needs to go in line 3 for it to output 0,1,2?
9. The output of the following code (that should simulate output for the fibonacci sequence) is 1,1,1,1,1, but we want it to be, 1,1,2,3,5. What one thing in the code needs to change to correct the code?
10. What will the following code output?
11. Fill in the blanks. What needs to be added on line 3 (after the second 'next') in order to get rid of any blank lines?
12. Fill in the blanks with the code that ought to go on line 4 (to complete the Bubble sort).
def bubbleSort(nlist):
for passnum in range(len(nlist)-1,0,-1):
for i in range(passnum):
___________________________
temp = nlist[i]
nlist[i] = nlist[i+1]
nlist[i+1] = temp
nlist = [14,46,43,27,57,41,45,21,70]
bubbleSort(nlist)
print(nlist)
if nlist[0]>nlist[1]:
if nlist[i]=nlist[i+1]:
if nlist[i+1]>nlist[i]:
if nlist[i]>nlist[i+1]:
13. In the following binary search, searching for the string "anton" returns 'false' even though "anton" is indeed in the list. Why?
14. The following code shows the creation of a 2d array in Python. What is the output of the code?
matrix=[[1,2,3],[10,20,30]]
i=0
j=2
print(matrix[i][j])
15. What is the output of the following code?
for x in range(2, 6):
print(x)
2,4,6
2,4,6,8
2,6,16,60
2,3,4,5