Input & Output (I/O)

Input & Output (I/O)#

In programming languages and computing more broadly, I/O stands for Input and Output.

We’ve seen I/O at work in previous chapters, where we provided inputs to the computer (using a CPU simulator or working at the terminal), a task or operation was performed, and there was an endpoint or output for that process.

Similarly, programming languages can take a variety of inputs (user-provided values, data, files, etc) and return outputs in a variety of formats (data stored in memory, output that shows up in the console, newly-created or -modified files, etc).

Examples#

In future chapters, we’ll do more with with aspects of I/O that have to do with reading and writing files. But for now, we’ve already seen I/O in action in Python via print() statements.

One way Python accepts input is via the input() function, which accepts a user input and assigns that to a variable.

A sample program that asks a user to enter their name:

# initial prompt
print("Enter your name: ")

# input function
name = input()

# output statement
print("Hello, " + name)

We can modify that program to include the initial prompt as part of the input() statement.

# initial prompt with input function
name = input("Enter your name: ")

# output
print("Hello, " + name)

NOTE: The default data type for a variable created using the input() function is a string object. We can change data types using the functions we’ll cover in the last section of this lab.

Comprehension Check#

Clipboard icon I/O Comprehension Check

Application#

Q6: Write a program that asks the user to enter their favorite color, and then prints a "Your favorite color is..." message. Answer to this question includes program + comments that document process and explain your code.

Q7: Write a program that accepts user input for the discrete pieces of information in “the standard Notre Dame introduction.”

  • Name

  • Class year

  • Home state/country

  • Major(s) / minor(s)

  • Residence hall

Use concatenation in combination with a print() statement to output a narrative introduction using the named variables.

Answer to this question includes program + comments that document process and explain your code.

Sample output for this program:

Knute Rockne, class of 1918, is majoring in pharmacy. He is originally from Norway but grew up in Chicago. He lives in Sorin Hall.