Python and R Programming
Generators, Decorators, Iteration and Regular Expressions
PGCP-BDA
iterator protocol
An iterator supplies next and raises StopIteration when exhausted; iter() obtains an iterator from an iterable.
iterable
A Python object capable of returning an iterator, usually through iter, so its elements can be consumed one at a time.
generator
A generator is a resumable iterator produced by a function containing yield, preserving its local execution state between values.
yield
A statement that suspends a generator function, emits one value and preserves local execution state for the next request.
generator expression
A lazy parenthesized comprehension that computes each item only when an iterator requests it.
decorator
A decorator receives a function or class and replaces its binding with a wrapped or transformed object at definition time.
A decorator receives a function or class and replaces its binding with a wrapped or transformed object at definition time. Python’s runtime model matters: names refer to objects, operations are dispatched by type and mutability determines whether an operation changes an object or creates another one. Clear code preserves object boundaries, validates external input and uses exceptions to report conditions a caller can handle. A small example of decorator should be traced from object or input creation through every relevant operation and return value. Include an empty or null-like case and one invalid case so the exception or boundary behavior is visible. Production use should keep external input validation, business logic, storage and presentation in separate functions or classes.
closure
A closure is a function carrying references to bindings from an enclosing lexical scope after that scope’s call has returned.
regular expression
A regular expression describes text patterns through literals, character classes, quantifiers, groups, anchors and alternation.
A regular expression describes text patterns through literals, character classes, quantifiers, groups, anchors and alternation. A concise Python expression is useful only when its data flow remains readable. Choose the built-in type or library abstraction that matches ordering, uniqueness, lookup, numerical or tabular requirements. Observe return values and side effects and keep transformation code separate from input, storage and presentation. When using regular expression, document which object owns mutable state, which caller releases resources and which failures can propagate. Names and types should express the contract without forcing a reader to inspect every implementation detail. Automated tests should verify the public behavior and avoid depending on incidental internal ordering unless that ordering is part of the contract.
groups and substitution
Groups capture subpatterns for extraction or backreferences and substitution replaces matched text with literal or computed replacements.
Groups capture subpatterns for extraction or backreferences and substitution replaces matched text with literal or computed replacements. Python’s runtime model matters: names refer to objects, operations are dispatched by type and mutability determines whether an operation changes an object or creates another one. Clear code preserves object boundaries, validates external input and uses exceptions to report conditions a caller can handle. A small example of groups and substitution should be traced from object or input creation through every relevant operation and return value. Include an empty or null-like case and one invalid case so the exception or boundary behavior is visible. Production use should keep external input validation, business logic, storage and presentation in separate functions or classes.
lazy evaluation
Deferring computation until a value is needed, as in Python generators and R function promises.
Match Search And Findall
match checks at the beginning, search finds the first occurrence anywhere and findall returns all nonoverlapping matches or captured groups. The mechanism should be demonstrated with a small valid case, a boundary case and an invalid case. This reveals type conversions, empty inputs, missing values and exception behavior before the same code is placed in an AI data pipeline or web service. The practical value of match search and findall appears when the program changes. A sound design permits one behavior to be replaced or extended without duplicating validation and cleanup code. Logging should identify the operation and outcome without exposing credentials or personal data and concurrent use must be supported explicitly rather than assumed from a successful single-threaded example.
Duck Typing
Duck typing relies on supported behavior rather than declared ancestry and reports missing protocol operations at runtime. The mechanism should be demonstrated with a small valid case, a boundary case and an invalid case. This reveals type conversions, empty inputs, missing values and exception behavior before the same code is placed in an AI data pipeline or web service. The practical value of duck typing appears when the program changes. A sound design permits one behavior to be replaced or extended without duplicating validation and cleanup code. Logging should identify the operation and outcome without exposing credentials or personal data and concurrent use must be supported explicitly rather than assumed from a successful single-threaded example.
Continue learning
Related notes
Put this topic into timed practice
Open mock tests when you want full-exam pacing, or keep drilling in practice mode.