Courses / Computer Science I
Programming I

Input, Output, and File Thinking

Computer Science I 248 words Free to read

Getting Data In and Out

A program that cannot communicate is useless. Input brings data in from the keyboard, files, or networks, while output sends results out to the screen, files, or devices.

Input usually arrives as a string, even when it looks like a number. Reading "42" from the keyboard gives the string "42", not the integer 42.

To do arithmetic, you must convert (parse) it using int("42")42int("42") \rightarrow 42. Forgetting this is why input "1" + "1" produces "11" instead of 2.

Input SourceReturned TypeAction Required
KeyboardStringParse with int()int() or float()float()
FileStringParse before math operations

File Thinking and Streams

Files store data that outlasts a single run. The standard file discipline requires three steps: open the file, process it, and close it to flush writes.

StepPurpose
OpenConnect to the file in read or write mode
ProcessRead from or write data to it
CloseRelease the resource and flush writes

A stream reads a file piece by piece (like line by line) rather than loading it entirely into memory. This handles files far larger than available RAM.

Common Pitfall: Treating input as already numeric. Always convert strings before math, and expect failures if the text isn't a valid number.
A file wider than memory, handled two ways -- one of them fits

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes

Programming I