Back to the roadmap

Programming I · Values, variables, and expressions

Prerequisite: arithmetic with whole numbers. Learn to trace assignments, distinguish integers from strings, and choose the correct division operator.

Lesson and summary

An expression produces a value. Assignment evaluates the right-hand expression first and then binds its result to a name. Python can bind a name to another value later. Integers represent whole numbers, floats represent approximations to real numbers, and strings hold text. Parentheses control grouping. With a positive integer divisor, // gives the floor quotient and % gives the remainder, so n = (n // d) × d + n % d.

  1. Read the current value minutes = 137
  2. Evaluate the right-hand expression 137 + 15 = 152
  3. Bind the result to the name minutes = 152
Assignment evaluates before it changes the binding.

Worked example

minutes = 137
hours = minutes // 60
remainder = minutes % 60
minutes = minutes + 15
print(hours, remainder, minutes)

The program prints 2 17 152. The first two assignments use 137, so updating minutes does not recalculate hours or remainder. In a second example, "12" + "3" produces the text "123". To add the numbers, convert both strings with int(), which produces 15 for those valid integer strings.

Common mistake

Use / when you want a fractional result. For example, 7 / 2 is 3.5. Use // for floor division. With negative values it rounds down, so −7 // 2 is −4.

Practice

Predict 9 // 4 and 9 % 4. Then trace x = 5, y = x + 2, x = x * 3. State the final values of x and y.

Practice solution

9 = 2 × 4 + 1, so the quotient is 2 and the remainder is 1. y becomes 7 before x is rebound to 15. The final values are x = 15 and y = 7.

Mini-exam · 10 points

Convert 185 minutes into hours and leftover minutes with Python expressions (4 points). Explain what 185 / 60 represents (2 points). Trace total = 8, next_total = total + 4, total = total * 2 and state both final values (4 points).

Exam answer key

185 // 60 gives 3 and 185 % 60 gives 5 (2 points each). / gives the fractional duration in hours, about 3.0833 (2). next_total becomes 12 and total later becomes 16 (2 each).

Python documentation · Numbers and expressions

Next lesson: Branches and tests

Virtual lab · Trace a time conversion

Compare whole hours, leftover minutes, and fractional hours.

Free tool built into this page. Use it in your browser without an account or download.

Procedure

Predict the outputs for 59, 60, and 137 minutes. Enter each value and record the three outputs in order: whole hours, remaining minutes, fractional hours. In Python Tutor, run the lesson code and step through each assignment. Which values change when minutes is reassigned?

Expected results and answer check

59 gives 0 hours and 59 minutes. 60 gives 1 hour and 0 minutes. 137 gives 2 hours and 17 minutes, or about 2.283 hours. Reassigning minutes changes only minutes unless the other expressions run again.

The built-in tool models this conversion. Python Tutor runs the actual Python code in a browser.

Python Tutor · Use the free browser visualizer. The built-in activity also works on its own.

© CurriVerse XFree knowledge · Open collaboration