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.