Expressions, input, and type conversion

20-minute lesson · module-02-simple-data

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Expressions, input, and type conversion

20-minute lesson · module-02-simple-data

  • Build arithmetic expressions using +, -, *, /, //, %, and **.
  • Read user input with input() and convert strings to numbers.
  • Predict the difference between integer division (//) and true division (/).
  • Recognize TypeError and ValueError when conversions fail.

Expressions are recipes for a single value

An expression is anything Python can compute down to a single value. These are all expressions:

3 + 4         # arithmetic
"hi" + "!"    # string concatenation
10 / 4        # division -> 2.5
10 // 4       # integer (floor) division -> 2
10 % 4        # remainder (modulo) -> 2
2 ** 8        # exponent (2 to the 8th) -> 256

Python evaluates expressions following standard precedence: parentheses, exponents, then multiplication/division/modulo, then addition/subtraction, left to right within the same level.

input() asks the user for a line of text

name = input("What is your name? ")
print("Hello,", name)

The thing input() returns is always a string — even if the user types digits. To do math on it, convert with int(...) or float(...):

raw = input("How old are you? ")
age = int(raw)
next_age = age + 1
print("Next year you will be", next_age)

Type conversion can fail

  • int("12")12 (works)
  • int("twelve") → raises ValueError
  • int("12.5") → raises ValueError (use float first if there's a decimal)
  • "hi" + 5 → raises TypeError (can't add a string and an int)
Predict-then-run. Before clicking Run on the editor below, write down what the program would print on a piece of paper. Then run it and compare.

Examples

Tip calculator

bill = 50
tip_pct = 18
tip = bill * tip_pct / 100
total = bill + tip
print("Tip:", tip)
print("Total:", total)

`bill * tip_pct / 100` evaluates left to right: first the multiplication, then the division. The total adds the original bill back in.

Checkpoint questions

What does this program print?

print(7 // 2, 7 % 2)

What will Python print on the last line?

raw = "42"
n = int(raw)
print(n + 1)

Which line raises a ValueError?

a = int("12")
b = int("twelve")
c = float("3.14")

Hands-on coding

Sum two integers from variables

Pyodide is loading (one-time, ~12 MB)…

ActiveCode
Output
(no output yet)

Convert minutes to hours and minutes

Pyodide is loading (one-time, ~12 MB)…

ActiveCode
Output
(no output yet)

CodeLens — step through execution

Trace integer vs. true division

CodeLens — step through frames, objects, and references

CodeLens
Code · Python 3
1a = 7
2b = 2
3true_div = a / b
4floor_div = a // b
5remainder = a % b
6print(true_div, floor_div, remainder)
7
line that just executed next line to execute
Print output
(click Trace)

Frames

Click Trace to start.

Objects

no steps yet
Edit the code

Debugging interlude

Predict the error first (+50 XP), then fix the code (+100 XP). Hints cost 30 XP each.

🐞

Why does this raise TypeError?

The student wanted to print "Total: 25" but Python raises TypeError. The variable `count` was read from a CSV (so it's a string). Identify the type mismatch and fix it.

Predict · 50 XPFix · 100 XP
count = "20"
bonus = 5
print("Total: " + count + bonus)
Step 1 — Predict the error

Read the program. Which kind of error (or non-error) will Python raise?

End-of-lesson quiz

End-of-lesson quiz

  1. What does input() return?

Reflection

  • What is one situation where you would prefer // (integer division) over / (true division)?

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Simple Python Data / Variables / Values / Operators / Order of Operations / Input

Adaptation notes. Reading rewritten in our own words. Examples and hidden tests authored specifically for PyTutor AI. The order-of-operations table is summarized in our own phrasing.

License notes. Verify chapter-level license metadata on Runestone before copying any text verbatim. This lesson contains no verbatim quotations.