Features of Python

Module 1 β€’ Session 01 β€’ Allocation: 1 Contact Hr

Bytecode & PVM Dynamic Typing Garbage Collection Multi-Paradigm

Created by Guido van Rossum in 1991, Python was designed around a core philosophy: developer productivity, code readability, and structural simplicity. The language principles are codified in The Zen of Python (import this), highlighting core tenets like "Readability counts" and "Simple is better than complex."

1. Simple, Readable, and Expressive Syntax

Python mimics natural English structure and eliminates much of the boilerplate syntactic clutter found in C, C++, or Java (such as curly braces {} or trailing semicolons ;). Instead, Python uses whitespace indentation to enforce clean block structures.

# Swapping two variables in C/Java requires a temp variable: # int temp = a; a = b; b = temp; # Expressive Python tuple unpacking accomplishes this in a single line: a = 10 b = 20 a, b = b, a print(f"Swapped values -> a: {a}, b: {b}") # Output: a: 20, b: 10

2. Interpreted Language Model (Bytecode + PVM)

A common misconception is that Python is purely interpreted without compilation. In reality, reference Python (CPython) uses a two-stage execution architecture:

  1. Compilation Stage: Python compiles human-readable .py source code into intermediate, platform-independent Bytecode (.pyc files stored in __pycache__).
  2. Execution Stage: The Python Virtual Machine (PVM) interprets bytecode instructions and executes them on the target CPU.
import dis def add_numbers(x, y): return x + y # Disassemble the function into internal Python bytecode instructions: dis.dis(add_numbers) """ Output Bytecode Instructions: LOAD_FAST 0 (x) LOAD_FAST 1 (y) BINARY_OP 0 (+) RETURN_VALUE """
Architectural Benefit: Bytecode compilation enables platform portability. You can write Python code on Windows and run the compiled bytecode on Linux or macOS without modification.

3. Dynamic Typing and "Duck Typing"

Python is dynamically typedβ€”variable data types are resolved at runtime rather than compile-time. You do not explicitly declare variable types. Variables in Python are merely references (pointers) to typed objects held in memory.

Python also leverages Duck Typing: "If it walks like a duck and quacks like a duck, it's a duck." Method availability on an object matters more than its explicit class hierarchy.

# Dynamic typing allows a single variable to re-point to different object types: data = 100 # data references an int object print(type(data)) # <class 'int'> data = "Compillo" # data now references a str object print(type(data)) # <class 'str'> # Duck Typing Demonstration: class Duck: def speak(self): return "Quack!" class Person: def speak(self): return "Hello!" def make_it_speak(entity): print(entity.speak()) # Does not care about entity type, only if speak() exists! make_it_speak(Duck()) # Output: Quack! make_it_speak(Person()) # Output: Hello!

4. Automatic Memory Management & Garbage Collection

Python developers do not manually allocate (e.g., malloc()) or deallocate (e.g., free()) memory. CPython manages memory using two core engines:

  • Reference Counting: Every object maintains a counter tracking how many references point to it. When the count drops to zero, Python immediately frees its memory.
  • Cyclic Garbage Collector: Periodically detects and cleans up circular reference loops (e.g., Object A points to Object B, and Object B points back to Object A) that reference counting alone cannot resolve.
import sys sample_list = [1, 2, 3] # Check the reference count for the list object (getrefcount adds 1 temporary ref): print(f"Reference count: {sys.getrefcount(sample_list) - 1}") # Output: 1 ref_copy = sample_list print(f"Reference count after aliasing: {sys.getrefcount(sample_list) - 1}") # Output: 2

5. Multi-Paradigm Flexibility

Python supports multiple programming styles without forcing a single strict paradigm:

  • Procedural: Organize code into sequential functions and modules.
  • Object-Oriented (OOP): Model real-world entities using classes, inheritance, encapsulation, and polymorphism.
  • Functional: Treat calculations as evaluations of mathematical functions using higher-order functions (map, filter, reduce, and lambda expressions).
numbers = [1, 2, 3, 4, 5, 6] # Functional paradigm: map and filter with lambda evens_squared = list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, numbers))) print(f"Functional output: {evens_squared}") # Output: [4, 16, 36]

6. Batteries Included & C/C++ Extensibility

Python features a vast Standard Library out of the box (covering networking, math, file formats, threading, cryptography, and OS interaction). Furthermore, performance-critical bottlenecks can be offloaded to C/C++ modules using extensions or libraries like NumPy, Cython, or ctypes.

πŸ‹οΈ Try It Yourself: Practice Challenges

Challenge 1: Variable Type Inspection

Use type() and id() to inspect variables of integer, float, string, and list types. Observe how reassigning a variable changes its underlying memory reference ID.

Challenge 2: Interactive Swapper

Use input() to take two strings from the console and swap their positions on a single line using tuple unpacking.

Challenge 3: Reference Counter Check

Import sys, assign a dictionary to a variable, alias it twice, and print its reference count at each step.

Challenge 4: Functional Transformer

Convert a list of temperature values in Celsius to Fahrenheit using a functional approach with map() and lambda.

⚑ Interactive Sandbox (Supports Python input() Prompts)
Console Output:
Click "Run Code" above to execute interactive input scripts...

πŸ“ Knowledge Check Quiz

1. What intermediary format does Python compile source code (.py) into before execution?
2. What mechanism allows Python to run methods on objects based on behavior rather than explicit class type?
3. How does standard CPython handle circular reference memory leaks?
Advertisement
Horizontal Ad Banner Slot (728x90 / Responsive)