codebyexample
module · Python

Types and Input/Output (Python)

This module practices typed input, safe parsing, and basic numeric summaries.

Learning Metadata

  • Difficulty: Beginner.
  • Estimated Time: 20-35 minutes.
  • Prerequisites: None; this is the entry module for the track.
  • Cross-Language Lens: Compare strongly typed console input in C++ and C# with explicit parsing in Go and Python's more dynamic input model.

Learning Outcomes

  • FND-TIO-01: Choose suitable primitive values and variables for a small problem.
  • FND-TIO-02: Read, validate, transform, and present console data.

Quick Run

Run from the repository root:

python scripts/automation.py run-module --module-path languages/python/01-foundations/types-and-io

Observe, Predict, Modify

Enter Ada Lovelace, 20, and 3.5, pressing Enter after each value.

Look for these result lines (interactive prompts and the summary heading are omitted):

Name: Ada Lovelace
Age: 20
GPA: 3.50
Adult: True

Change the age to 17 and predict the boolean before running again. input() returns text; int() and float() convert it. Invalid numeric text raises ValueError in this introductory example.

Restore the original values before moving on to the exercises.

Topics Covered

  • Reading typed values from standard input.
  • Computing sum, average, minimum, and maximum.
  • Parsing structured one-line input into typed fields.
  • Printing results with clear labels and numeric formatting.

Common Pitfalls

  • Assuming input parsing always succeeds without validation.
  • Accepting non-positive counts when statistics require at least one value.
  • Mixing integer and floating-point arithmetic incorrectly.

Cross-Language Notes

  • Python starts with strings too, but dynamic values make early experiments feel lighter than in the statically typed tracks.
  • Compared with C++ or Go, most mistakes show up at runtime rather than during compilation.
  • The key comparison is fast experimentation versus the stronger guardrails of explicit typed input pipelines.

Exercise Focus

  • exercises/01.py: read N numeric values and print sum, average, minimum, and maximum.
  • exercises/02.py: parse product price quantity and print the computed total price.

Exercise Specs

  1. exercises/01.py
  • Input: integer N followed by N numeric values.
  • Output: summary lines for sum, average, minimum, and maximum.
  • Edge cases: N <= 0; repeated values where minimum equals maximum.
  1. exercises/02.py
  • Input: single-line record: product price quantity.
  • Output: parsed product name and computed total price.
  • Edge cases: wrong token count; quantity = 0.

Check Your Work

Run from the repository root after implementing a starter:

python scripts/automation.py check-exercise --language python --level 01-foundations --module types-and-io --exercise 01

Change --exercise 01 to --exercise 02 for the second task. Consult exercises/solutions/ only after making a complete attempt.

Visible Practice Cases

These cases come from the exercise checker. Implement the general behavior; do not hardcode these answers. Each input line below is a separate line of standard input. The unmodified starters are incomplete, so a failed check before implementation is expected.

Exercise 01

Normal input:

3
10
20
30

Expected result labels and values (prompts omitted; decimal formatting may add zeros):

Sum: 60
Average: 20
Minimum: 10
Maximum: 30

Also check these boundary cases. The checker compares their full output, including prompts and spaces. In this table, \n means a newline; input typed by the user is not part of the program output.

CaseInput lines (\n separates lines)Exact program output
edge: N <= 00\n10\n20\n30How many numbers? Please enter a positive count.\n
edge: repeated values where minimum equals maximum3\n5\n5\n5How many numbers? Value 1: Value 2: Value 3: Sum: 15.0\nAverage: 5.0\nMinimum: 5.0\nMaximum: 5.0\n

Exercise 02

Normal input:

notebook 2.50 4

Expected result labels and values (prompts omitted; decimal formatting may add zeros):

Product: notebook
Total price: 10

Also check these boundary cases. The checker compares their full output, including prompts and spaces. In this table, \n means a newline; input typed by the user is not part of the program output.

CaseInput lines (\n separates lines)Exact program output
edge: wrong token countnotebook 2.50Enter product price quantity: Invalid format. Use: product price quantity\n
edge: quantity = 0notebook 2.50 0Enter product price quantity: Product: notebook\nTotal price: 0.0\n
edge: extra product fieldnotebook 2.50 4 extraEnter product price quantity: Invalid format. Use: product price quantity\n

Checkpoint

  • I can explain the core ideas of this module.
  • I can run and modify example/main.py.
  • I completed exercises/01.py.
  • I completed exercises/02.py.
  • I validated at least one edge case for each exercise.

Content revision 1d0b3f4

main.pyLoading draft…

Loading your editor…

Checking execution availability…

CONSOLE Ready when you are

Your output and feedback will appear here.

← Back to the curriculumYour next insight is one experiment away.