This guide is specifically for the Python 3 language. Note that older versions (eg Python 2) do vary quite a bit in syntax and operation.
Starting Out
All Python programs should begin with a ‘shebang’ command which tells the operating system how to deal with the script if it is called from outside of the Python program.
#!/usr/bin/env python3
Command Line Arguments
Command line arguments are useful in many programs, especially on command line based systems. In Python command line arguments are passed via the sys.argv
list. For example:
import sys
print ("This program has: ", len(sys.argv), " command line arguments")
print ("Arg list: ", str(sys.argv))
$ python3 testprogram.py hello world
> This program has: 3 command line arguments
> Arg list [‘testprogram.py’, ‘hello’, ‘world’]