Basic Input and Output in Python

  1. Getting Started with Python
  2. Variables and Data Types in Python
  3. Basic Input and Output in Python
  4. Conditional Statements in Python
  5. Loops in Python
  6. Lists and Tuples in Python
  7. Dictionaries and Sets in Python
  8. Functions and Modules in Python
  9. Exception Handling in Python
  10. File Handling in Python
  11. Working with Dates and Times in Python
  12. List Comprehensions in Python

Welcome back to our Python programming series for beginners! In this third installment, we’ll explore the essential concepts of basic input and output (I/O) in Python. Understanding how to interact with users through input and display information through output is fundamental for building interactive and user-friendly Python programs. Let’s dive into the world of I/O in Python.

Introduction to Basic I/O in Python

In programming, input refers to the data or information provided to a program, and output is the result or information produced by the program. Python offers simple yet powerful tools to handle input and output. In this article, we’ll focus on two core functions: `input()` for taking user input and `print()` for displaying output.

Taking User Input with `input()`

The `input()` function allows your Python program to wait for the user to enter text. You can prompt the user for information or instructions, and their input will be stored as a string. Here’s a basic example:

user_name = input("Please enter your name: ")
print("Hello, " + user_name + "!")

In this code, `input(“Please enter your name: “)` displays the message “Please enter your name: ” to the user. The user’s input is then stored in the `user_name` variable and displayed as part of the greeting.

Displaying Output with `print()`

The `print()` function is used to display information to the user or to output data for debugging and analysis. You can print text, variables, and expressions. Here are some examples:

print("Hello, Python!")
x = 10
y = 20
print(f"The sum of {x} and {y} is {x + y}")

In this code, we first print a simple greeting. Then, we use an f-string (formatted string literal) to display the sum of two variables, `x` and `y`, along with some descriptive text. This is a cleaner and more modern way to format strings in Python.

Combining Input and Output

Combining input and output functions allows you to create interactive programs. Here’s an example that takes the user’s name as input and then greets them:

user_name = input("Please enter your name: ")
print(f"Hello, {user_name}!")

This code first uses `input()` to obtain the user’s name and stores it in the `user_name` variable. Then, it uses `print()` with an f-string to display a personalized greeting.

Handling Numeric Input

When you use `input()`, Python treats the user’s input as a string, even if they enter a number. To work with numeric input, you’ll need to convert it to the appropriate data type (int or float). Here’s an example:

age_str = input("Please enter your age: ")
age = int(age_str)  Convert the input to an integer
print(f"In 10 years, you will be {age + 10} years old.")

In this code, we first accept the user’s age as a string, and then we convert it to an integer using `int()` so that we can perform arithmetic operations on it. The result is displayed using an f-string.

Handling User Choices

You can use user input to make decisions in your programs. For example, here’s a simple program that asks the user whether they like programming:

choice = input("Do you like programming? (yes/no): ")
if choice.lower() == "yes":
    print("That's great!")
    print("Maybe you'll learn to like it!")

In this code, we use `input()` to get the user’s choice and then use an `if` statement to provide different responses based on their input.


In this article, we’ve covered the basics of basic input and output in Python. You’ve learned how to use the `input()` function to take user input and the `print()` function to display output. These functions are the building blocks of interactive Python programs.

As you continue your Python journey, remember that input and output are crucial for making your programs useful and engaging. You can use these concepts to create simple text-based games, interactive calculators, and much more. In the next article of our series, we’ll explore control structures like conditional statements and loops, which will allow you to add logic and decision-making to your programs. Happy coding!