Python Programming Interview Questions
Python Language History, Basics & Program Structure
What is Python?
Python is a high-level, interpreted, general-purpose programming language known for its simplicity and readability. It supports multiple programming paradigms including procedural, object-oriented, and functional programming.
Who developed Python and when?
Python was created by Guido van Rossum and first released in 1991. It was named after the British comedy group Monty Python, not the snake.
What are the main features of Python?
Key features include: easy to learn and read, interpreted language, dynamically typed, object-oriented, extensive standard library, platform independent, open source, and supports automatic memory management (garbage collection).
What is the basic structure of a Python program?
A Python program typically includes: import statements, function and class definitions, global variables, and the main execution block (usually under if __name__ == "__main__":).
What is Python's Zen?
Python's Zen (PEP 20) is a collection of 19 aphorisms that capture the philosophy of Python. You can view it by typing import this in Python interpreter. Key principles include "Beautiful is better than ugly" and "Simple is better than complex."
What is the difference between Python 2 and Python 3?
Major differences include: print function (print vs print()), integer division (3/2 = 1 in Python 2, 1.5 in Python 3), Unicode support (Python 3 strings are Unicode by default), and xrange() removed in Python 3 (use range()).
What is PEP in Python?
PEP stands for Python Enhancement Proposal. It's a design document that describes new features, processes, or environment changes for Python. PEP 8 is particularly important as it defines Python's style guide.
What are Python's built-in data types?
Built-in types include: Numeric (int, float, complex), Sequence (str, list, tuple), Mapping (dict), Set (set, frozenset), Boolean (bool), and Binary (bytes, bytearray).
What is the purpose of indentation in Python?
Indentation (whitespace at the beginning of lines) is used to define code blocks in Python. Unlike other languages that use braces {}, Python uses indentation to indicate grouping of statements. Typically 4 spaces are used per indentation level.
What is the Python interpreter?
The Python interpreter reads and executes Python code line by line. It's available as CPython (standard), Jython (Java-based), IronPython (.NET-based), and PyPy (JIT compiler).
What is the purpose of __name__ in Python?
__name__ is a special variable that holds the name of the module. When a module is run directly, __name__ equals "__main__". This is used to check if a module is being run as a script or imported.
What is Python's memory management?
Python uses automatic memory management with a private heap. It employs reference counting and a cyclic garbage collector to automatically free memory. The gc module provides control over the garbage collector.
What are Python decorators?
Decorators are functions that modify the behavior of other functions or methods. They use the @decorator_name syntax and are a powerful tool for metaprogramming in Python.
What is the difference between list and tuple?
Lists are mutable (can be changed), defined with square brackets []. Tuples are immutable (cannot be changed), defined with parentheses (). Lists have more methods available than tuples.
What are Python modules and packages?
A module is a single Python file containing code. A package is a collection of modules in directories that include an __init__.py file. Packages help organize related modules.
What is the Python Standard Library?
The Python Standard Library is a collection of modules and packages that come with Python installation. It includes modules for file I/O, system calls, internet protocols, data structures, and more, providing "batteries included" functionality.
What is pip in Python?
pip (Package Installer for Python) is the standard package manager for Python. It's used to install and manage Python packages from the Python Package Index (PyPI) and other repositories.
What are Python virtual environments?
Virtual environments are isolated Python environments that allow you to manage dependencies for different projects separately. Created using venv or virtualenv, they prevent version conflicts between projects.
What is duck typing in Python?
Duck typing is a concept where an object's suitability is determined by the presence of certain methods and properties, rather than the type of the object itself. "If it looks like a duck and quacks like a duck, it must be a duck."
What are Python comprehensions?
Comprehensions provide a concise way to create sequences (lists, dictionaries, sets). List comprehension: [x**2 for x in range(10)], Dict comprehension: {x: x**2 for x in range(5)}, Set comprehension: {x for x in 'abracadabra'}.
Note: These questions cover fundamental concepts of Python programming. Understanding these basics is essential for any Python programming interview. Python's simplicity and readability make it a popular choice for beginners and experts alike.