🐍 Python Intermediate

What is Python's argparse module?

Why Interviewers Ask This

This question targets practical, hands-on experience with Python. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

The argparse module parses command-line arguments for Python scripts. Create a parser: parser = argparse.ArgumentParser(description="Process files"); parser.add_argument("filename", help="Input file"); parser.add_argument("-v", "--verbose", action="store_true"); parser.add_argument("-n", "--count", type=int, default=10); args = parser.parse_args(). Access: args.filename, args.verbose, args.count. Argument types: positional (required), optional (-v/--verbose), action="store_true" (flag), choices=["json", "csv"] (restricted values), nargs="+" (one or more). Subparsers for sub-commands: parser.add_subparsers(dest="command"). Auto-generates --help. Alternatives: click (decorator-based, more ergonomic), typer (uses type hints for argument definition — modern favorite).

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Python codebase.