PYTHON Flashcards

functions, modules, and packages (21 cards)

1
Q

What is a function in Python?

A

Blocks of code designed to perform specific tasks and promote code reuse

Functions can be built-in or user-defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What keyword is used to declare a user-defined function in Python?

A

def

This keyword is followed by the function name and parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are parameters in Python functions?

A

Variables that accept values when a function is called

Type hints are available in Python 3.5+.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are arguments in the context of Python functions?

A

Values passed inside the parentheses of the function

They provide input to the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are default arguments in Python functions?

A

Parameters with default values

They allow functions to be called with fewer arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are keyword arguments?

A

Arguments passed with specified names

They enhance readability by explicitly stating parameter names.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are positional arguments?

A

Arguments passed in a specific order

The order of arguments must match the function’s parameter order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are arbitrary arguments in Python?

A

Arguments that allow a variable number of inputs

*args for non-keyword arguments and **kwargs for keyword arguments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are docstrings?

A

Strings used to describe the functionality of a function

They are considered good practice for documentation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are nested functions?

A

Functions defined inside other functions

They can access variables from the enclosing scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are anonymous functions in Python?

A

Functions created using the lambda keyword

They are typically used for short, throwaway functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a recursive function?

A

A function that calls itself

It requires a careful exit strategy to avoid infinite recursion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the return statement in a function?

A

Exits a function and returns a value to the caller

If no expression is specified, it returns None.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does ‘pass by reference’ mean in Python?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a module in Python?

A

A Python file (.py) containing functions and global variables

Modules are used for code organization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of importing a module?

A

To use functions and variables defined in that module

Use the statement import module_name.

17
Q

What is a package in Python?

A

A directory containing a collection of modules and an __init__.py file

Packages are used for code distribution and reuse.

18
Q

How do you import a specific module from a package?

A

Use the import package_name.module_name statement

This allows access to the specified module within the package.

19
Q

What is a library in Python?

A

A collection of related code functionalities that can be imported into a program

Libraries may consist of a collection of packages.

20
Q

What is the difference between a module and a package?

A

A module is a single file; a package is a directory containing modules and an __init__.py file

This distinction helps organize code effectively.

21
Q

What is the difference between a package and a library?

A

A package is a collection of modules; a library is a broader term for a collection of packages and modules

Libraries offer related functionalities.