Overview
This lecture covers essential Python interview questions spanning beginner to advanced levels, with explanations of key concepts, syntax, and differences between major Python features.
Python Basics & Concepts
- Python is a popular, flexible, and widely used programming language for both beginners and experts.
- A module is a single .py file with related functions, classes, or variables.
- A package is a directory of modules with an
__init__.py file for organization.
- Python is primarily an interpreted language, compiling source code to bytecode before execution.
- Benefits of Python: simplicity, versatility, rich libraries (Django, pandas, TensorFlow), community support, portability, development speed, dynamic typing, open-source.
- Global attributes are public (global scope), protected have a single underscore (accessible, but should be avoided outside class), private have double underscores (not accessible outside the class).
- Python is case sensitive.
Data Structures & Control Flow
- Lists are mutable, use square brackets, allow modification; tuples are immutable, use parentheses, are more memory-efficient.
- Slicing extracts portions of sequences using
[start:stop:step] syntax.
- For loops iterate over collections; while loops repeat until a condition is false.
- Indentation is required in Python to define code blocks and improve readability.
break exits a loop; continue skips to the next iteration; pass is a placeholder.
- Sets are unordered, mutable collections of immutable items.
Functions, Classes & OOP
self refers to the instance of a class, not a keyword.
- Inheritance allows a child class to use or override parent class attributes and methods; supports single, multi-level, hierarchical, and multiple inheritance.
- Classes are defined using the
class keyword; methods and attributes are declared inside.
*args allows variable-length positional arguments; **kwargs for variable-length keyword arguments.***
Memory Management & Performance
- Python manages memory via reference counting and automatic garbage collection using a private heap.
- NumPy arrays are faster than lists for numerical operations due to C-based implementation.
- Shallow copy shares references; deep copy duplicates objects to avoid shared references.
Built-in Tools, Libraries & Syntax
- Common built-in modules:
os, sys, math, random, datetime, json.
range creates lists in Python 2; xrange is a generator-like for large ranges (not in Python 3).
zip aggregates elements from multiple iterables.
- Pandas: open-source library for data analysis and manipulation.
- PEP8: style guide for writing readable Python code.
Error Handling & Documentation
- Exception handling uses
try, except, and finally blocks.
- Runtime errors occur during program execution.
- Docstrings (triple-quoted) document functions and classes.
Advanced Features & Libraries
- Multi-threading uses the
threading module but is limited by the Global Interpreter Lock (GIL).
- Generators use
yield to return items lazily.
- Django and Flask are popular web frameworks; Django suits large apps, Flask for smaller, flexible projects.
- PIP is the Python package installer.
- Compatibility between Python 2 and 3 achieved using
__future__, six/future libraries, and best practices.
Operations & Other Concepts
- Modulus (%) returns remainder,
/ is floating-point division, // is floor division.
- Type conversion functions:
int(), float(), str(), list(), tuple(), set(), dict().
- A Series (Pandas) is a 1D array; a DataFrame is a 2D table.
len() returns the length of objects such as strings or lists.
Key Terms & Definitions
- Module — a single Python file containing code.
- Package — a directory with multiple modules and an
__init__.py.
- Bytecode — intermediate, low-level code executed by the Python interpreter.
- Inheritance — mechanism for a class to inherit attributes/methods from another class.
- GIL (Global Interpreter Lock) — mutex limiting thread execution to one at a time in CPython.
- PEP8 — Python style guide for readable and consistent code.
- Docstring — triple-quoted string documenting code.
- Generator — function yielding values with
yield, creating iterators.
Action Items / Next Steps
- Review Python syntax, key data structures, and OOP concepts.
- Practice writing basic and advanced Python programs.
- Explore built-in modules and popular libraries like pandas, NumPy, Django, Flask.
- Follow PEP8 for code style in practice problems.
- Prepare for common interview coding exercises (e.g., Fibonacci series, list/tuple operations).