Python / Intermediate

Once you understand the basics, it’s time to go deeper! Intermediate topics help you write cleaner, more efficient, and more powerful Python programs.

1. File Handling (Read & Write)

Python lets you easily work with files — reading and writing data.

# Write to a file
with open("example.txt", "w") as file:
    file.write("Hello, this is Python!\n")

# Read from a file
with open("example.txt", "r") as file:
    content = file.read()
    print(content)
Python

2. Modules & Packages

Modules are reusable code files. Python comes with many built-in ones, like math or random.

import math
import random

print(math.sqrt(16))     # 4.0
print(random.randint(1, 10))  # Random number between 1–10
Python

3. Error & Exception Handling

Errors happen. Exceptions let your program fail gracefully instead of crashing.

try:
    number = int("abc")  # This will raise an error
except ValueError:
    print("Oops! That was not a number.")
finally:
    print("Execution finished.")
Python

4. List Comprehensions

A shorter, cleaner way to create lists.

# Normal way
squares = []
for i in range(5):
    squares.append(i**2)

# List comprehension
squares2 = [i**2 for i in range(5)]

print(squares2)  # [0, 1, 4, 9, 16]
Python

5. Working with Libraries in Python

Python has thousands of external libraries. For example, requests helps you fetch data from the internet.

import requests

response = requests.get("https://api.github.com")
print(response.status_code)  # 200 means OK
Python

6. Object-Oriented Programming Basics

OOP organizes code into classes and objects.

class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print(f"{self.name} says woof!")

my_dog = Dog("Buddy")
my_dog.bark()   # Buddy says woof!
Python

7. Classes and Objects in Python

Objects are instances of classes. You can give them properties and methods.

class Car:
    def __init__(self, brand, year):
        self.brand = brand
        self.year = year

    def info(self):
        return f"{self.brand} - {self.year}"

car1 = Car("Tesla", 2023)
print(car1.info())  # Tesla - 2023
Python

8. Virtual Environments in Python

Virtual environments help manage dependencies for different projects.

# Create a virtual environment
python -m venv myenv

# Activate (Windows)
myenv\Scripts\activate

# Activate (Mac/Linux)
source myenv/bin/activate
Python

👉 At this level, you will begin to use Python in a more professional manner. File management, error handling, classes, and libraries form the foundation of real projects.

Leave a Reply

Your email address will not be published. Required fields are marked *