In Python, input/output (I/O) refers to the way a program interacts with the user or external resources like files. It includes receiving data (input) from users, files, or other sources and displaying or writing data (output) to the screen or other external destinations.
1. Input in Python:
The input()
function allows you to capture data entered by the user from the keyboard. The data entered is always returned as a string, so you might need to convert it to other types, such as int
or float
.
Example:
firstName = input("Enter your first name: ") # Takes user input as a string
print("Hello, " + firstName ) # Output: Hello,
Converting Input Data: If the user enters a number, you can convert it to an integer or float:
age = int(input("Enter your age: ")) # Convert input to an integer
print("You are", age, "years old.")
2. Output in Python:
Python uses the print()
function to display output to the console. You can display strings, variables, and even formatted text.
Example
print("Hello, Friends!") # Output: Hello, Friends!
Using Variables:
name = "John"
age = 35
print("Name:", name, "Age:", age) # Output: Name: John Age: 35
Formatted Strings: You can use formatted strings (f-strings) to create more complex output.
name = "John"
age = 35
print(f"My name is {name} and I am {age} years old.")
# Output: My name is John and I am 35 years old.