Python Programming · Middle Stage (6–8) · 1 course, 14 worked problems, 31 practice questions
A variable is a name that holds a value, like a labelled box. In Python you make one with =, for example mass = 2.5. Each value has a type. Whole numbers are int, numbers with a decimal point are float, text is str, and True or False is bool. The type decides what you can do with a value. You can add two numbers, but adding a number to a string gives an error. The function type() tells you the type, and int(), float() and str() change one type to another. Clear names such as speed make code easy to read.
int, float, str and booltype()Choose an answer, then check it. Each one shows a full explanation.
A program converts values with int(), str() and float() before it uses them.
print(int("7") + int(7.9), str(3) + str(4.0), float("2") * 2)
What does the program print?
Half of seven is turned into a whole number in two ways: by rounding it and by converting it with int(), for the positive and the negative value.
a = 7 / 2
print(a, round(a), int(-a), round(-a))
What is the output?
A program holds a number as text, and multiplies it in two ways. a = "7" b = 2 print(int(a) * b, a * b) What does the program print?
A student turns values into numbers with int() and float(). Each line below is tried on its own.
Which lines run without an error? Choose three.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
Two names get new values in one assignment, and the new value of b uses a.
a, b = 1, 2
a, b = b, a + b
print(a, b)
What does the program print?
Whole numbers can be written in binary, octal or hexadecimal, and with underscores. A student checks five statements about such literals.
Which of these expressions are True? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A program turns four floats into whole numbers, three with round() and one with int().
print(round(2.5), round(3.5), round(-1.5), int(-1.7))
What does the program print?
A program compares floating point results with ==.
x = 0.1 + 0.2
print(x == 0.3, round(x, 2) == 0.3, 3 * 0.1 == 0.3)
What does the program print?
A student predicts the type of the result of some expressions.
Which of the type tests below are True? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
Four assignments are offered. Each one is tried alone, as a complete program. Which assignment is accepted by Python?
Each one has a hint, the key idea and a full worked solution.