NOETRION

Choose a topic

LESSON 1 OF 9 · Python & data foundations
TUTORIAL · 4 MIN READ

Your first Python program: input, variables, and output

Build a tiny calculator and learn to read what each line does.

Your first useful Python program should be small enough that you can predict its answer. We will build a study-time calculator: enter the number of sessions you completed, multiply by a fixed session length, and display the total minutes. This gives every line a job and makes mistakes easy to investigate.

You need a working Python interpreter and a way to run a file. Save the examples as study_time.py and run python study_time.py or python3 study_time.py, depending on your system. The examples use Python 3.12 and require no third-party packages.

Build the smallest complete program

text = input("How many study sessions did you complete? ")
sessions = int(text)
minutes_per_session = 25
total_minutes = sessions * minutes_per_session
print("Study time:", total_minutes, "minutes")

Type 3 when the prompt appears. The program should print Study time: 75 minutes. The value 25 is our chosen session length, not a scientific recommendation about attention. You can change it to match your own routine.

The Python documentation for input() explains that it returns text. Typing digits does not automatically give you an integer. We explicitly convert the returned string with int() before doing arithmetic. Multiplication then calculates a new value; it does not change the original input string.

Trace values instead of memorizing lines

Trace for an input of 3
Line completedNameValueType
Inputtext"3"str
Conversionsessions3int
Configurationminutes_per_session25int
Calculationtotal_minutes75int
Read "3"Convert to 3Multiply by 25Display 75 minutes
Original teaching diagram: input, conversion, calculation, and output are four separate steps.

A variable is a name referring to an object. Here, meaningful names tell you whether a number means sessions or minutes. Avoid naming everything x merely because it is short. When a result looks wrong, tracing the units often reveals the problem before tracing Python syntax does.

Try placing print(type(text)) immediately after the input line. You will see <class 'str'>. Place print(type(sessions)) after conversion to see <class 'int'>. These temporary observations help explain the program. Remove them when the intended output is clear.

Learn from three different inputs

Enter 0. The result is zero minutes, which is a valid calculation. Enter -2. Python calculates negative minutes because our program has not yet enforced the meaning of a session count. Enter three. Conversion raises ValueError, because that word is not a valid integer representation.

These cases expose different problems. A mathematical operation can be valid while its input is unsuitable for the application. A conversion failure prevents the arithmetic altogether. Read the final line of an exception first, then find the named line in your file. The errors tutorial explains this distinction between syntax errors and exceptions during execution.

Add a useful boundary

text = input("How many study sessions did you complete? ")
try:
    sessions = int(text)
except ValueError:
    print("Please enter a whole number, such as 3.")
else:
    if sessions < 0:
        print("The session count cannot be negative.")
    else:
        total_minutes = sessions * 25
        print(f"Study time: {total_minutes} minutes")

Indentation groups the instructions Python should execute together. In this version, the conversion sits inside try. Its matching except handles an invalid integer. The else block runs when conversion succeeds, and the condition checks the application's nonnegative-count rule.

This is intentionally a one-attempt program. It prints an explanation and finishes; it does not keep asking indefinitely. When you learn loops, you can add repeated entry. Build one behavior at a time so you can tell which change caused a failure.

Make the output easy to check

The last line uses an f-string: an f before the quotes lets a value inside braces appear in the text. The input and output tutorial documents this formatting. Include the unit rather than printing a bare number. A correct 75 is still ambiguous if the reader cannot tell whether it means sessions, minutes, or hours.

Practice before moving on

  1. Change the session length to 20. Predict the result for four sessions before running it.
  2. Test zero, a negative count, and a word. Explain which branch each input takes.
  3. Add a separate line displaying hours, calculated as total_minutes / 60.
Check your answers

Four 20-minute sessions total 80 minutes. Zero reaches the calculation, a negative count reaches the boundary message, and a word reaches the conversion message. Eighty minutes is approximately 1.333 hours; rounding the display is a separate formatting choice.

You are ready for the next lesson when you can explain why input() returns a string, trace the conversion and multiplication, and predict an output without running the code first.

Sources and further reading

Source links checked October 13, 2026. Worked examples are original teaching examples; they are not research findings or product benchmarks.

Check your understanding

What does input() return before int() converts it?

Choose one answer