Python Programming · Middle Stage (6–8) · 1 course, 13 worked problems, 28 practice questions
An expression is a piece of code that Python works out to give a value, such as 3 + 4 * 2. Operators are the symbols that act on values. The arithmetic operators are +, -, *, /, ** for powers, // for whole-number division and % for the remainder. Python follows the usual order of operations, so brackets come first and * comes before +. Comparison operators such as ==, < and != give True or False, and and, or and not combine them. Expressions turn formulas from science into working code.
// and % to split a number into partsChoose an answer, then check it. Each one shows a full explanation.
One line of arithmetic, with no brackets at all, uses the remainder operator, a product, floor division and a sum. print(17 % 5 * 2 + 10 // 4) What does the program print?
Bitwise operators work on the binary digits of whole numbers. Each line below combines 6 and 3, or shifts 1, and compares the result with ==.
Pick every comparison that comes out True. More than one may be right.
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 splits a four-digit number into parts with // and %.
n = 4725
a = n // 100
b = n % 100
c = n // 10 % 10
print(a, b, c)
What does the program print?
Seven divided by seven is worked out with / and with //. The two results are compared, and then a string and True are each tested against the number one.
a = 7 / 7
b = 7 // 7
print(a == b, type(a) == type(b), "1" == 1, True == 1)
What does the program print?
A program uses the bitwise operators on two small whole numbers. x = 6 y = 3 print(x & y, x | y, x ^ y, ~x, x << 1 >> 2) What does the program print?
A program does integer division and remainder on a negative number. a = -7 print(a // 2, a % 2, -a // 2) What does the program print?
A program mixes unary minus and powers. print(-2 ** 2, 2 ** 3 ** 2, (-2) ** 2) What does the program print?
A student compares text, lists and numbers with the operators <, > and ==, and wants to know which of five results are true.
Which of these comparisons 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.
Which comes first, the minus or the division? What does print(20 - 6 / 2) show?
Two number tools together: what does print(abs(-8) + round(2.6)) show?
Each one has a hint, the key idea and a full worked solution.