Basic Input and Output in Python 🖥️

Python provides simple ways to display output to users and take input from them using print() and input() functions.
1️⃣ Output: print() Function 🖨️
The print() function is used to display messages, numbers, and variables.
🔹 Printing Strings
print("Hello, World!")
print("Python is awesome!")
Output:
csharp
Hello, World!
Python is awesome!
🔹 Printing Variables
name = "Alice"
age = 25
print(name) # Output: Alice
print(age) # Output: 25
🔹 Printing Multiple Values
Use commas , to print multiple values.
name = "Bob"
age = 30
print("Name:", name, "Age:", age)
Output:
Name: Bob Age: 30
🔹 Using sep and end Parameters
sep (separator) defines what separates values.
end defines what comes at the end of the output (default is newline \n).
print("Python", "Java", "C++", sep=" | ") # Custom separator
print("Hello", end=" ")
print("World!") # No new line between prints
Output:
Python | Java | C++
Hello World!
2️⃣ Input: input() Function ⌨️
The input() function allows users to enter data.
🔹 Basic Input
name = input("Enter your name: ")
print("Hello,", name)
Example Interaction:
Enter your name: Alice
Hello, Alice
🔹 Taking Numeric Input
By default, input() returns a string, so you need to convert it using int() or float().
age = input("Enter your age: ") # This stores age as a string
print(type(age)) # Output:
🔹 Converting Input to Integer or Float
age = int(input("Enter your age: ")) # Converts input to an integer
height = float(input("Enter your height in meters: ")) # Converts to float
print("Age:", age, "Height:", height)
Example Interaction:
Enter your age: 25
Enter your height in meters: 1.75
Age: 25 Height: 1.75
3️⃣ Combining Input and Output
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello, {name}! You are {age} years old.") # f-string formatting
Example Interaction:
Enter your name: John
Enter your age: 30
Hello, John! You are 30 years old.
Date: 2025-03-22 00:00:00.000000