codebyexample
module · Python

Formatted Console Output (Python)

This module practices table-style output, alignment, and precision control.

Learning Metadata

  • Difficulty: Beginner.
  • Estimated Time: 20-35 minutes.
  • Prerequisites: 01-foundations/types-and-io, 01-foundations/operators-and-expressions.
  • Cross-Language Lens: Compare stream manipulators, format strings, fmt, and Python formatting for the same reporting task.

Learning Outcomes

  • FND-FMT-01: Produce stable human-readable tabular and numeric output.
  • FND-FMT-02: Choose precision, alignment, and labels appropriate to the data.

Quick Run

Run from the repository root:

python scripts/automation.py run-module --module-path languages/python/01-foundations/formatted-output-and-iomanip

Topics Covered

  • Column alignment for tabular output.
  • Fixed precision for monetary and statistical values.
  • Building readable reports from structured input.
  • Controlling output format based on user precision input.

Common Pitfalls

  • Producing unreadable tables with inconsistent widths.
  • Not validating precision ranges before formatting.
  • Forgetting to maintain numeric precision in totals and averages.

Cross-Language Notes

  • In Python, read this module through the native focus ?Formatted Console Output?; the shared folder name remains stable for side-by-side navigation.
  • Use dynamic values, collection protocols, and context managers to demonstrate how to produce stable human-readable tabular and numeric output.
  • Compare observable behavior with the other tracks when learning to choose precision, alignment, and labels appropriate to the data; equivalent evidence matters more than identical syntax.

Exercise Focus

  • exercises/01.py: collect product rows and print an aligned invoice table with totals.
  • exercises/02.py: compute summary metrics and print them using user-selected precision.

Exercise Specs

  1. exercises/01.py
  • Input: product count, then name, price, and quantity for each product.
  • Output: formatted table with line totals and grand total.
  • Edge cases: non-positive product count; long product names affecting alignment.
  1. exercises/02.py
  • Input: numeric list plus precision value from 0 to 6.
  • Output: count, sum, average, minimum, and maximum with selected precision.
  • Edge cases: empty numeric input; precision outside 0..6.

Check Your Work

Run from the repository root after implementing a starter:

python scripts/automation.py check-exercise --language python --level 01-foundations --module formatted-output-and-iomanip --exercise 01

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

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.