Python Basics Recap
Overview
Teaching: 0 min
Exercises: 0 minQuestions
Python Refresher
Objectives
Understanding key concepts in Python
This course follows on from the python introduction course. To ensure that we are starting from similar positions, there follows a short multiple choice quiz on key python concepts that we will be building on through this course.
Variables and Lists
1. Assigning a value to a variable
We wish to store the string Cat
as a value in the variable animal
, which of these lines of code will do this for us?
animal = 'Cat'
animal = Cat
Cat = animal
animal(Cat)
Solution
Answer 1 is correct
2. Assigning values to a list
We wish to create a list of values, which of these lines of code is valid to do this?
varlist = [34, 57, '2d']
varlist = (12, 'vr', 95)
varlist = 'xcf', 12, 97
Solution
Answer 1 is correct
3a. Indexing characters in a string
Lists and strings both contain multiple indexed values (in the case of strings these are specifically individual characters rather than other values). If we have a variable animal
which contains the string penguin
, which of these options will print the first character (p
) for us?
print(animal[0])
print(animal[1])
print(animal['p'])
Solution
Answer 1 is correct
3b. Indexing characters in a string (slicing)
We can also select whole sections of lists and strings, not just single elements. Using the same variable animal
, containing the string penguin
, as above, which of these options will print the last 2 characters for us? (Note that there is more an one correct answer)
print(animal[5:])
print(animal[6:])
print(animal[6:7])
print(animal[5:7])
print(animal[-2:])
print(animal[:-2])
Solution
Answers 1, 4, and 5 are correct
Loops
4. Constructing a for
loop
Please write a simple for
loop which will print out each of the characters in the animal
variable one at a time.
Solution
for char in animal: print(char)
Software Modules
5. Loading new functions
We want to use the functions in the numpy
library in our code. How do we open this library in our code?
import numpy
load numpy
open numpy
Solution
Answer 1 is correct
If statements and conditionals
6. Conditionals
Which of these conditional tests returns a True
result?
4 > 3
'a' != 'b'
6 >= 6.0
'3' == 3
3 > 'c'
Solution
Answers 1, 2, and 3 return
True
results. 4 returnsFalse
. 5 raises an Error.
7. If statements
What is printed when this if
statement is used?
if 4 > 5:
print('A')
elif 4 <= 5:
print('B')
elif 4 < 5:
print('C')
else:
print('D')
A
B
C
B
andC
D
A
andD
Solution
B
. Both4 <= 5
and4 < 5
would return aTrue
result, but theif
statement is exited as soon as theTrue
result is returned.
Key Points
variables
lists
indexing
loops
conditionals