02 - Variables and Operators

 1. A variable is something that holds a value that may change. In simplest terms, a variable is a little like a storage box that you can put a value in. In the example below, what is the variable?

  x=2 is the variable, because a variable is always a whole statement

  = is the variable, and it doesn't store anything

  x is the variable and 2 is the value stored inside it

  2 is the variable and x is the value stored inside it

 2. The output of the following code that contains two variables is 5:



 3. In the code below, what is the output?
red = 5
blue = 10
print (red, blue)





 4. It is possible to assign the value of one variable to be the value of another. Analyse the code below and predict the output. Note that the values of the variables are changing.
red = 5
blue = 10
yellow = red
red = blue
print (yellow, red, blue)





 5. Python math works as you would expect it to. If x=2 and y=3, then x+y would give you 5. In the code below, what operators have been used?
def main():

  () : =

  main def print

  / + *

  No operators have been used in the code shown

 6. Python adheres to the PEDMAS or BODMAS order of operations. BODMAS meaning Brackets, Of or Order, Division/Multiplication, Addition/Subtraction. If: x=2 y=3 z=5 What will the following statement output?
(x + y) * z

  You cannot have a statement in Python with the x and y in brackets.




 7. Python also has comparison operators which allow you to carry out comparisons. Complete the table to provide a definition for != (What does it mean?) Operation Means < Less than > Greater than <= Less than or equal to >

  Equal to

  Not equal to

  Less than and greater than


 8. Name the variables that have been declared below and on what line can you spot an error.
def main():
	name="Joe Bloggs"

  Joe Bloggs, 32 and Male are the variables. Line 6 has the error as it should not have the ()

  name, age and gender are the variables. Line 4 has the error as Male should be in speech marks

  There are no variables in the code. Line 2 has the error as Joe Bloggs should NOT be in speech marks

  def, main and = are the variables. Line 4 has the error as Male should be just M

 9. Why will the code below fail to run correctly?
def main():

  Because finalscore has not been declared and given a value

  Because score1 and score2 have not been declared and given values

  Because the main subroutine/function has not been declared as a variable

  Because finalscore should be equal to 10

 10. What is the output of the following code?
def main():

  Error - variable3 has not been declared or given a value, so it doesn't exist


  Error - variable3 cannot be printed before variable1 or variable2