Today

Python Lesson: Input and Output

1. Output in Python (print)

The print() function is used to display output on the screen.

Example:

print("Hello, world!")
print(10)
print(3.14)
    

This will output:

Hello, world!
10
3.14
    

2. Printing Multiple Values

You can print multiple values by separating them with commas.

name = "Alex"
age = 25
print(name, age)
    

Output:

Alex 25
    

3. Input in Python (input)

The input() function allows the user to enter data. The input is always returned as a string.

name = input("Enter your name: ")
print("Hello", name)
    

4. Converting Input Data Types

Since input() returns a string, you must convert it when working with numbers.

age = int(input("Enter your age: "))
print("Next year you will be", age + 1)
    
⚠️ If you do not convert the input, Python will raise an error.

5. String Formatting with f-strings

f-strings allow you to easily insert variables into text.

name = "Maria"
score = 90
print(f"{name} scored {score} points")
    

Output:

Maria scored 90 points
    

6. Combining Input and Output

city = input("Enter your city: ")
year = int(input("Enter the year you were born: "))
age = 2026 - year

print(f"You live in {city} and you are {age} years old.")
    

7. Common Mistakes

Practice Exercises

  1. Ask the user for their favorite food and print it.
  2. Ask for two numbers and print their sum.
  3. Ask for a name and age and print a sentence using an f-string.
Quiz