Introduction
In Python, input and output operations allow interaction with users. Using basic Python functions such as input()
and print()
, you can collect data from users and display information to the screen. These are foundational concepts that are essential in developing interactive Python programs.
Objective
The objective of this topic is to understand the usage of input and output functions in Python. We will learn how to take input from the user, process it, and display it using the input()
and print()
functions.
Python Code Example
# This program takes user's name and age as input and displays a greeting message
# Taking input from the user
name = input("Enter your name: ")
age = input("Enter your age: ")
# Displaying output to the user
print(f"Hello, {name}! You are {age} years old.")
Explanation of the Program Structure
The Python program above uses two main input-output functions:
input()
: This function is used to accept user input. In this case, it collects the user’s name and age as string data.print()
: This function is used to output the data to the screen. Here, it displays a personalized greeting message using the user’s name and age.
How to Run the Program
To run this Python program, follow these steps:
- Ensure you have Python installed on your machine (version 3.x is recommended).
- Open a text editor and copy the code above into a new file, save it with a
.py
extension (e.g.,greeting.py
). - Open the terminal (or command prompt), navigate to the folder where your
.py
file is saved, and run the program by typingpython greeting.py
. - The program will prompt you to enter your name and age. Once entered, it will display the greeting message.