Simple argparse example wanted 1 argument, 3 results
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
argparse is Python's standard way to parse command-line arguments, and it is especially useful when a script should behave differently based on user input. If you want one argument to produce three related results, the clean approach is to parse the value once and then compute all three outputs explicitly in normal Python code.
A Single Positional Argument
The most direct design is to accept one positional integer and derive three results from it. For example, we can print the square, cube, and factorial of the number.
Run it like this:
Example output:
This pattern keeps parsing and computation separate, which makes the code easier to test and extend.
Why argparse Helps Even for One Argument
At first glance, using argparse for a single input may seem like overkill. But it buys you several useful things immediately:
- automatic help text
- automatic type conversion
- clear error messages for invalid input
For example, if the user passes a non-integer string, argparse reports the error cleanly instead of forcing you to write custom validation from scratch.
A Version With Named Arguments
Sometimes a named option is clearer than a positional one.
Then the command becomes:
This is more verbose, but it is sometimes preferable in scripts with several options because it makes the call self-documenting.
If the "Three Results" Are Different Modes
Some people mean something slightly different by "one argument, three results." They may want one command-line argument that selects among three behaviors rather than one number that produces three values.
In that case, choices is a good fit:
So the right argparse design depends on whether "three results" means three outputs from one value or three branches selected by one value.
Help Text Comes for Free
One of the most useful features of argparse is that the script documents itself:
That command prints the usage message, argument descriptions, and type expectations. It is a small feature, but it is one reason argparse is better than manual sys.argv handling in most real scripts.
Common Pitfalls
The biggest pitfall is putting too much logic into the parsing layer. argparse should gather and validate input; the actual program behavior should usually happen afterward in ordinary Python code.
Another common mistake is forgetting type conversion. If you do not specify type=int, the parsed argument arrives as a string, which can silently break later calculations.
Developers also sometimes choose a positional argument when a named option would make the command clearer, or vice versa. The right choice depends on how often the script is used and how many arguments it is likely to grow.
Summary
- Use
argparseto parse the input once, then compute the three outputs in normal Python code. - A single positional argument is often the simplest design for one required value.
- Named options such as
--numbercan be clearer in more explicit command lines. - If one argument selects among three behaviors,
choicesis often the right tool. - '
argparseadds validation, help text, and type conversion with very little code.'
Related reading
- Simple Digit Recognition OCR in OpenCV-Python
- Simple example using BernoulliNB naive bayes classifier scikit-learn in python - cannot explain classification
- Simple Linear Regression in Python
- Simple multi layer neural network implementation
- Simple Python Challenge Fastest Bitwise XOR on Data Buffers
- Simple Python implementation of collaborative topic modeling?
- Simple way to find if two different lists contain exactly the same elements?
- Simple way to measure cell execution time in ipython notebook
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.