PYTHON Flashcards
functions, modules, and packages (21 cards)
What is a function in Python?
Blocks of code designed to perform specific tasks and promote code reuse
Functions can be built-in or user-defined.
What keyword is used to declare a user-defined function in Python?
def
This keyword is followed by the function name and parentheses.
What are parameters in Python functions?
Variables that accept values when a function is called
Type hints are available in Python 3.5+.
What are arguments in the context of Python functions?
Values passed inside the parentheses of the function
They provide input to the function.
What are default arguments in Python functions?
Parameters with default values
They allow functions to be called with fewer arguments.
What are keyword arguments?
Arguments passed with specified names
They enhance readability by explicitly stating parameter names.
What are positional arguments?
Arguments passed in a specific order
The order of arguments must match the function’s parameter order.
What are arbitrary arguments in Python?
Arguments that allow a variable number of inputs
*args for non-keyword arguments and **kwargs for keyword arguments.
What are docstrings?
Strings used to describe the functionality of a function
They are considered good practice for documentation.
What are nested functions?
Functions defined inside other functions
They can access variables from the enclosing scope.
What are anonymous functions in Python?
Functions created using the lambda keyword
They are typically used for short, throwaway functions.
What is a recursive function?
A function that calls itself
It requires a careful exit strategy to avoid infinite recursion.
What is the purpose of the return statement in a function?
Exits a function and returns a value to the caller
If no expression is specified, it returns None.
What does ‘pass by reference’ mean in Python?
Variable names are references; a new reference is created when passed to a function
Reassigning the reference inside the function breaks the connection with the original object.
What is a module in Python?
A Python file (.py) containing functions and global variables
Modules are used for code organization.
What is the purpose of importing a module?
To use functions and variables defined in that module
Use the statement import module_name.
What is a package in Python?
A directory containing a collection of modules and an __init__.py file
Packages are used for code distribution and reuse.
How do you import a specific module from a package?
Use the import package_name.module_name statement
This allows access to the specified module within the package.
What is a library in Python?
A collection of related code functionalities that can be imported into a program
Libraries may consist of a collection of packages.
What is the difference between a module and a package?
A module is a single file; a package is a directory containing modules and an __init__.py file
This distinction helps organize code effectively.
What is the difference between a package and a library?
A package is a collection of modules; a library is a broader term for a collection of packages and modules
Libraries offer related functionalities.