Python is an interpreted scripting language. This means that Python programs are, in their most basic form, just text files with a list of commands, which are executed by a program called the Python interpreter. Compiling Python programs, or "scripts", into binary 1s and 0s is not necessary, as the interpreter can read the source code directly. In general, commands are executed in order, starting with the first line of the program and on down to the last, although certain statements give the possibility of repeating or skipping over chunks of code (cf. Section 4. Control flow). In addition, a Python program may start with the following line:
which is ignored on Windows systems, but not on a Unix or Linux system. Its use is to tell the operating system that it should ask the Python interpreter to run this script, if the script has been marked as executable and the user attempts to run it.
Any useful computer program, in a general sense, is a construct that takes some type of input and produces some type of output. The inputs and outputs, and anything else the program might work with in between, are all treated as data. For example, an audio recording program takes as its input data a sequence of volumes and frequencies, and produces a sound file listing these, or some subset of these. The data one usually works with in a simple program can be broadly separated into two groups:
A literal is exactly what it says (literally!). For example, the number 5 is a literal:
will print the number 5 on the screen.
A sequence of letters (a string) is also a literal, if enclosed by single or double quotes:
will print the sentence on a line on the screen, but
will not, because there's no quotation marks to tell Python that the string is a literal.
Python can store values in containers called variables. This allows you to do things like storing the result of a calculation and calculating something else with it:
a = 2 b = 3 c = a+b d = c*c
a now holds the value 2; b holds 3; c holds their sum (5); and d holds the square of c (25).
It's important to remember that the equal signs assign the value on the right to the variable on the left:
a = 5 b = 4 =a = b
a was originally assigned the value 5, but now has the value 4, and b still has the value 4.
Data comes in various types, such as strings, integers and floats. It's important to know that different data types are treated differently by Python:
outputs the number 10, but
outputs "55", because Python thinks the 5s with quotations are strings, not numbers. To get Python to do what you want, you may need to convert your data to the appropriate type. See Section 5.4.5 for more information on data type conversion.
1.2. Comments and docstrings
For your own convenience, and to help others understand your code, it is strongly recommended to include remarks (or comments) about what your code is doing together with the code. Comments in Python are designated with a # symbol: anything after the # is ignored by the Python interpreter.
a = (2+3)**2 # a has 5 squared now
A different method of documenting your code is by a docstring. Docstrings are a sort of introductory explanation of the program you're writing, put at the start of the program. These are started and ended by three double-quote symbols in a row. For example:
def printHelloWorldFunction(): """This function prints out a greeting to the world! Written July 1st, 2010 by A. Programmer. This comment will end after this triple quote--->""" print("Hello, world!") return
Docstrings are not exactly comments, in that they're not ignored by Python, but they're still a form of documentation.
Comments are also very useful when your code doesn't work--a time-honoured debugging method is to 'comment out' everything except the bare essentials to isolate what isn't doing what it should.