IT 243

Programming with Python

TU BIM · Semester 5 · BIM curriculum effective from 2021

Requirement
required
Credits
3
Study Guide
8 units available

Chapter notes

Authoritative study guide across 8 syllabus units

Open a unit to read its study notes.

Unit 1: Introduction to Python· 3 LHs· ~4 marks

Pass path: Learn Python indentation rules, difference between == and is, dynamic typing, and venv setup. Top path: Explain CPython memory management with PyObject headers, reference counting, and Python byte-code compilation (.pyc).

Part 1: Core Concepts (2 Marks)

★ Dynamic Typing in Python: Variables do not have declared data types; types are bound dynamically to objects in heap memory at runtime, allowing variable names to bind to different object types across execution. ★ Identity (is) vs Equality (==): == checks value equality (whether values of two objects are equivalent via __eq__); is checks memory identity (whether both variables reference the exact same memory address via id(a) == id(b)). ★ Virtual Environment (venv): An isolated directory tree containing a specific Python interpreter and independent set of installed libraries, preventing version conflicts between project dependencies.

Part 2: Structural Frameworks (10 Marks)

Python Architecture, Dynamic Typing, and the id() Function

  • How Python Executes Code:
    1. Source Code (.py) is parsed by CPython into an Abstract Syntax Tree (AST).
    2. AST is compiled into platform-independent Python Bytecode (.pyc files stored in __pycache__).
    3. Python Virtual Machine (PVM) interprets bytecode into CPU machine instructions.
  • Memory Identity and Integer Interning:
# Variables are references to objects in memory
x = 256
y = 256
print(x == y)  # True (same value)
print(x is y)  # True (small integer interning: -5 to 256 share memory)

a = 1000
b = 1000
print(a == b)  # True (same value)
print(a is b)  # False in REPL (distinct memory objects allocated)
print(id(a), id(b))
  • Operators Summary:
    • Identity: is, is not
    • Membership: in, not in
    • Floor Division: // (truncates towards negative infinity)
    • Exponentiation: **

Part 3: Distinction Tables

Criteria Identity Operator (is) Equality Operator (==)
Comparison Target Checks physical memory address (id(a) == id(b)) Checks data contents/values (a.__eq__(b))
Overloadable No; identity check cannot be overridden by user classes Yes; custom classes override __eq__ method
Best Practice Use Comparing singletons (e.g. if val is None:) Comparing data values (strings, numbers, lists)
Speed Extremely fast (single pointer address comparison) Can be slow (recursively checks collection items)

Part 4: Worked Problems & Exam Traps

  • Easy: What does the id() function return in Python?
    • Answer: An integer representing the unique identity of an object, corresponding to its actual memory address in CPython.
  • Exam-level: Explain why a = [1, 2]; b = [1, 2]; a == b evaluates to True while a is b evaluates to False.
    • Answer: a == b compares the contents of the lists, which are identical [1, 2]. However, lists are mutable objects, so Python allocates two distinct heap memory buffers; hence their memory addresses differ (id(a) != id(b)), making a is b false.
  • Twist variant: What does 5 // -2 evaluate to in Python?
    • Answer: -3 (NOT -2), because Python’s floor division rounds towards negative infinity (lfloor−2.5rfloor=−3\\lfloor -2.5 \\rfloor = -3).

Students lose marks here because they use is to compare string values (e.g. if name is "Ram":), which fails when string interning boundaries are exceeded; value comparison must always use ==.