Preview

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

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

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

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

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

 2. The output of the following code that contains two variables is 5:
num1=2
num2=3
print(num1+num2)

  True

  False

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

  10,5

  red,blue

  blue,red

  5,10

 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,5,5

  10,5,5

  5,10,10

  5,10,5

 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():
	num1=3
	num2=10
	total=num1/num2+2
	discount=total/100*20
	print(discount)
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

  25

  30

  125

  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

  Surprising

  Less than and greater than

  Not equal to

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

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

  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

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

 9. Why will the code below fail to run correctly?
def main():
	finalscore=score1+score2
	print(finalscore)
main()

  Because finalscore has not been declared and given a value

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

  Because score1 and score2 have not been declared and given values

  Because finalscore should be equal to 10

 10. What is the output of the following code?
def main():
	variable1=2
	variable2=3
	print(variable3)
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

  3

  5