Dead Simple Python Flashcards

(16 cards)

1
Q

How to quit the interactive Python session?

A

exit()

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

Two steps to open a venv project of python?

A
  1. open the project containing env directory.
  2. set the python interpreter from terminal (bash/cmd/powershell).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to check which python interpreter is currently active in my venv?

A
  1. where python (windows)
  2. which python (Linux/macOS)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to activate the python interpreter of the venv from terminal?

A
  1. For bash, type:
    source ./Scripts/activate
  2. For cmd, type: .\Scripts\activate.bat
  3. For powershell: .\Scripts\activate.ps1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If trying to activate Python interpreter results in permission error in powershell (scripts not allowed to execute), then what to do?

A
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to leave the virtual environment of Python?

A
  1. For powershell: deactivate
  2. For cmd: .\Scripts\deactivate.bat
  3. For bash: deactivate
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Create a virtual environment in Python by the name “myenv”.

A

python3 -m venv myenv

It creates a myenv/ directory in the current working directory.

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

To run a Python module from CLI, it must have _ .

Create a module that displays your name and run it from CLI using -m flag

A

“__main__” entry point.

\_\_name\_\_ == '\_\_main\_\_'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to print the module name (mytool) when running the module using
python -m mytool command

A

print(sys.argv[0])

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

What is a positional argument in python CLI?
How is it different from other (optional) arguments?

A

Positional argument
* doesn’t use --.
* is required by default.
* Order matters.

Non-positional argument
* starts with -- or -.
* often has a default value
*

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

CLI command:
python -m mymodule coffee --name Sahil

CLI output:
searching coffee for Sahil

A
def main():
parser = argparse.ArgumentParser(description="Search items for you")
    parser.add_argument(
        "itemname", 
        help="item to search for",
        nargs="?",
        default="coffee"
    )
    parser.add_argument("--name", help="Your name", required=True)
    argsObject = parser.parse_args()
    print(f"Searching {argsObject.itemname} for {argsObject.name}...")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to specify that argument is optional, i.e., at most one (0 or 1) argument is allowed, while adding argument to ArgumentParser?

A

nargs

parser = argparse.ArgumentParser()
parser.add_argument(
  "--foo", 
  nargs="?", 
  default="value"
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are lazy imports in Python?

A

Importing modules inside a function or conditionally, makes the import lazily evaluated.

def process_data():
    import pandas as pd
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

As best practice, keep imports scoped to the respective modules and entrypoint of the application lean.
What is entrypoint of a Python application?

A

main.py

Imports like pandas are inside run_app, which

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

main.py contains following code:

if \_\_name\_\_ == "\_\_main\_\_":
    run_app()

The run_app() function contains import to pandas. During during container startup with CMD ["python", "main.py"] , will the pandas module be loaded?

A

No.
pandas is only loaded when run_app() is called — not during container startup.

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

Python normally doesn’t allow breaking a statement across lines unless inside parentheses, brackets, or braces.
Wrapping the chaining call of functions inside _ lets you split it neatly across multiple lines

A

parentheses
(…)

Everything inside the parentheses is treated as one single expression. Without parentheses, you’d need ugly backslashes \ for line continuation.