02-03: Input and Output¶
input() Function¶
input() is used to take input from the user through the keyboard.
- It pauses the program and waits for user input
- Whatever the user types is returned as a string (str)
- Optional message (prompt) can be shown to guide the user
✅ Syntax
✅ Example
👉 Sample run:
💡 Explanation - The program displays → Enter your name: - The user types → Imran - name stores "Imran" (string) - print() displays the result
⚠️ Important Note
- Even if the user enters 25, Python stores it as: "25" # string, not integer
📌 Taking Multiple Inputs¶
🔹 Simple Way
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum =", a + b)
👉 Sample run:
🔹 Multiple Inputs in One Line
👉 Sample run:
Type Conversion (Casting)¶
🔹 Why Type Conversion is Needed?¶
Since input() returns a string, mathematical operations will fail without conversion.
❌ Example (Error)
👉 Error because: - "num" is string - 5 is integer
✅ Correct Approach
👉 Sample run:
🔹 Common Conversion Functions¶
| Function | Description | Example |
|---|---|---|
int() |
Converts to integer | int("10") → 10 |
float() |
Converts to decimal | float("3.5") → 3.5 |
str() |
Converts to string | str(100) → "100" |
🔹 Float Example
👉 Sample run:
🔹 String Conversion Example
👉 Output:
print() Function¶
print() is used to display output on the screen
- It can print text, numbers, variables, and expressions
- It automatically converts values to string when displaying
✅ Basic Usage
👉 Output:
🔹 Printing Multiple Values
👉 Output:
⚙️ Important Parameters of print()¶
🔹 1. sep (Separator)¶
Defines how multiple values are separated
👉 Output:
🔹 2. end (Ending Character)¶
Controls what is printed at the end
👉 Output:
🔹 Default Behavior
👉 Output:
👉 Because default end = "\n" (new line)
📌 Output Formatting¶
Formatting helps display output in a clear and readable way
🔹 Method 1: Using Comma ,¶
👉 Output:
👉 Simple and beginner-friendly
🔹 Method 2: Using + (Concatenation)¶
👉 Output:
⚠️ Only works with strings
👉 Output:
🔹 Method 3: format() Method¶
👉 Output:
🔹 Method 4: f-Strings (Best ⭐)¶
Introduced in Python 3.6
👉 Output:
f-strings are powerful, because: - Clean and readable - Can include variables + expressions
👉 Output:
Formatting Numbers using f-Strings¶
🔹 Basic Syntax¶
🔹 1.1 Decimal Precision¶
Example:
👉 Output:
💡 Explanation
.2f → 2 decimal places
f → float
Examples
👉 Output:
🔹 1.2 Width & Alignment¶
👉 Output:
Alignment Types¶
print('*****')
print(f"{25:<5}") # Left
print(f"{25:>5}") # Right
print(f"{25:^5}") # Center
print('*****')
👉 Output:
🔹 1.3 Zero Padding¶
👉 Output:
🔹 1.4 Comma Separator¶
👉 Output:
🔹 1.5 Percentage Formatting¶
👉 Output:
🔹 1.6 Scientific Notation¶
👉 Output:
🔹 1.7 Combining Everything¶
👉 Output:
Combined Example (Real Use)¶
name = input("Enter your name: ")
age = int(input("Enter your age: "))
height = float(input("Enter your height: "))
print(f"\n--- User Information ---")
print(f"Name : {name}")
print(f"Age : {age}")
print(f"Height : {height:.2f}")
print(f"In 5 years, age will be {age + 5}")
👉 Sample run:
Enter your name: Imran
Enter your age: 25
Enter your height: 5.9
--- User Information ---
Name : Imran
Age : 25
Height : 5.90
In 5 years, age will be 30
Quick Summary¶
- input() → takes input (always string)
- print() → displays output
- sep → controls separator
- end → controls line ending
- int(), float(), str() → type conversion
- f-strings → best for formatting
Practice Problems¶
# Problem 1
name = input("Enter name: ")
print(f"Welcome {name}")
# Problem 2
num = int(input("Enter a number: "))
print(f"Square = {num * num}")
# Problem 3
a, b = map(int, input("Enter two numbers: ").split())
print(f"Sum = {a + b}, Difference = {a - b}")
👉 Sample run:
Enter name: Imran
Welcome Imran
Enter a number: 6
Square = 36
Enter two numbers: 10 4
Sum = 14, Difference = 6
---¶
Exercises: 02-03: Exercises — Input and Output
⬅️ Previous: 02-02: Variables and Data Types ➡️ Next: 02-04: Operators