PCEP-30-02 · Question #136
You execute the following command in the terminal. python index.py Hello You want the command to print out Hello. What has to be inside of index.py?
The correct answer is A. from sys import argv\nprint(argv[1]). Option A is correct because Python's sys module exposes command-line arguments through a list called argv (argument vector), where argv[0] is always the script name (index.py) and argv[1] is the first user-supplied argument - in this case, Hello. Why the distractors fail: B and…
Question
Options
- Afrom sys import argv\nprint(argv[1])
- Bfrom sys import args\nprint(args[1])
- Cfrom sys import args\nprint(args[0])
- Dfrom sys import argv\nprint(argv[0])
How the community answered
(37 responses)- A81% (30)
- B3% (1)
- C11% (4)
- D5% (2)
Explanation
Option A is correct because Python's sys module exposes command-line arguments through a list called argv (argument vector), where argv[0] is always the script name (index.py) and argv[1] is the first user-supplied argument - in this case, Hello.
Why the distractors fail:
- B and C use
args, which does not exist insys- importing it would raise anImportError. - D uses the correct
argvname but printsargv[0], which is"index.py"(the script name), not"Hello".
Memory tip: Think of argv like a numbered list starting at zero - slot 0 is always "who am I?" (the script), and slot 1 is "what was I told?" (your first argument). The name argv comes from C programming convention: argument vector.
Community Discussion
No community discussion yet for this question.