Python Tutorial
Python Flow Control
Python Functions
Python Data Types
Python Date and Time
Python Files
Python String
Python List
Python Dictionary
Python Variable
Python Input/Output
Python Exceptions
Python Advanced
In this Python Input and Output tutorial, we'll cover the basics of reading and writing data in Python. We'll focus on the following topics:
The print()
function in Python is used to output data to the console. By default, it prints the data followed by a newline character.
Syntax:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Example:
print("Hello, World!")
Output:
Hello, World!
The input()
function in Python is used to read a line of text from the user (input from the console). It returns the input as a string.
Syntax:
input(prompt)
Example:
name = input("What's your name? ") print(f"Hello, {name}!")
Output:
What's your name? John Hello, John!
To work with files, we need to open them using the open()
function, which returns a file object. The most common modes are:
'r'
: Read (default)'w'
: Write (truncate the file first)'a'
: Append'x'
: Create (if the file doesn't exist)with open('file.txt', 'r') as file: content = file.read() print(content)
with open('file.txt', 'w') as file: file.write('Hello, World!')
with open('file.txt', 'a') as file: file.write('\nThis line is appended to the file.')
Reading:
with open('file.txt', 'r') as file: for line in file: print(line.strip())
Writing:
lines = ['Line 1', 'Line 2', 'Line 3'] with open('file.txt', 'w') as file: for line in lines: file.write(f'{line}\n')
That's it! You now have a basic understanding of how to handle input and output operations in Python. Keep practicing and exploring more advanced features to improve your skills.
How to take user input in Python:
input()
function. It reads a line from the user and returns it as a string.user_input = input("Enter something: ") print("You entered:", user_input)
Reading and writing files in Python:
open()
with mode 'r'
) and writing (open()
with mode 'w'
) files.with open("example.txt", "r") as file: content = file.read() print(content)
with open("example.txt", "w") as file: file.write("Hello, File!")
Formatted output in Python:
format()
method for formatted output in Python.name = "Alice" age = 25 print(f"Name: {name}, Age: {age}")
format()
method):name = "Bob" age = 30 print("Name: {}, Age: {}".format(name, age))
Using input() function in Python:
input()
function is used to get user input in Python. It returns a string, and you can convert it to the desired data type if needed.age_str = input("Enter your age: ") age = int(age_str) print(f"Your age is: {age}")
File handling in Python for input and output:
open()
function.with open("example.txt", "r") as file: content = file.read() print(content) with open("output.txt", "w") as file: file.write("Writing to a file!")
Standard input and output in Python:
stdin
) and output (stdout
) are commonly used for reading from the keyboard and printing to the console.user_input = input("Enter something: ") print("You entered:", user_input)
Reading and writing to text files in Python:
open()
function with appropriate modes ('r' for reading, 'w' for writing).with open("example.txt", "r") as file: content = file.read() print(content) with open("output.txt", "w") as file: file.write("Writing to a text file!")
Python input validation and error handling:
try-except
blocks helps manage unexpected situations.while True: try: age = int(input("Enter your age: ")) break except ValueError: print("Invalid input. Please enter a valid number.") print(f"Your age is: {age}")