Paul's Python CoursePPC

sys.argv, what was typed

Not met yet

sys.argv is a list of what was typed to start the program. sys.argv[0] is the program's own name; the arguments start at sys.argv[1], and sys.argv[1:] is all of them. Every item is text, even 3, so convert numbers yourself. If someone forgets an argument, indexing past the end raises IndexError, which is one reason bigger tools use argparse.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks command-line arguments arrive as numbers when digits are typed.
  • Thinks sys.argv[0] is the first word typed after the program's name, rather than the program's own name.

Where it comes up