Exam Qs Flashcards
Exercise 1
Write a single script which prints out the results of the following sums. Place the values into variables before printing them rather than just passing the results to the print function (you will need this for questions g and h).
Exercise 2
Write some code which calculates the area of a rectangle using the formula area = width * height
Do this by setting up a variable for width and height, then calculate the area and store it in the area variable. Then print out “Area: x, Height: y, Width: z”.
Exercise 3
Set up a list called my_list containing the numbers 1 to 4 inclusive. Print the first and last entries in the list. Try and find two different ways to print the last entry in the list.
Set up an empty list. Loop around the numbers between 0 and 10 and, on each loop iteration, add the number squared to the end of the list. Print the final list.
Exercise 5
Set up a tuple containing three floating point numbers: 2.0, 4.0, 6.0. Print out the tuple.
Exercise 6
Print every number from 0 to 30 inclusive.
Exercise 7
Use a loop to print every odd number between 0 to 30 inclusive
Exercise 8
Write python code to create and output a times table (from 1 to 12) of a number which you define as a variable. Your code should start like this
An example output might start:
Exercise 9
Take your previous example and use another loop to make it produce multiplication tables for numbers from 1 to 12. Put a title before each table.
Your output should start something like:
Exercise 10
Write a program to output a list of temperatures in Celsius and Fahrenheit running from -10C to 50C.
The equation for calculating Fahrenheit from Celsius is: f=C∗95+32 . italicised text Print the table neatly.
Double-click (or enter) to edit
What is wrong with this line of code
The quotes do not match. Either use all single quotes or all double quotes. For example, name=’Alice’
Why does this code raise an error? - (2)
age is a string variable but you are adding an integer to it.
Variable types have to match when doing arithmetic.
What is incorrect with this code
The list only has 3 entries but you are trying to access the fourth one.
Python indexing starts at zero.
What will this print, and why might it confuse a beginner?
This will concatenate the lists: [1,2,3,4,5,6]
You might be expecting it to add them element by element [5,7,9], or even include list2 as a member of list 1 [1,2,3,[4,5,6]]
What concept does this example illustrate, and what is the mistake
You cannot change tuples once you make them. Here you are trying to alter the second element in the tupe. You can’t.
Identify and correct the mistake in this loop
The variable ‘num’ is not defined. That line should read
print(number
What will be the output of this loop, and what common misunderstanding might this clarify
0,1,2,3,4
It will demonstrate that python ranges are zero indexed and exclusive (they start at zero and do not include the last number)
What will be the output of this nested loop, and what concept does it illustrate
0 0
0 1
1 0
1 1
2 0
2 1
Ranges start at zero and exclude the highest element. Also loop blocks are defined by indentation. And nested loops are evaluated from the deepest loop out.
Why doesn’t this code snippet print “Hello, John!”?
The variable ‘name’ and the word ‘name’ inside the string are different things. You could write
print(“Hello”,name)
To get the right answer.
What error will this code produce and why?
hat the function looks like it takes two arguments (x and y) but you are only providing one argument when you call it (2)
Explain what each of these ‘magic’ / or ‘shell’ commands does.
pwd = print the working directory
cd = change directory (in this case to the one above)
ls = list the contents of the current directory