Mastering Intermediate Python: Essential Techniques and Best Practices
Input and Output
Input and output operations are essential for interacting with users and handling data in files. Let’s explore how to read from and write to files, as well as how to get user input.
Reading from and Writing to Files
Definition: File operations allow you to store data persistently by reading from and writing to files on the disk.
Syntax:
# Opening a file
file = open("filename.txt", "mode")
# Reading from a file
content = file.read()
# Writing to a file
file.write("Some text")
# Closing a file
file.close()
Modes:
'r'
: Read (default mode, file must exist)'w'
: Write (creates a new file or truncates an existing file)'a'
: Append (adds content to the end of the file)'r+'
: Read and write
Methods:
open()
: Opens a file and returns a file object.read()
: Reads the entire content of the file.readline()
: Reads one line from the file.readlines()
: Reads all lines and returns them as a list.write()
: Writes a string to the file.writelines()
: Writes a list of strings to the file.close()
: Closes the file.
Examples:
Reading from a File:
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
Writing to a File:
file = open("example.txt", "w")
file.write("Hello, World!")
file.close()
Appending to a File:
file = open("example.txt", "a")
file.write("\nAppended text.")
file.close()
Reading Line by Line:
file = open("example.txt", "r")
for line in file:
print(line.strip())
file.close()
Using with Statement:
with open("example.txt", "r") as file:
content = file.read()
print(content)
# No need to explicitly close the file
User Input
Definition: User input allows you to interact with users by getting data from them during program execution.
Syntax:
# Getting user input
user_input = input("Enter something: ")
Examples:
Simple User Input:
name = input("What is your name? ")
print(f"Hello, {name}!")
Getting Numerical Input:
age = int(input("Enter your age: "))
print(f"You are {age} years old.")
Multiple Inputs:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Name: {name}, Age: {age}")
Input Validation:
while True:
age = input("Enter your age: ")
if age.isdigit():
age = int(age)
break
else:
print("Please enter a valid number.")
print(f"You are {age} years old.")
Using Input in Calculations:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print(f"The sum is: {result}")
Basic Modules and Libraries
Modules and libraries in Python provide additional functionality that you can use in your programs. Let’s explore how to import and use modules, and get familiar with some of Python’s standard libraries like math, random, and datetime.
Importing Modules
Definition: Modules are files containing Python code that can define functions, classes, and variables. Libraries are collections of modules.
Syntax:
# Importing an entire module
import module_name
# Importing specific functions or classes from a module
from module_name import function_name, class_name
# Importing a module with an alias
import module_name as alias
Examples:
Importing an Entire Module:
import math
print(math.sqrt(16)) # Output: 4.0
Importing Specific Functions:
from math import sqrt, pi
print(sqrt(16)) # Output: 4.0
print(pi) # Output: 3.141592653589793
Importing with an Alias:
import math as m
print(m.sqrt(16)) # Output: 4.0
Using a Custom Module:
# Assuming you have a file named my_module.py with a function greet()
from my_module import greet
greet("Alice")
Importing All Functions from a Module:
from math import *
print(sqrt(16)) # Output: 4.0
print(pi) # Output: 3.141592653589793
Standard Libraries
Python’s standard libraries are collections of modules that come with Python and provide a wide range of functionalities.
math Library
Definition: Provides mathematical functions.
Common Methods:
sqrt(x)
: Returns the square root of x.pi
: The mathematical constant π.sin(x)
,cos(x)
,tan(x)
: Trigonometric functions.log(x)
: Returns the natural logarithm of x.
Examples:
import math
print(math.sqrt(25)) # Output: 5.0
print(math.pi) # Output: 3.141592653589793
print(math.sin(math.pi / 2)) # Output: 1.0
random Library
Definition: Provides functions for generating random numbers.
Common Methods:
random()
: Returns a random float between 0.0 and 1.0.randint(a, b)
: Returns a random integer between a and b.choice(seq)
: Returns a random element from the sequence seq.shuffle(seq)
: Shuffles the sequence seq in place.
Examples:
import random
print(random.random()) # Output: Random float between 0.0 and 1.0
print(random.randint(1, 10)) # Output: Random integer between 1 and 10
print(random.choice(['apple', 'banana', 'cherry'])) # Output: Random choice from the list
fruits = ['apple', 'banana', 'cherry']
random.shuffle(fruits)
print(fruits) # Output: Shuffled list
datetime Library
Definition: Provides classes for manipulating dates and times.
Common Methods:
datetime.now()
: Returns the current date and time.date(year, month, day)
: Creates a date object.time(hour, minute, second)
: Creates a time object.timedelta(days, seconds, microseconds)
: Represents a duration.
Examples:
from datetime import datetime, date, time, timedelta
# Current date and time
now = datetime.now()
print(now) # Output: Current date and time
# Specific date
my_birthday = date(1990, 5, 17)
print(my_birthday) # Output: 1990-05-17
# Specific time
lunch_time = time(12, 30)
print(lunch_time) # Output: 12:30:00
# Time difference
delta = timedelta(days=5)
print(delta) # Output: 5 days, 0:00:00
Error Handling in Python
Error handling in Python allows you to manage and respond to exceptions (errors) that occur during program execution. This ensures your program can handle unexpected situations gracefully.
Exceptions
Definition: Exceptions are errors detected during execution. They can be handled using try, except, finally, and raise statements.
Syntax:
try:
# code that might raise an exception
except ExceptionType:
# code that runs if an exception occurs
finally:
# code that runs no matter what (optional)
Methods:
try
: Block of code to test for errors.except
: Block of code to handle the error.finally
: Block of code that will always execute, regardless of whether an error occurred.raise
: Used to raise an exception manually.
Examples:
Basic Try-Except:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
Handling Multiple Exceptions:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
except TypeError:
print("Invalid type!")
Using Finally:
try:
file = open("example.txt", "r")
content = file.read()
except FileNotFoundError:
print("File not found!")
finally:
file.close()
print("File closed.")
Raising an Exception:
def check_age(age):
if age < 18:
raise ValueError("Age must be at least 18.")
return "Access granted."
try:
print(check_age(15))
except ValueError as e:
print(e)
Custom Exception Handling:
class CustomError(Exception):
pass
try:
raise CustomError("This is a custom error.")
except CustomError as e:
print(e)
Detailed Examples:
Basic Try-Except with User Input:
try:
number = int(input("Enter a number: "))
print(f"The number is {number}")
except ValueError:
print("That's not a valid number!")
Handling Multiple Exceptions with User Input:
try:
number = int(input("Enter a number: "))
result = 10 / number
print(f"Result is {result}")
except ValueError:
print("That's not a valid number!")
except ZeroDivisionError:
print("Cannot divide by zero!")
Using Finally with File:
try:
file = open("example.txt", "r")
content = file.read()
print(content)
except FileNotFoundError:
print("File not found!")
finally:
print("Attempted to read the file.")
Raising an Exception (ZeroDivisionError):
def divide(a, b):
if b == 0:
raise ZeroDivisionError("Division by zero is not allowed.")
return a / b
try:
print(divide(10, 0))
except ZeroDivisionError as e:
print(e)
Custom Exception with Negative Numbers:
class NegativeNumberError(Exception):
pass
def check_positive(number):
if number < 0:
raise NegativeNumberError("Negative numbers are not allowed.")
return number
try:
print(check_positive(-5))
except NegativeNumberError as e:
print(e)
Basic Scripting and Automation
Scripting and automation can save time and effort by automating repetitive tasks. Let’s explore how to write simple scripts and handle command line arguments using the sys module.
Writing Scripts
Definition: A script is a file containing Python code that performs a specific task. Scripts can automate repetitive tasks, process data, or perform system administration tasks.
Syntax:
# A simple script to print "Hello, World!"
print("Hello, World!")
Examples:
Automating File Renaming:
import os
def rename_files(directory, prefix):
for filename in os.listdir(directory):
new_name = prefix + filename
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
rename_files("/path/to/directory", "new_")
Batch Image Resizing:
from PIL import Image
import os
def resize_images(directory, size):
for filename in os.listdir(directory):
if filename.endswith(".jpg"):
img = Image.open(os.path.join(directory, filename))
img = img.resize(size)
img.save(os.path.join(directory, "resized_" + filename))
resize_images("/path/to/images", (800, 600))
Automating Backup:
import shutil
import os
def backup_files(source_dir, backup_dir):
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
for filename in os.listdir(source_dir):
shutil.copy(os.path.join(source_dir, filename), os.path.join(backup_dir, filename))
backup_files("/path/to/source", "/path/to/backup")
Log File Analysis:
def analyze_log(file_path):
with open(file_path, "r") as file:
lines = file.readlines()
error_count = sum(1 for line in lines if "ERROR" in line)
print(f"Total errors: {error_count}")
analyze_log("/path/to/logfile.log")
Sending Automated Emails:
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to_email):
from_email = "your_email@example.com"
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = from_email
msg["To"] = to_email
with smtplib.SMTP("smtp.example.com") as server:
server.login("your_username", "your_password")
server.sendmail(from_email, to_email, msg.as_string())
send_email("Test Subject", "This is a test email.", "recipient@example.com")
Command Line Arguments
Definition: Command line arguments allow you to pass information to a script from the command line. The sys module provides access to these arguments.
Syntax:
import sys
# Accessing command line arguments
args = sys.argv
Examples:
Simple Command Line Argument:
import sys
if len(sys.argv) != 2:
print("Usage: python script.py ")
sys.exit(1)
name = sys.argv[1]
print(f"Hello, {name}!")
Multiple Command Line Arguments:
import sys
if len(sys.argv) != 3:
print("Usage: python script.py ")
sys.exit(1)
num1 = int(sys.argv[1])
num2 = int(sys.argv[2])
print(f"Sum: {num1 + num2}")
File Processing with Command Line Arguments:
import sys
if len(sys.argv) != 2:
print("Usage: python script.py ")
sys.exit(1)
filename = sys.argv[1]
with open(filename, "r") as file:
content = file.read()
print(content)
Automating Tasks with Command Line Arguments:
import sys
import os
import shutil
if len(sys.argv) != 3:
print("Usage: python script.py ")
sys.exit(1)
source_dir = sys.argv[1]
backup_dir = sys.argv[2]
if not os.path.exists(backup_dir):
os.makedirs(backup_dir)
for filename in os.listdir(source_dir):
shutil.copy(os.path.join(source_dir, filename), os.path.join(backup_dir, filename))
print("Backup completed.")
Dynamic Script Behavior with Command Line Arguments:
import sys
if len(sys.argv) < 2:
print("Usage: python script.py