Nearby lessons

158 of 159

Python - Input and Output Statements

📌 What You Will Learn
  • Explain the purpose of input and output statements
  • Distinguish Python 2 input functions from the Python 3 input() function
  • Read integer, float and boolean input using type casting
  • Read multiple values from one line using split()
  • Evaluate expressions dynamically using eval()

Introduction

Input and Output statements are used to interact with the user.

They help us:

  • Read input from the keyboard.
  • Display output on the screen.

Reading Dynamic Input from the Keyboard

Python provides functions to read values entered by the user.

The available functions are different in Python 2 and Python 3.

Input Functions in Python 2

Python 2 provides two input functions:

  1. raw_input()
  2. input()

1. raw_input()

The raw_input() function always reads input as a string.

If another data type is required, we must convert it using type casting.

Example - raw_input()

🐍Code Cell
1x = raw_input("Enter First Number:")
2 
3print(type(x))
Output
No output captured.

Important Note

For every type of input, raw_input() always returns a string.

2. input() in Python 2

The input() function in Python 2 reads input directly in the required data type.

Type casting is generally not required.

Example - input() in Python 2

🐍Code Cell
1x = input("Enter Value:")
2 
3print(type(x))
Output
Possible Inputs

10        -> int
"durga"   -> str
10.5      -> float
True      -> bool

Python 3 input() Function

Python 3 provides only the input() function.

The raw_input() function is not available.

Python 3 input() works like Python 2 raw_input().

This means every input value is treated as a string.

Reading Integer Input

Since input() returns a string, use int() to convert it into an integer.

Example - Integer Input

🐍Code Cell
1x = int(input("Enter First Number:"))
Output
No output captured.

Reading Float Input

Use float() to convert the input into a floating-point number.

Example - Float Input

🐍Code Cell
1salary = float(input("Enter Salary:"))
Output
No output captured.

Reading Boolean Input

Be careful here: bool("False") is True, because any non-empty string is truthy. So casting the raw input with bool() does not work for a True/False choice.

To parse it correctly, compare the string instead.

Example - Boolean Input

🐍Code Cell
1married = input("Employee Married ? [True|False]:").strip().lower() == "true"
2 
3print(married)
Output
Employee Married ? [True|False]:True

True

Important Note

bool(input(...)) is a common trap: entering False would still print True, because any non-empty string is truthy.

The comparison version handles both cases correctly — entering True prints True, and entering False prints False.

Complete Example

🐍Code Cell
1eno = int(input("Enter Employee No:"))
2ename = input("Enter Employee Name:")
3esal = float(input("Enter Employee Salary:"))
4eaddr = input("Enter Employee Address:")
5married = input("Employee Married ? [True|False]:").strip().lower() == "true"
6 
7print("Please Confirm Information")
8print("Employee No :", eno)
9print("Employee Name :", ename)
10print("Employee Salary :", esal)
11print("Employee Address :", eaddr)
12print("Employee Married ? :", married)
Output
Enter Employee No:100
Enter Employee Name:Sunny
Enter Employee Salary:1000
Enter Employee Address:Mumbai
Employee Married ? [True|False]:True

Please Confirm Information

Employee No : 100
Employee Name : Sunny
Employee Salary : 1000.0
Employee Address : Mumbai
Employee Married ? : True

Reading Multiple Values in One Line

The split() method is used to read multiple values entered in a single line.

Example - Multiple Integer Values

🐍Code Cell
1a, b = [int(x) for x in input("Enter 2 numbers :").split()]
2 
3print("Product is :", a * b)
Output
Enter 2 numbers :10 20

Product is : 200

Important Note

By default, split() uses a space as the separator.

You can also specify another separator if needed.

Reading Multiple Float Values Using Comma

🐍Code Cell
1a, b, c = [float(x) for x in input("Enter 3 float numbers :").split(',')]
2 
3print("The Sum is :", a + b + c)
Output
Enter 3 float numbers :10.5,20.6,20.1

The Sum is : 51.2

eval() Function

The eval() function takes a string and evaluates it as a Python expression.

Example 1 - eval()

🐍Code Cell
1x = eval("10+20+30")
2 
3print(x)
Output
60

Example 2 - eval() with User Input

🐍Code Cell
1x = eval(input("Enter Expression:"))
2 
3print(x)
Output
Sample Input

10+2*3/4

Output

11.5

Python 2 vs Python 3 Input Functions

Python 2 Python 3
Provides raw_input() and input(). Provides only input().
raw_input() returns a string. input() returns a string.

Real World Usage

Input and Output statements are commonly used in:

  • Login systems
  • Student management systems
  • Banking applications
  • Online forms
  • Console-based applications

Important Notes

  • Python 2 provides both raw_input() and input().
  • Python 3 provides only input().
  • Python 3 input() always returns a string.
  • Use type casting such as int(), float(), and bool() to convert input into the required data type.
  • Do not use bool() to parse a True/False string — bool("False") is True. Compare the string instead.
  • split() is used to read multiple values from one line.
  • eval() evaluates a string as a Python expression.

Quick Summary

Function Purpose
raw_input() Reads input as a string (Python 2).
input() (Python 2) Reads input in the required data type.
input() (Python 3) Reads input as a string.
split() Reads multiple values from one line.
eval() Evaluates a string expression.
📝 Key Takeaways
  • Output statements display results and input statements read values from the user
  • Python 2 provided raw_input() and input(); Python 3 provides only input()
  • Python 3 input() always returns a string, so use int(), float() or bool() to cast it
  • split() reads multiple values entered on a single line
  • eval() evaluates a string as a Python expression

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8