Basic Input and Output

Basic input and output (I/O) in Python involves using the print() function to display output on the console and the input() function to prompt the user for input.


Output using print():


Example:-

# Displaying output

print("Hello, World!")  # Output: Hello, World!

name = "Alice"

age = 25

print("My name is", name, "and I am", age, "years old.")  # Output: My name is Alice and I am 25 years old.


Input from the user using input():


Example:-

# Taking input from the user

name = input("Enter your name: ")

print("Hello,", name)  # Output: Hello, [user's name]


age = input("Enter your age: ")

print("You are", age, "years old.")  # Output: You are [user's age] years old.


Note: The input() function always returns a string, even if the user enters a numeric value. If you want to perform numerical operations with the input, you may need to convert the string to the appropriate data type using functions like 'int()' or 'float()' or 'str()'.


age = int(input("Enter your age: "))

name= str(input("Enter your name: ")) 


next_year = age + 1

print(name) # Output: [name]

print("Next year, you will be", next_year, "years old.")  # Output: Next year, you will be [user's age + 1] years old.


Understanding basic input and output allows you to create interactive programs that can accept user input and provide relevant output during runtime.