Python Lesson 6: Input and Output

🐍 Python CourseLesson 6 of 26 · 23% complete

Real programs talk to users. input() gets data from the user, print() shows data to the user.

Getting Input

name = input("What is your name? ")
print("Hello,", name)

# When you run this:
# What is your name? Alice
# Hello, Alice

Important: input() always returns a string, even if the user types a number.

Input + Type Conversion

# Get a number from user
age = int(input("Enter your age: "))
print("Next year you will be", age + 1)

# Float input
height = float(input("Enter your height in meters: "))
print("Your height:", height)

f-strings — Best Way to Format Output

name = "Alice"
age = 25
score = 98.5

# f-string (Python 3.6+) — put variable in curly braces
print(f"Name: {name}, Age: {age}, Score: {score}")

# Format numbers
price = 9.999
print(f"Price: ${price:.2f}")   # Price: $10.00

big_num = 1000000
print(f"Count: {big_num:,}")    # Count: 1,000,000

print() Options

# No newline (stay on same line)
print("Loading", end="")
print("...", end="")
print(" Done!")
# Output: Loading... Done!

# Custom separator
print("Mon","Tue","Wed", sep=" | ")
# Output: Mon | Tue | Wed

Simple Calculator

num1 = float(input("First number: "))
num2 = float(input("Second number: "))
total = num1 + num2
print(f"{num1} + {num2} = {total}")

🏋️ Practice Task

Build a Personal Info Card. Ask user for: name, age, favorite programming language. Print a formatted card with all their info using f-strings with labels.

💡 Hint: Use three input() calls. Format with f”Name: {name} | Age: {age} | Loves: {lang}”

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *