Knowlet

Unit IV: Python Functions and Packages


1. Python Functions

Functions are sub-programs that carry out specific tasks. They help in code reusability and making complex programs easier to manage.

  • Function Definition: Created using the def keyword followed by the function name and parentheses.
  • Function Call: Executed by using the function name followed by parentheses containing any necessary arguments.
  • Types of Functions: Includes built-in functions (like print()) and user-defined functions.

2. Function Arguments & Parameters

Arguments are values passed into a function when it is called. Parameters are the variables listed in the function definition.

  • Positional Arguments: Arguments that must be passed in the correct positional order.
  • Keyword Arguments: Arguments passed by specifying the parameter name, allowing them to be out of order.
  • Default Arguments: Parameters that take a default value if no argument is provided during the call.

3. Pass by Value vs Object Reference

Python uses a mechanism called Pass by Object Reference (also known as Pass by Assignment).

  • Immutable Objects: When you pass immutable objects (like numbers or strings), it behaves similarly to "Pass by Value" because the original object cannot be changed.
  • Mutable Objects: When you pass mutable objects (like lists), it behaves similarly to "Pass by Reference" because changes made inside the function affect the original object.

4. Recursion

Recursion is a programming technique where a function calls itself to solve a problem.

  • Base Case: The condition that stops the recursion to prevent infinite loops.
  • Recursive Case: The part where the function calls itself with a modified argument.
  • Advantages: Can make code cleaner and more elegant for problems like factorials or tree traversals.
  • Disadvantages: Higher memory usage and risk of stack overflow if the recursion depth is too high.

5. Scope and Lifetime of Variables

The scope of a variable defines where in the program it can be accessed. The lifetime is the period during which the variable exists in memory.

  • Local Scope: Variables declared inside a function are local to that function and exist only while it is running.
  • Global Scope: Variables declared outside of all functions can be accessed throughout the module.

6. Python Modules and Packages

These are organizational tools for larger Python projects.

  • Python Modules: A file containing Python code (functions, classes, variables) that can be imported into other scripts.
  • Python Packages: A directory containing multiple modules and a special __init__.py file, allowing for a hierarchical structuring of module namespaces.

Exam Tips

  • Indentation: Remember that all code inside a function must be indented.
  • Recursion Rule: Always ensure your recursive functions have a base case to avoid a RecursionError.
  • Global Keyword: To modify a global variable inside a function, you must use the global keyword.

Common Pitfalls

  • Mutable Default Arguments: Never use mutable objects (like [] or {}) as default arguments, as they are shared across all calls.
  • Scope Confusion: Accessing a local variable before it is defined within a function will cause an UnboundLocalError.

Frequently Asked Questions

Q: Is Python pass-by-value or pass-by-reference?
Neither. It is pass-by-object-reference. If the object is mutable, changes reflect outside; if immutable, they do not.

Q: What is the difference between a module and a library?
A module is a single file. A package is a collection of modules. A library is a collection of packages that provides specific functionality.

Did this resource help you study?

Share feedback or report issues to help improve this resource.