Input and Output
Introduction to Python
Getting Started
Comments
Variables
Data Types
Type Casting
Input and Output
Operators
Strings
Booleans
Conditional Statements
While Loop
For Loop
Loop Control Statements
Lists
Tuples
Sets
Dictionaries
Functions
Function Arguments and Return Values
Variable Scope
Recursion
Lambda Functions
Arrays and Python Lists
Modules
Dates and Math
String Formatting
File Handling
Try and Except
Classes and Objects
Inheritance
Iterators
JSON
RegEx
Input and Output
Definition: Input and Output (I/O) are the ways a program communicates with the user. Python uses the input() function to receive data from the user and the print() function to display data on the screen.
Why: Learning I/O is essential because it allows you to create interactive programs that can accept user information and provide results or feedback.
Syntax
variable = input("Prompt Message")- To get user input.print(data_to_show)- To show output.
Example
name = input("Enter your name: ")
print("Hello,", name)
Explanation
- The
input()function pauses the program and waits for the user to type something. - The text inside the parentheses
"Enter your name: "is shown as a prompt to the user. - The
print()function takes the value stored in the variable and displays it along with the text "Hello,".
Key Notes
- The
input()function always reads data as **text (string)**. - If you want the user to enter a number for calculations, you must convert it using
int()orfloat()(e.g.,age = int(input("Enter age: "))). - You can print multiple items at once by separating them with a comma inside the
print()function.
Example
Executing...
❌ Error:
✅ Output:
// Click Run ▶ to execute
🏋️ Test Yourself With Exercises
Take our quiz on Input and Output to test your knowledge.
Exercise »