๐ Phase 1: Programming Fundamentals
๐ Topic 3: Python Input & Output
In the previous lesson, you learned about Python Operators. Now it's time to learn how Python interacts with users by taking input and displaying output.
Input and Output (I/O) are fundamental concepts because almost every real-world program accepts input, processes it, and produces meaningful output.
๐น 1. What is Input & Output?
A Python program generally follows three steps:
Input โ Process โ Output
Example:
โข User enters two numbers Input
โข Python adds them Process
โข Sum is displayed Output
๐น 2. Displaying Output
Python uses the print() function to display information on the screen.
Example
print("Hello, Data Science!")Output
Hello, Data Science!
๐น 3. Printing Variables
You can print variables along with text.
name = "Deepak"
print(name)
Output:
Deepak
Or:
name = "Deepak"
print("Welcome", name)
Output:
Welcome Deepak
๐น 4. Taking User Input
Python uses the input() function to receive input from users.
name = input("Enter your name: ")
print("Hello", name)Example Output:
Enter your name: Deepak
Hello Deepak
๐น 5. Important Note โญ
The input() function always returns a string, even if the user enters a number.
age = input("Enter age: ")
print(type(age))Output:
<class 'str'>
๐น 6. Converting Input to Integer
To perform mathematical operations, convert the input using int().
age = int(input("Enter your age: "))
print(age + 5)Example:
Enter your age: 25
30
๐น 7. Taking Decimal Input
Use float() for decimal numbers.
price = float(input("Enter price: "))
print(price)๐น 8. Taking Multiple Inputs
You can take multiple inputs in a single line.
name, city = input("Enter your name and city: ").split()
print(name)
print(city)Example Input:
Deepak Mumbai
Output:
Deepak
Mumbai
๐น 9. Formatting Output
Using f-Strings โญ Recommended
name = "Deepak"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Deepak and I am 25 years old.
Using .format()
name = "Deepak"
print("Welcome {}".format(name))
๐น 10. Example Program
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}")
print(f"Next year you will be {age + 1} years old.")Example Output:
Enter your name: Deepak
Enter your age: 25
Hello Deepak
Next year you will be 26 years old.
๐น 11. Common Mistake
num1 = input("Enter first number: ")
num2 = input("Enter second number: ")
print(num1 + num2)Input:
10
20
Output:
1020
Why?
Because both values are strings.
Correct way: