nerdexam
Python_Institute

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

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?

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)
  • A
    81% (30)
  • B
    3% (1)
  • C
    11% (4)
  • D
    5% (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 in sys - importing it would raise an ImportError.
  • D uses the correct argv name but prints argv[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.

Full PCEP-30-02 Practice