Assessment: Python Conditionals

Oct 2, 2014 • Birgit Bachler

Question 1:
What would this Python code snippet print?

foo=10
bar=10
baz=20
if (foo*2!=baz):
  print "a"
elif (baz or foo<baz):
  print "b"
elif (foo<=baz and foo+bar==baz):
  print "c"
else:
  print "d"

Answers:

  • a
  • b
  • c
  • d

Question 2:
The following function should tell whether a given number x is even or odd. Complete the condition:

def testnum(x):
  if(x ___________):
     print "x is an even number"
  else:
     print "x is an odd number"