๐ฏ Learning Objectives
- โจ Understand the fundamental purpose of `input()` and `print()` functions in Python.
- โ๏ธ Differentiate between how `input()` takes user data and `print()` displays output.
- ๐ ๏ธ Apply `input()` and `print()` correctly in simple Python programs.
- ๐ง Recognize common use cases for each function.
๐ Required Materials
- ๐ป Computer with Python 3 installed.
- ๐ Text editor or Integrated Development Environment (IDE) like VS Code or PyCharm.
- ๐ Internet access (optional, for further research).
โฑ๏ธ Warm-up Activity (5 minutes)
Think-Pair-Share:
- ๐ค Think: "Imagine you're building a simple calculator. How would you get numbers from the user? How would you show the result back to them?"
- ๐ฃ๏ธ Share: Discuss your ideas with a partner (or reflect individually).
๐ก Main Instruction: Unpacking Input() vs Print()
โก๏ธ The `print()` Function: Showing Information
- ๐ฅ๏ธ Purpose: The `print()` function is used to display output to the console. It's how your Python program "talks" to the user.
- ๐ Syntax: The basic syntax is `print(value1, value2, ..., sep=' ', end='\n')`.
- ๐ฌ Common Use Cases:
- ๐ฃ Displaying messages or instructions to the user.
- ๐ข Showing the result of calculations or variable values.
- ๐ Debugging by printing intermediate values.
- โ๏ธ Example:
print("Hello, world!")
name = "Alice"
print("My name is", name)
result = 10 + 5
print("The sum is:", result)
- โ๏ธ Key Points:
- ๐ By default, `print()` adds a space between items (using `sep=' '`) and moves to a new line at the end (using `end='\n'`).
- ๐ You can customize `sep` (separator) and `end` (what to add at the end) arguments. For instance: `print("A", "B", "C", sep="-")` would output `A-B-C`.
โฌ
๏ธ The `input()` Function: Getting Information
- ๐ Purpose: The `input()` function is used to get input from the user via the console. It's how your Python program "listens" to the user.
- ๐ Syntax: The basic syntax is `variable = input("Prompt message here")`.
- ๐ฌ Common Use Cases:
- ๐ค Asking the user for their name or age.
- ๐ฎ Getting choices or commands in an interactive program.
- โ Soliciting data for calculations or processing.
- โ๏ธ Example:
user_name = input("What is your name? ")
print("Nice to meet you,", user_name)
age_str = input("How old are you? ")
# CRITICAL: input() always returns a string, so convert if needed!
age = int(age_str)
print("You are", age, "years old.")
- โ๏ธ Key Points:
- ๐ค The value returned by `input()` is always a string, even if the user types numbers.
- ๐ข If you need to perform mathematical operations, you must convert the input string to an integer (`int()`) or a float (`float()`).
- ๐ข The "prompt message" is optional but highly recommended to tell the user what to type.
๐ Key Differences: Input() vs Print()
| Feature |
`print()` Function |
`input()` Function |
| ๐ฏ Primary Role |
Displays output to the console. |
Gets input from the user. |
| โ๏ธ Direction of Data Flow |
Program to User (Output). |
User to Program (Input). |
| ๐ฆ Returns Value |
Returns `None` (it just performs an action). |
Returns the user's input as a string. |
| โ Argument Purpose |
Values to be displayed. |
An optional prompt message for the user. |
๐ Assessment: Practice Quiz
Choose the correct function or provide the output/code snippet.
- โ Which function would you use to show the message "Welcome!" to the user?
# Your code here
- โ To get the user's favorite color, which function is appropriate?
# Your code here
- โ What will be the output of the following code?
name = "Charlie"
print("Hello,", name, "!")
- โ If a user types `25` when prompted by `input("Enter your age: ")`, what is the data type of the value received by the program?
- โ Write a Python statement that asks the user for their favorite number and stores it in a variable called `fav_num`.
# Your code here
- โ Correct the following code so it can add two numbers entered by the user:
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
sum_result = num1 + num2
print("The sum is:", sum_result)
- โ What is the main difference in data flow between `input()` and `print()`?