Input and Output in Python

← PrevNext →

Input and Output in Python are essential concepts that allow a program to communicate with users. Input is used to receive data from the user, while output is used to display information or results on the screen. In this tutorial you will learn how to use input and output in Python. Understand the input() and print() functions with simple examples and practical programs for beginners.

Input and output are important concepts in programming. They allow users to interact with a program.

Python provides two commonly used built-in functions for these operations:

Let's understand both functions with examples.

1. The print() Function in Python

The print() function is used to display text, numbers, variables, and other information.

Syntax:

print(value)

Example:

print("Hello Python")
print(100)
print(10 + 20)

Output:

Hello Python
100
30

You can also print multiple values in one statement.

name = "Shouvik"
age = 16

print("Name:", name, "Age:", age)

Output:

Name: Shouvik Age: 16

2. The input() Function in Python

The input() function is used to accept data from the user while a program is running.

Example:

name = input("Enter your name: ")
print("Welcome", name)

Sample Output:

Enter your name: Rahul
Welcome Rahul

In this example, the program asks the user to enter their name and displays a welcome message.

3. Understanding Input Data Types

One important thing to remember is that the input() function always returns data as a string.

For example:

age = input("Enter your age: ")
print(type(age))

If the user enters "25", the output will be:

<class 'str'>

Even though the user entered a number, Python treats it as text.

To perform mathematical calculations, you need to convert the input into the appropriate data type.

4. Type Conversion with input()

Python provides functions such as int() and float() to convert user input.

Example Using int():

age = int(input("Enter your age: "))
print("Next year your age will be:", age + 1)

Example Using float():

price = float(input("Enter product price: "))
print("Product price:", price)

5. Taking Multiple Inputs

You can accept multiple values from the user using the split() method.

Example:

name, city = input("Enter name and city: ").split()

print("Name:", name)
print("City:", city)

Sample Input:

Shouvik Delhi

Output:

Name: Shouvik
City: Delhi

6. Using Different print() Parameters

The print() function provides useful parameters such as sep and end.

The sep Parameter

It specifies the separator between multiple values.

print("Python", "Java", "C++", sep="-")

Output:

Python-Java-C++

The end Parameter

By default, print() moves to a new line after displaying output. You can change this behavior using end.

print("Hello", end=" ")
print("World")

Output:

Hello World

7. Practical Example: Simple Calculator

Let's create a basic calculator using input and output functions.

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

sum = num1 + num2

print("Addition:", sum)

This program accepts two numbers and displays their sum.

Conclusion

Input and output functions help make Python programs interactive. The input() function accepts information from users, while the print() function displays results.

Understanding these functions is important because almost every beginner-level Python application uses input and output operations. Practice with different values and simple programs to become comfortable with them.

← PreviousNext →