Skip to content
Lesson 2 of 10 · Python for Complete Beginners

Variables and arithmetic

Name values with variables, calculate with the operators, and read input.

16 min

A variable is a name for a value. You create one with =, which means store this value under this name, not "equals" as in maths:

python4 lines
price = 4
quantity = 3
total = price * quantity
print(total)    # 12

You can change what a name points to at any time: quantity = quantity + 1 reads the old value, adds one and stores the result back. Good names say what the value is: total_cost, not x. Use lowercase with underscores.

Python does arithmetic the way a calculator does, and obeys the usual order: brackets, then powers, then * / // %, then + -.

/ always gives a float (7 / 2 is 3.5). // divides and throws away the remainder (7 // 2 is 3). % gives only the remainder (7 % 2 is 1). Together they answer "how many whole groups, and what is left?"

input("Question? ") pauses, shows the question and returns what the user typed as a string, even if they typed digits. Wrap it in int() or float() to convert:

python2 lines
age = int(input("Age? "))
print(age + 1)
Reference

The arithmetic operators

+

Meaning
add
Example
7 + 2
Result
9

-

Meaning
subtract
Example
7 - 2
Result
5

*

Meaning
multiply
Example
7 * 2
Result
14

/

Meaning
divide (always a float)
Example
7 / 2
Result
3.5

//

Meaning
floor divide
Example
7 // 2
Result
3

%

Meaning
remainder
Example
7 % 2
Result
1

**

Meaning
power
Example
7 ** 2
Result
49

Brackets always win: `(1 + 2) * 3` is `9`, but `1 + 2 * 3` is `7`.

A shop sells pens at $1.50 each. You buy 6 and pay with a $20 note. How much change do you get?

  1. 1

    pens = 6 price = 1.5

    Name the two facts so the numbers are not scattered through the code.

1/4
Try it

Change one input at a time. Each one is a variable: the program is the same formula, and only the values change. Set the tax to 0 and check the total equals price × quantity.

10 $
3
10 %

Total to pay

—

Exercise4 checks
python

Write total_cost(price, qty, tax_percent) that returns the price of qty items, plus tax, rounded to 2 decimal places.

For example total_cost(10, 3, 10) is 33.0: three items cost 30, and 10% tax adds 3.

Your code must satisfy

  • total_cost(10, 3, 10)→33ten percent tax
  • total_cost(19.99, 2, 0)→39.98no tax
  • total_cost(5, 1, 20)→6one item
  • total_cost(3, 3, 7)→9.63rounds
starter code2 lines
def total_cost(price, qty, tax_percent):
    pass
Exercise3 cases
python

Read a number of days from input and print it as whole weeks and leftover days, in this form:

code1 line
2 weeks, 3 days

So 17 days prints 2 weeks, 3 days. Always use the plural words.

Your code must satisfy

  • 17 days
  • exact weeks
  • under a week
starter code1 line
days = int(input())
  • A variable is a name for a value; = stores, it does not compare.
  • / always returns a float, // drops the remainder, % keeps only the remainder.
  • Brackets and the usual order of operations decide which part is calculated first.
  • input() always returns a string: convert with int() or float() before doing sums.
Check yourself

Work these out by hand first.

1Numeric answer

What is 17 // 5?

2Numeric answer

What is 17 % 5?

3Numeric answer

What is 2 ** 10?

4Multiple choice

After x = 4 then x = x + 1, what is x?

  • A4
  • B5
  • C41
  • DAn error
5Multiple choice

A user types 12 at n = input(). What is n + n?

  • A24
  • B"24"
  • C"1212"
  • DAn error
6Fill the blanks

To turn the text the user typed into a whole number, wrap it in (...).

Capitalisation, spacing and punctuation are ignored when grading.

Practise this lesson, not just read it

In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of Python for Complete Beginners is planned around your week. Free.