Access Python Quiz「bristol.ac.uk」
Check your learning: topics so far (quiz)
- In Python, what is a string and how is it defined? Choose from the options below.
A. A collection of characters within quotation marks
B. A decimal number
C. An ordered collection of other objects between square brackets
D. A whole number
E. A collection of key:value pairs between curly brackets
- Look at these lines of code:
b = 3
a = b*2
b = 4
What would "a" be evaluate to?
- Look at this expression:
list2 = [3, 10]
list2.append(23)
What will "list2" look like after these lines have been executed?
[23, 3, 10]
[3, 10]
[3, 10, [23]]
[3, 10, 23]
- Look at the different statements below and determine which are correct (Answer: True or False).
A.False
B. True (a list can be updated)
C. True
- Look at these lines of code:
a = 2.0 + 2.0*5.0 - 6.0
b = a/2.0
What would the variable "b" evaluate to?
Check your learning: While loops (quiz)
- What is a while loop? Choose from the options below.
A. A whole number
B. A collection of key:value pairs between curly brackets
C. A loop which continues to run as long as a condition is met.
D. A loop which iterates over a sequence (e.g. a list or a range of numbers)
E. A statement which tests a condition (or a set of conditions) to decide which lines of code to execute
- Look at these lines of code. What is wrong with this loop?
number = 10.0
while number/5.0 <= 3
print(number)
number += 1
Choose all applicable options below.
A. Variable "number" is not defined
B. No colon (after the conditional statement), :
C. Conditional statement is not valid
D. No indentation (body of the loop)
- Look at the lines of code below.
number = 30
while number >= 30:
print(number)
number += 1
Will this result in an infinite loop?
A. True
B. False
- Look at these lines of code below.
b = 5
while b < 11:
print(b)
b += 2
How many times would this loop run?
- More challenging question
More challenging question
list1 = ["a", "b", "c"]
# Testing the length of the list
while len(list1) > 0:
print(list1)
# Cutting off the last element in each loop
list1 = list1[0:-1]
Tips:
- The len() counts how many entries there are within the list. len(['a','b','c']) would be 3
- Using the slice list[0:-1] selects all but the last element of a list
- An empty list [ ] has a length of 0
A. Loops 3 times and prints out:
['a', 'b', 'c']
['a', 'b']
['a']
B. Loops 3 times and prints out:
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
C. Loops 4 times and prints out:
['a', 'b', 'c']
['a', 'b']
['a']
[]
D. Loops infinite times and prints out:
['a', 'b', 'c']
['a', 'b', 'c']
['a', 'b', 'c']
[...]
Asynchronous Activity 2: For versus While
Asynchronous Activity 2: For versus While
Check your learning: loops and branches (quiz)
- What is an if statement? Choose from the options below.
A. A statement which tests a condition (or a set of conditions) to decide which lines of code to execute
B. A decimal number
C. A loop which iterates over a sequence (e.g. a list or a range of numbers)
D. A collection of characters within quotation marks
E. A loop which continues to run as long as a condition is met.
- What is a for loop? Choose from the options below.
A. A whole number
B. A loop which continues to run as long as a condition is met.
C. A statement which tests a condition (or a set of conditions) to decide which lines of code to execute
D. A collection of key:value pairs between curly brackets
E. A loop which iterates over a sequence (e.g. a list or a range of numbers)
- Look at the lines of code below.
for number in range(2, 6):
print("Executing loop")
How many times will this for loop run?
- Look at these lines of code below.
for range(1,5):
print(item)
What is wrong with this for loop?
A. Scaffolding for a for
loop requires the word in
as well
B. item
is not defined
C. For loops should include a conditional statement
D. Too much indentation
E. The colon symbol : is not needed
- Look at these lines of code below.
count = 100
if count/10 = 10
print(count)
What's wrong with this if statement?
A. count
variable should be a float rather than an integer
B. Too much indentation
C. The conditional statement is incorrect
D. This will result in an infinite loop
E. Missing a colon, :
- Look at these lines of code .
number = 12
if number/2 == 4:
print("if condition is True")
elif number/3 == 4:
print("elif condition (1) is True")
else:
print("else block executed")
What will be printed (if anything)?
- More challenging
Look at these lines of code below.
nested_list = [[1],[2,2],[3,3,3]]
for sublist in nested_list:
while len(sublist) < 4:
copy_first_entry = sublist[0]
sublist.append(copy_first_entry)
print(nested_list)
What will nested_list
look like after this code is completed?
Hints:
- Consider what is being passed to the while loop and what condition the while loop is checking
- What is the while loop doing?
A. [[1, 1], [2, 2, 2], [3, 3, 3, 3]]
B. []
C. [[1], [2, 2], [3, 3, 3]]
D. [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]]
Check your learning - the numpy module
- In Python, what is a numpy array and how is it defined? Choose from the options below.
A. An ordered collection of other objects, all of the same type. Provided by the numpy external library.
B. An ordered collection of other objects between square brackets. A built-in Python object.
C. A whole number
D. A collection of key:value pairs between curly brackets
- Look at the different statements below and determine which are correct (Answer: True/False).
A. All values within a given numpy array must be the same type
B. Numpy arrays have shape and dimensionality
C. Elements of a numpy array cannot be updated once they have been created
True
False
- How would you access the last element of an array e.g.
array1
defined below:
import numpy as np
array1 = np.array([103, 54, 91, 23])
array1[-1]
np.array1[-1]
array1[4]
array1[3]
- Which of these functions can be used to calculate the average (mean) for a numpy array (for example
array1
from Question 3)? You can assume numpy has been imported using the import statement below.
import numpy as np
array1 = np.array([103, 54, 91, 23])
np.mean(array1)
mean(array1)
average(array1)
np.array(array1)
- Will the statement below create a valid numpy array (array2)?
import numpy as np
array2 = np.array(["dog", 5, "cat", 9], dtype=int)
Answer True/False for this.
True
False
- Look at these arrays created below (array3, array4 and array5)
Note: when printed, numpy arrays often displayed without the commas - this is a stylistic choice and does not change how numpy arrays are defined.
import numpy as np
array3 = np.array([1, 2, 3, 4])
array4 = np.ones(4)
array5 = array3 + array4
print(array5)
What will the output (array5) look like when printed?
[2 3 4 5]
[5 6 7 8]
[1 2 3 4]
[1 1 1 1]
Check your learning - multi-dimensional arrays
- A numpy ndarray is an immutable object which cannot be updated (in-place) once it has been created.
A. True
B. False
- How would you select a range from the second element to the end of the array, for array1?
array1 = np.array([1., 2., -1., 1., 0., 1.])
A. array1[0:-1]
B. array1[1:]
C. array1[3:]
D. array1[1:-1]
- For array2 defined below, how many dimensions does this numpy array have?
import numpy as np
shape = (2, 3, 4)
array2 = np.ones(shape)
- For array3 defined below, what would the value of element be?
array3 = np.array([[1, 2, 3, 4],
[5, 6, 7, 8]])
element = array3[0, 0]
A. 5
B. 8
C. [1, 2, 3, 4]
D. 1
- For array4, created below, evaluate the following statements (Answer: True/False)
from numpy import random
rng = random.default_rng()
shape = (3, 3)
array4 = rng.random(shape)
True
False
- Look at array5 created below
array5 = np.array([[10, 8, 6],
[12, 11, 10],
[13, 10, 7]])
If we apply a boolean condition to filter this array to find all elements equal to 10 to create a new array called filtered_array, which of these properties for this array are correct?
filtered_array = array5[array5 == 10]
Shape would be 3 x 2 x 3
This would find 3 elements
This is not a valid statement
公众号:AI悦创【二维码】
AI悦创·编程一对一
AI悦创·推出辅导班啦,包括「Python 语言辅导班、C++ 辅导班、java 辅导班、算法/数据结构辅导班、少儿编程、pygame 游戏开发、Web、Linux」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践等。当然,还有线下线上摄影课程、Photoshop、Premiere 一对一教学、QQ、微信在线,随时响应!微信:Jiabcdefh
C++ 信息奥赛题解,长期更新!长期招收一对一中小学信息奥赛集训,莆田、厦门地区有机会线下上门,其他地区线上。微信:Jiabcdefh
方法一:QQ
方法二:微信:Jiabcdefh
- 0
- 0
- 0
- 0
- 0
- 0