In the previous post, we learned about Python data types and how different kinds of data are stored. Now, let's learn how to interact with users by taking input and displaying output.
📖 Phase 1: Programming Fundamentals
📌 Topic 6: Input & Output
Every program performs two basic operations:
• Input – Receiving data from the user.
• Output – Displaying information to the user.
For example, when you enter your username and password on a website, that's input. When the website displays "Login Successful," that's output.
Output in Python
Python uses the print() function to display output on the screen.
Example:
print("Hello, World!")
Output:
Hello, World!
You can also print numbers and variables.
name = "Surya"
age = 25
print(name)
print(age)
Output:
Surya
25
Printing Multiple Values
name = "Ajay"
age = 25
print("Name:", name)
print("Age:", age)
Output:
Name: Ajay
Age: 25
Input in Python
Python uses the input() function to accept input from the user.
Example:
name = input("Enter your name: ")
print("Hello,", name)
Sample Output:
Enter your name: Deepak
Hello, Deepak
Taking Numeric Input
By default, input() returns a string.
age = input("Enter your age: ")
print(type(age)) #
To use it as a number, convert with int() or float().
age = int(input("Enter your age: "))
Example: Adding Two Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum =", sum)
Sample Output:
Enter first number: 10
Enter second number: 20
Sum = 30
Common Beginner Mistakes
❌ Forgetting that input() always returns a string.
❌ Trying to add two numbers without converting them.
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print(num1 + num2)
If user enters 10 and 20 → Output: 1020
This happens because Python joins two strings instead of adding two numbers.
Best Practices
✅ Use clear prompts while taking input.
✅ Convert numeric input using int() or float() whenever required.
✅ Use meaningful variable names.
Key Takeaways
• print() is used to display output.
• input() is used to receive input from the user.
• input() always returns a string.
• Convert user input using int() or float() for mathematical operations.
• Input and Output are the foundation of interactive Python programs.
➡️ Double Tap ❤️ For More
Post #1856
3.38K
- ❤ 22
- 👏 1