Python Input and Output Operations
In the previous lesson, we learned about Variables and Data Types in Python. Today, we will learn how Python accepts data from users and displays information on the screen using Input and Output operations.
Input and Output operations are very important because they allow programs to interact with users.
What is Output in Python?
Output means displaying information to the user.
Python uses the "print()" function to display output on the screen.
Example
print("Welcome to Python Programming")
Output
Welcome to Python Programming
Printing Variables
The "print()" function can also display values stored in variables.
Example
name = "Augustine"
age = 21
print(name)
print(age)
Output
Augustine
21
Printing Multiple Values
Python allows printing multiple values in one line.
Example
name = "Augustine"
course = "Python"
print("Student:", name, "Course:", course)
Output
Student: Augustine Course:
Python
What is Input in Python?
Input means receiving information from the user.
Python uses the "input()" function to collect user input.
Example
name = input("Enter your name: ")
print("Welcome", name)
Sample Output
Enter your name: Augustine
Welcome Augustine
Important Note About input()
The "input()" function stores data as a String by default.
Example
age = input("Enter your age: ")
print(type(age))
Output
<class 'str'>
Even if the user enters a number, Python treats it as text unless it is converted.
Converting User Input
We can convert input data into other data types using:
- "int()"
- "float()"
- "str()"
Example
age = int(input("Enter your age: "))
print(age)
print(type(age))
Output
21
<class 'int'>
Simple Addition Program
Example
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Total =", sum)
Sample Output
Enter first number: 10
Enter second number: 20
Total = 30
Escape Characters in Python
Escape characters are special characters used inside strings.
Escape Character| Meaning
"\n"| New Line
"\t"| Tab Space
"\""| Double Quotes
"\"| Backslash
Example
print("Python\nProgramming")
Output
Python
Programming
Comments in Python
Comments are used to explain code and improve readability.
Single-Line Comment
# This is a comment
print("Python")
Multi-Line Comment
"""
This is a
multi-line comment
"""
Why Input and Output are Important
Input and Output operations help programs:
- Interact with users
- Accept real-time data
- Display results
- Create interactive applications
- Perform calculations based on user input
Without Input and Output, programs would not communicate with users effectively.
Conclusion
Input and Output operations are essential in Python programming. The "input()" function allows users to provide information, while the "print()" function displays results on the screen.
Understanding these concepts is important because almost every Python application uses Input and Output operations.
Practice creating programs that accept user data and display meaningful results.
Top comments (0)