Input and Output (I/O) Basics
Input and Output (I/O) Basics
Input and Output (I/O) is how programs interact with the world around them. Input is any information your program receives, and output is any information it sends out. Understanding I/O is a foundational skill in programming, paving the way for working with files, user interfaces, and network communications.
Objectives
By the end of this lesson, you will:
- Understand what Input and Output (I/O) mean in programming.
- Learn how to use basic input and output functions in Python and JavaScript.
- Prepare to explore File I/O in the next lesson.
1. What is I/O?
I/O stands for Input and Output:
- Input: Data that the program receives from a user, a file, or another program.
- Output: Data that the program sends to the screen, a file, or another program.
Think of I/O as a conversation between your program and its environment.
Example Scenarios:
- Input: A user types their name into a program.
- Output: The program displays a personalized greeting.
2. Input and Output in Python
Python provides simple and powerful ways to handle I/O.
2.1 Input in Python
The input()
function lets you collect data from the user:
# Get input from the user
data = input("What is your name? ")
print("Hello, " + data + "!")
2.2 Output in Python
Use the print()
function to display data:
# Output a message
print("Welcome to the world of programming!")
2.3 Formatting Output in Python
Python makes it easy to format output using f-strings:
name = "Alex"
age = 12
print(f"My name is {name} and I am {age} years old.")
3. Input and Output in JavaScript
JavaScript handles I/O in a slightly different way, often working with web browsers or environments like Node.js.
3.1 Input in JavaScript
Use the prompt()
function in a browser to get input:
// Get input from the user
const name = prompt("What is your name?");
console.log("Hello, " + name + "!");
In Node.js, you can use the readline
module for input:
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("What is your name? ", function (name) {
console.log(`Hello, ${name}!`);
rl.close();
});
3.2 Output in JavaScript
JavaScript uses console.log()
for output:
// Output a message
console.log("Welcome to the world of programming!");
4. Practice Activities
Activity 1: Greeting Program
Write a program that asks the user for their favorite color and then prints a message using that color.
-
Python Example:
color = input("What is your favorite color? ") print(f"Wow, {color} is a beautiful color!")
-
JavaScript Example:
const color = prompt("What is your favorite color?"); console.log(`Wow, ${color} is a beautiful color!`);
Activity 2: Temperature Conversion
Create a program that:
- Asks the user for a temperature in Celsius.
- Converts it to Fahrenheit.
- Prints the result.
-
Python Example:
celsius = float(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9/5) + 32 print(f"{celsius}C is equal to {fahrenheit}F.")
-
JavaScript Example:
const celsius = parseFloat(prompt("Enter temperature in Celsius:")); const fahrenheit = (celsius * 9) / 5 + 32; console.log(`${celsius}C is equal to ${fahrenheit}F.`);
5. What’s Next?
Now that you’ve learned the basics of Input and Output, you’re ready to explore File I/O. In the next lesson, we’ll learn how to read from and write to files, unlocking even more possibilities for interacting with data.
Happy coding!