Python and R Programming
Functions, Arguments, Scope, Lambdas and Functional Tools
PGCP-BDA
function definition
def creates a function object and binds its name when execution reaches the definition statement.
def creates a function object and binds its name when execution reaches the definition statement. 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 function definition 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.
return value
return ends a function and supplies one object to the caller; falling off the function returns None.
return ends a function and supplies one object to the caller; falling off the function returns None. 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 return value, 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.
positional and keyword arguments
Positional arguments bind by order, keyword arguments bind by parameter name and a call cannot assign one parameter twice.
Positional arguments bind by order, keyword arguments bind by parameter name and a call cannot assign one parameter twice. 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 positional and keyword arguments 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.
default arguments
Default argument expressions are evaluated once when def executes, so mutable defaults persist across calls unless avoided deliberately.
Default argument expressions are evaluated once when def executes, so mutable defaults persist across calls unless avoided deliberately. 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 default arguments 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.
variable-length arguments
A star parameter gathers extra positional arguments into a tuple, while a double-star parameter gathers extra keyword arguments into a dictionary.
A star parameter gathers extra positional arguments into a tuple, while a double-star parameter gathers extra keyword arguments into a dictionary. 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 variable-length arguments, 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.
local global and nonlocal scope
Assignment normally creates a local name; global targets module scope and nonlocal targets an existing enclosing-function binding.
Assignment normally creates a local name; global targets module scope and nonlocal targets an existing enclosing-function binding. 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 local global and nonlocal scope 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.
first-class function
Functions are objects that can be stored, passed, returned, decorated and called through any reference.
Functions are objects that can be stored, passed, returned, decorated and called through any reference. 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 first-class function 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.
lambda
A lambda creates an anonymous function containing one expression and follows the same late-binding closure rules as def.
A lambda creates an anonymous function containing one expression and follows the same late-binding closure rules as def. 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 lambda 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.
map filter and reduce
map transforms iterable items, filter retains truthy predicate results and reduce cumulatively combines values through a binary function.
map transforms iterable items, filter retains truthy predicate results and reduce cumulatively combines values through a binary function. 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 map filter and reduce, 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.
closure
A closure is a function carrying references to bindings from an enclosing lexical scope after that scope’s call has returned.
A closure is a function carrying references to bindings from an enclosing lexical scope after that scope’s call has returned. 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 closure 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.
Higher-Order Function
A higher-order function receives functions as arguments, returns them or both. 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 higher-order function 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.
Raw String
A raw string literal suppresses most Python backslash escaping, making regular-expression patterns easier to read while retaining regex escapes. 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 raw string, 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.
Type Dir And Help
type reports an object’s class, dir lists discoverable attributes and help renders documentation and signatures available through introspection. 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 type dir and help, 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.
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.