u11-slides-modules-packages Flashcards

1
Q

What is the main purpose of modules in Python?

A

Modules allow you to reuse code (such as functions) in different programs and projects by putting the code into separate files.

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

What is the naming convention for Python modules?

A

Modules should use lowercase letters with underscores (if needed) for separation.

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

What is the naming convention for Python packages?

A

Packages should use lowercase letters, and underscores are discouraged.

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

What are the four main ways to import a module named ‘my_module’?

A
  1. import my_module (use as my_module.add(…))\n2. import my_module as mm (use as mm.add(…))\n3. from my_module import add (use as add(…))\n4. from my_module import add as my_add (use as my_add(…))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What makes a directory a Python package?

A

A directory becomes a Python package when it contains an \_\_init\_\_.py file (which can be empty or contain initialization code).

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

What happens when you import a module in Python?

A

All code within the module is executed when it’s imported.

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

How do you prevent code from executing when a module is imported?

A

Put the code inside an if \_\_name\_\_ == "\_\_main\_\_": conditional block. This code will only run when the file is executed as a script, not when imported.

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

What are the different ways to import from a package?

A
  1. import mypackage.my_module\n2. import mypackage.my_module as mm\n3. from mypackage import my_module\n4. from mypackage.my_module import add\n5. from mypackage.my_module import add as my_add
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between using import my_module and from my_module import add?

A

With import my_module, you need to use the module name to access functions (my_module.add()), while with from my_module import add, you can use the function directly (add()).

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

Does the \_\_init\_\_.py file in a package need to be empty?

A

No, while it can be empty, it can also contain initialization code for the package.

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

How do you import and rename both a module and a specific function?

A

For module: import my_module as mm\nFor function: from my_module import add as my_add

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

What is the syntax for importing from a nested package structure?

A

Use dot notation: from mypackage.my_module import add or import mypackage.my_module

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

What’s the purpose of renaming modules or functions during import?

A

Renaming (using ‘as’) helps avoid naming conflicts and can make code more readable or concise when module names are long.

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

What’s the difference between a module and a package?

A

A module is a single Python file, while a package is a directory containing multiple modules and must include an \_\_init\_\_.py file.

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

When running a Python file as a script, what is the value of \_\_name\_\_?

A

When running a file directly as a script, \_\_name\_\_ equals "\_\_main\_\_". When the file is imported as a module, \_\_name\_\_ equals the module’s name.

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