(Based on ENGINEER 1P13 computing material, written by Willie Pai)
For more study material, view all previous Pre-Lab & In-Lab Jupyter Notebooks
- Inputs and Outputs
- Built-in Functions
- Errors
- Defining and calling functions
- Passing Arguments
- Returning Values
- Variable Scope
- Lists
- String Operations
- For Loops
- Boolean Comparison Operators
- Decision Structures
- While Loops
- Opening/closing files
- Reading & looping through files (Methods)
- Writing & looping to files (Methods)
- Boolean Comparison Operators
- Decision Structures
2 Practice Quizzes from PythonSpot.com
Operators and Expressions Quiz
print(25 % 4)
A) 25/4
B) 6.25
C) 1
D) 6
# Your answer:
# TEST YOUR CODE HERE
print(25 / 4)
A) 25/4
B) 6.25
C) 1
D) 6
# Your answer:
# TEST YOUR CODE HERE
print(25 // 4)
A) 25/4
B) 6.25
C) 1
D) 6
# Your answer:
# TEST YOUR CODE HERE
a = 1
b = 2
c = "3"
print(a + b + c)
A) 6
B) TypeError
C) 33
D) 123
# Your answer:
# TEST YOUR CODE HERE
first_value = input()
sum = first_value + str(3)
print(sum)
A) 253
B) 8
C) TypeError
D) 25 3
# Your answer:
# TEST YOUR CODE HERE
print(not(True and False) or not(False and False))
# Your answer:
# TEST YOUR CODE HERE
A) print('def HelloWorld(string):')
B) print(str(input('int("Hello!"):')))
C) print ( "Hello World!" )
D) print(13 + "years old")
# Your answer:
# TEST YOUR CODE HERE
some_list = ["One", "Two", "Three", "Four", "Five"]
for word in some_list:
word.strip('e').split()
print(word)
A)
On
Thr
Fiv
Svn
Nin
B)
On
Thr
Fiv
Seven
Nin
C)
['On']
['Thr']
['Fiv']
['Seven']
['Nin']
D)
One
Three
Five
Seven
Nine
E)
['O', 'n']
['T', 'h', 'r']
['F', 'i', 'v']
['S', 'e', 'v', 'e', n']
['N', 'i', 'n']
# Your answer:
# TEST YOUR CODE HERE
a = [1, 10, 25]
b = ['bed', 'blanket', 'pillow']
c = [2, 'hi', [1, 2]]
A) len(b[1].split()) + len(c) - a[0]
B) len(c[1]) - len(str(a[2])) + round(len(b[0]))
C) sum(c[2]) + b[2].index('l') - (a[2] % 11) + 1
D) c[2].index(2) - (a[1] / 10.0) + len(b[0])
# Your answer:
# TEST YOUR CODE HERE
list_a = [2, 4, 6, 8]
list_b = [1, 3, 5, 9]
result = 0;
for i in list_a:
for j in list_b:
if i < j and i * j > result:
result += 1
print("Proceed")
A) 6 times
B) 7 times
C) 8 times
D) 9 times
# Your answer:
# TEST YOUR CODE HERE
A)
x = 2020
def modify():
x = 2021
global x
return x
print(modify())
B)
x = 2020
def modify(y):
global x
x = 2021
return y
print(modify(x))
C)
x = 2020
def modify(y):
global x
y = x
x = 2021
return(y)
modify(x)
print(x)
D)
x = 2020
def modify(y):
global x
return y
x = 2021
return x
print(modify(x))
# Your answer:
# TEST YOUR CODE HERE
14. Which of the following functions returns a list of even numbers found inside a given list of integers?
A)
def returnEven(list):
even_numbers = []
for i in range(list):
if i % 2 == 0:
even_numbers.append(i)
return even_numbers
B)
def returnEven(list):
even_numbers = []
for number in list:
if i % 2 == 0:
even_numbers += number
return even_numbers
C)
def returnEven(list):
even_numbers = []
for i in range(len(list)):
if list[i] % 2 == 0:
even_numbers.append(list[i])
return even_numbers
D)
def returnEven(list):
even_numbers = []
for number in even_numbers:
if number % 2 == 0:
even_numbers.append(number)
return even_numbers
# Your answer:
# TEST YOUR CODE HERE
for i in range(10):
i += 2
print(i)
A)
x = 10
while x < 30:
print(int(x / 2 - 3))
x += 2
B)
x = 2
while x <= 12:
print(x)
x += 1
C)
x = 2
while x < 12:
x += 1
print(x)
D)
x = 0
while x > 12:
print(x)
x += 1
# Your answer:
# TEST YOUR CODE HERE
# Your answer:
# TEST YOUR CODE HERE
17. True or False: The ‘in’ operator is used to check if a value exists within an iterable object container such as a list. Evaluates to true if it finds a variable in the specified sequence and false otherwise.
# Your answer:
# TEST YOUR CODE HERE
# Your answer:
# TEST YOUR CODE HERE
# Your answer:
# TEST YOUR CODE HERE
['A', 1]
['B', 2]
['C', 3]
['D', 4]
['E', 5]
file = open("document.txt", 'r')
for line in file.readlines():
print(line[1])
file.close()
A)
1
2
3
4
5
B)
"1"
"2"
"3"
"4"
"5"
C)
'
'
'
'
'
D)
A
B
C
D
E
# Your answer:
# TEST YOUR CODE HERE
One A
Two B
Three C
Four D
Five E
One
A
Two
B
Three
C
Four
D
Five
E
A)
file = open("document.txt", 'r')
for line in file.readlines():
print(line)
file.close()
B)
file = open("document.txt", 'r')
for line in file.readlines():
print(line)
line = file.readline()
print(line)
file.close()
C)
file = open("document.txt", 'r')
line = file.readline()
while line != "":
line.split()
print(line[0])
print(line[1])
file.close()
D)
file = open("document.txt", 'r')
line = file.readline()
while line != "":
print(line.split()[0] + "\n" + line.split()[1])
line = file.readline()
file.close()
# Your answer:
# TEST YOUR CODE HERE
22. Given a function definition explaining the purpose of the function, rewrite the function to correct its error.
def divideByTwo(numbers):
'''
Given a list of integers, return a list of floats containing half the value of each integer
'''
half_numbers = []
for num in numbers:
half_num = num / 2
half_numbers.append(num)
return half_numbers
Double click to write your answer here in this cell. Don't delete the ``` marks.