Operators

Computer Science · Python

Question 1 of 10

0

    What does this Python code print? print(14 + 6)

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Operators explained

    Operators are symbols that combine values into new values, such as addition or comparison. Python provides arithmetic, comparison and logical operators, and a few of them behave differently from what many people expect.

    Python has arithmetic operators for addition, subtraction, multiplication, division, floor division, remainder and power. Comparison operators such as equal to, not equal to, less than and greater than return True or False. Logical operators are the words and, or and not.

    The division operator always returns a float, so 7 divided by 2 gives 3.5. Floor division returns the result rounded down to a whole number, so 7 // 2 gives 3. For negative numbers, floor division rounds towards negative infinity: -7 // 2 gives -4, not -3.

    The remainder operator % gives the leftover after division, and for a non-zero divisor its result takes the sign of the divisor, so -7 % 2 gives 1. The power operator ** raises a number to a power, so 2 ** 10 gives 1024.

    Logical operators return one of their operands rather than always True or False. For example, 0 or 5 gives 5. Python also allows chained comparisons, so 1 < x < 10 means the same as 1 < x and x < 10.

    Worked example

    print(17 // 5)
    print(17 % 5)
    print(-7 // 2)
    print(-7 % 2)
    print(2 ** 10)
    
    1. Floor division: ⌊17/5⌋=3\lfloor 17 / 5 \rfloor = 3, and the remainder is 17−5×3=217 - 5 \times 3 = 2.
    2. For -7, floor division gives -4, and the remainder is −7−2×(−4)=1-7 - 2 \times (-4) = 1.
    3. Power: 210=10242^{10} = 1024.

    Check: (−4)×2+1=−7(-4) \times 2 + 1 = -7, which matches the dividend.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      What does this Python code print? print(8 + 4)

    • Medium

      What does this Python code print? print(15 // 2)

    • Hard

      What does this Python code print? print((13 + 5) // 2)