Pylerno
English

Input and output

Convert text from a user into the type needed for a calculation.

On this page

The concept

input(prompt) displays a prompt and reads a line as a string. Even when the user types digits, the result is text. This lesson separates raw_age from age to show a useful boundary: raw input comes from outside the program; parsed data is ready for a particular operation.

The default example uses "20" so it can run unattended. Add --interactive to enter your own value. The small sys.argv check only chooses demonstration mode; imports and conditionals are explained in later lessons. Here, entering "abc" intentionally causes ValueError. Later you will handle that error at the program boundary rather than pretending the value is valid.

Worked example

python
import sys

raw_age = "20"
if "--interactive" in sys.argv:
    raw_age = input("Enter your age: ")
age = int(raw_age)
print(f"Next year you will be {age + 1}.")
print(float("12.5") + 2)
print("Age: " + str(age))
print(int(3.9))

Trace the result

  1. Without --interactive, raw_age stays at the prepared string "20". With the flag, input replaces that value.
  2. int(raw_age) parses the text. Only after this step can the program safely add the integer 1.
  3. The remaining calls show float parsing, conversion back to text for concatenation, and integer truncation.
text
Next year you will be 21.
14.5
Age: 20
3
Line-by-line walkthrough

Line 1: import sys

Load sys (interpreter information and command-line arguments). Importing makes these names available; it is not a call to every function in the module.

Line 3: raw_age = "20"

Evaluate '20', then bind or update raw_age with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Lines 4–5: if "--interactive" in sys.argv:

Evaluate '--interactive' in sys.argv. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.

Line 5: raw_age = input("Enter your age: ")

Evaluate input('Enter your age: '), then bind or update raw_age with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 6: age = int(raw_age)

Evaluate int(raw_age), then bind or update age with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.

Line 7: print(f"Next year you will be {age + 1}.")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print(float("12.5") + 2)

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 9: print("Age: " + str(age))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 10: print(int(3.9))

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Converting arbitrary input without handling errors can terminate a program. For this early lesson, read the final traceback line; do not solve the problem by using eval.

Check your understanding

What does this program print?

python
print(int("8") + 2)
Choose one answer

Search documentation

Share this page