Python Basics Recap
Last updated on 2025-04-15 | Edit this page
Estimated time: 0 minutes
Overview
Questions
- 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)
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
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'])
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])
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.
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
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'
Answers 1, 2, and 3 return True
results. 4 returns
False
. 5 raises an Error.
B
. Both 4 <= 5
and 4 < 5
would return a True
result, but the if
statement is exited as soon as the True
result is
returned.
Key Points
- variables
- lists
- indexing
- loops
- conditionals