Skip to content
Lesson 3 of 13

Step 1 of 8 · Reading · ~4 min

Learn

Getting Started

Working with Numbers

Python does arithmetic with no imports and no setup. By the end of this lesson you can combine numbers with the seven arithmetic operators, choose between Python's two division operators on purpose, and turn a line of input into a number you can compute with.

A line of input is text, not a number

input() hands back a string, always, even when every character on the line is a digit. Strings do not do arithmetic - * on a string repeats it:

python

The built-in int(...) turns a string of digits into an int:

python

int(input()) is the standard one-step idiom: read the line, convert it. Do that once per line, as you read it, and the rest of your program works with real numbers. The wider conversion story

  • decimals, text that is not a number at all - is the next lesson.

The seven arithmetic operators

python

/ and // are different operators

/ is true division and always produces a float, even when the answer comes out even: 9 / 3 is 3.0, not 3. // is floor division: it rounds down, toward negative infinity, and keeps whole numbers whole. 7 // 2 is 3, and -7 // 2 is -4, not -3.

Use / for an exact mathematical value, // when the answer must be a whole number.

% gives the remainder

a % b is what is left over after floor division. 17 % 5 is 2, because 17 is three fives plus 2. Together the two operators split a quantity into whole units and a leftover:

python

% is also how you test divisibility: a number n is even exactly when n % 2 is 0.

Order of operations

Python uses ordinary maths precedence: parentheses first, then **, then * / // % (left to right), then + and - (left to right).

python

Those last two lines are the trap: a factor in front of a sum applies to the whole sum only when the sum is in parentheses. When in doubt, add them; they cost nothing.

Floats are not exact

Decimal fractions such as 0.1 cannot be stored exactly in binary, the same way 1/3 cannot be written exactly in decimal:

python

That is how IEEE 754 arithmetic works everywhere, not a Python quirk. Never compare floats with ==; compare against a small tolerance, or keep whole numbers whole with //.

A few built-ins you get for free

python

max works the same way; import math adds math.sqrt, math.pi and more.

Common mistakes

  • Computing on the raw input(). Multiplying two unconverted lines raises TypeError: can't multiply sequence by non-int of type 'str' and the program dies with no output at all. Convert as you read: int(input()).
  • Losing the parentheses around a sum. 2 * 5 + 3 is 13, while 2 * (5 + 3) is 16. The first form doubles one number instead of the total, so the answer is short by the second number.
  • Printing two values with one print. print(15, 16) puts 15 16 on a single line with a space between them. Two numbers on two lines means two separate print calls.

Your exercise

Read a rectangle's width and height, then report its area on the first line and its perimeter on the second. The starter already reads both lines and converts them, so you are adding the two print calls only. The slip that catches most people is the perimeter: the doubling applies to the sum of the two sides, so that sum needs parentheses of its own.

Up nextType ConversionGetting Started

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…