AD JB Toolbox 01 EN

Python Tutorial / Build a First Program with Variables, Input and Output

Python variables refer to values, input() reads text and print() displays results. Convert input text to a numeric type before using it in arithmetic, then format the result for the reader.

Distinguish assignment from comments

The assignment operator = associates the result on its right with the name on its left. It does not test mathematical equality. Reassigning a name makes later expressions use the new value. Choose meaningful names and avoid overwriting built-in names such as print, input or list.

amount = 3500
amount = amount + 500
print(amount)  # Display the calculated result

This prints 4000. Text after # is a comment rather than an executed instruction. Comments are most useful when they explain a choice, unit or constraint instead of merely restating the code.

For the relevant APIs and platform requirements, see Build a First Program with Variables, Input and Output documentation.

Run the input and output example

Save the following as input_expense.py and run python input_expense.py in the project environment. Enter lunch at the name prompt and 8000 at the amount prompt.

name = input("name: ").strip()
amount_text = input("amount: ").strip()
amount = int(amount_text)
print(f"{name}: {amount:,} KRW")

The program’s standard output, with the typed input omitted, is:

name: amount: lunch: 8,000 KRW

Follow each value through conversion

The first call reads an expense name and removes surrounding whitespace. amount_text remains a string until int() converts it to an integer. An f-string places expressions inside braces; the comma format specifier displays thousands separators.

A terminal normally echoes what you type, so its interactive line layout will differ from the output-only block above. Input waiting is also normal: a stationary cursor does not necessarily mean the program is stuck.

Change one input and inspect failures

  1. Enter 9000 instead of 8000 and check for 9,000 KRW.
  2. Enter 8000.5 and observe ValueError because int() does not parse a decimal-point string.
  3. Try an empty amount, then restore the valid 8000 input and confirm normal execution.

Track where text becomes a number rather than assuming that a numeric-looking input is already numeric. Handling invalid input and asking again requires a deliberate validation loop; the exception handling lesson explains how to add that behavior without hiding unrelated failures.

More in This Category
Python Tutorial / Install Python on Windows and Verify the Interpreter

Python Tutorial / Install Python on Windows and Verify the Interpreter

Set up Python on Windows with the official Install Manager, create a project environment, and verify command resolution, versions and executable paths.

Python Tutorial / Set Up Python on macOS and Ubuntu

Python Tutorial / Set Up Python on macOS and Ubuntu

Prepare a Python development environment on macOS or Ubuntu without replacing system Python. Verify paths, create a virtual environment and test execution.

Python Tutorial / Create Virtual Environments and Manage Packages with pip

Python Tutorial / Create Virtual Environments and Manage Packages with pip

Use venv and python -m pip to separate projects, record dependencies and troubleshoot interpreter, package and activation problems.

Python Tutorial / Write and Run Python in VS Code

Python Tutorial / Write and Run Python in VS Code

Choose a virtual-environment interpreter in VS Code, run a complete Python script and use a breakpoint to check values while diagnosing environment mismatches.

Python Tutorial / What Is Python? Features and Practical Uses

Python Tutorial / What Is Python? Features and Practical Uses

Understand Python’s role, distinguish source files from the interpreter and terminal, and run a complete expense-total example with a small follow-up experiment.

Python Tutorial / Build a First Program with Variables, Input and Output

Python Tutorial / Build a First Program with Variables, Input and Output

Read expense input, convert text to an integer and format a result. Understand assignment, comments, f-strings and the source of common conversion errors.