Skip to main content

Values, Names and Execution

What this concept is​

A program is a sequence of instructions. Python evaluates an expression to produce a value. Assignment binds a name to that value; it does not state an algebraic equation that stays true forever. Text such as "25" and the integer 25 are different values with different permitted operations.

Why it matters here​

The tracker receives text and calculates minutes. If you concatenate text instead of adding integers, your report can look plausible while being wrong.

If no Python command is available yet, follow the setup and first-run bridge within this lesson's allocation.

Concrete example​

Create first.py in your editor:

first = 25
second = 15
total = first + second
first = 30
print(total)
print("25" + "15")

Run python3 first.py in that folder (Windows: py -3 first.py). The outputs are 40 and 2515. Changing first does not recompute total. The file is your source; the terminal output is a result of running it. Do not paste the interactive prompt characters >>> into a file.

Read one instruction at a time​

Python runs the top-level instructions in a file from top to bottom. For the example above, trace the names on paper:

  1. first = 25: evaluate the right side, then bind first to integer 25.
  2. second = 15: bind a second name to 15.
  3. total = first + second: look up both values, add them, then bind total to 40.
  4. first = 30: rebind first. The previously computed total is still 40.
  5. print(total): look up total and display it. Printing does not change it.

An expression produces a value: first + second. A statement performs an instruction: total = first + second. A comment, starting with # outside a string, explains an intention to the reader. It is not an instruction to Python.

Types and arithmetic you will actually use​

An int is a whole number such as 25; a float represents numbers such as 2.5; a str holds text such as "study"; a bool is True or False. None represents the absence of a value in contracts that choose that convention. It is different from zero or an empty string.

For positive whole study minutes, 125 // 60 gives 2 complete hours and 125 % 60 gives 5 remaining minutes. Ordinary division, 125 / 60, gives a fractional number. * and division bind more tightly than +: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. Use parentheses when they make your intention clearer. Whole minutes keep this tracker simple; do not convert fractional minutes to integers just to hide a data-model decision.

Names are case-sensitive: minutes and Minutes are different. Prefer descriptive names such as session_minutes, avoid spaces in names, and do not name a variable int, input or print: that would hide a tool you need later.

From keyboard text to a useful result​

Save this as a separate file, one_session.py:

raw_minutes = input("Minutes studied: ")
minutes = int(raw_minutes)
hours = minutes // 60
remaining = minutes % 60
print(f"Study time: {hours} hours and {remaining} minutes")

Enter 125 and predict the sentence before running. input displays the prompt and waits for a line; pressing Enter supplies the text. int attempts conversion. The f before the output string allows expressions inside braces to be inserted into the sentence. Without f, the braces would appear literally.

Now enter two. Conversion fails before the calculation or final print. Conversion answers “can this text represent an integer?”; it does not answer “is this an allowed session length?” Entering 0 succeeds here, although the later tracker will reject it. Lesson 5 separates these checks.

If the first run is confusing​

Run the command in a terminal, not at a Python >>> prompt. Save the editor file first. A “can't open file” message calls for checking your folder and filename; it is different from an error inside your program. A script containing just 2 + 3 displays nothing: use print(2 + 3). Start a new run after editing; values from the previous run do not automatically survive.

Common confusion / misconception​

= assigns; == compares. Quotes create text. input() returns text even if the user types digits. int("25") converts suitable text, but int("two") raises an error. A name must be assigned before it is read.

How to use it​

Predict each line, then run it. Use type(value) when unsure of the data type. Save the file before running again; make one change at a time.

Check yourself​

What does total contain after first changes? Why does text addition differ? Answer: 40; string addition concatenates rather than performing arithmetic.

Mini drill or application​

Write a three-session total using integer variables. Change one session, recompute the total and explain why the recomputation matters. Then deliberately use an undefined name and identify the line reported.

Read this only if stuck​

Official source: Python tutorial. Read only the matching section after attempting the example. These lessons and exercises are original curriculum material.