Python Technical Interview

Master the fundamentals with interactive questions and detailed explanations

📦
Modules & Packages

+

What's the difference between a module and a package in Python?

Module: A single Python file (.py) containing code that can be imported directly.

Package: A directory containing multiple modules with an __init__.py file.

# Module example import utils from utils import some_function # Package example import data_processing.cleaners from data_processing import validators

What is the purpose of __init__.py in a Python package?

The __init__.py file serves multiple purposes:

  • Marks the directory as a Python package
  • Contains initialization code that runs when the package is imported
  • Controls what gets imported with from package import *

_
Single Underscore Usage

+

What are the different uses of a single underscore (_) in Python?

The single underscore has several uses:

# 1. Throwaway variable name, _, age = ("John", "middle", 25) # 2. Internal use convention class MyClass: def _internal_method(self): pass # 3. Last expression in REPL >>> 5 + 3 8 >>> _ 8 # 4. Internationalization from gettext import gettext as _ print(_("Hello")) # 5. Number separator (Python 3.6+) million = 1_000_000

âš¡
Static vs Class Methods

+

Explain the difference between @staticmethod and @classmethod decorators

class Person: species = "Homo sapiens" def __init__(self, name, age): self.name = name self.age = age @staticmethod def is_adult(age): """No automatic arguments""" return age >= 18 @classmethod def get_species(cls): """Receives class as first argument""" return cls.species @classmethod def from_string(cls, person_str): """Alternative constructor""" name, age = person_str.split('-') return cls(name, int(age)) # Usage Person.is_adult(25) # Static method Person.get_species() # Class method person = Person.from_string("Bob-25") # Alt constructor

📊
Python to D3.js Integration

+

How would you pass data from Python to D3.js for visualization?

Several approaches for Python to D3.js data flow:

# Python - Generate JSON import json import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bob', 'Charlie'], 'value': [30, 45, 25] }) # Save as JSON with open('data.json', 'w') as f: json.dump(df.to_dict('records'), f) # Flask API endpoint @app.route('/api/data') def get_data(): return jsonify(df.to_dict('records'))
// D3.js - Load and use data d3.json("data.json").then(function(data) { // Create visualization d3.select("svg") .selectAll("rect") .data(data) .enter() .append("rect") .attr("height", d => d.value); });

🔄
Django Middleware

+

What is the purpose of middleware in Django?

Middleware are processing layers that sit between the web server and Django application, handling requests and responses globally.

# Custom middleware example class CustomMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # Process request before view print(f"Request: {request.path}") response = self.get_response(request) # Process response after view print(f"Response: {response.status_code}") return response

Common uses: Authentication, CORS, logging, caching, security headers