What is Python?
Python is an object-oriented programming language created by Guido Rossum in 1989. It is ideally designed for rapid prototyping of complex applications. It has interfaces to many OS system calls and libraries and is extensible to C or C++. Many large companies use the Python programming language include NASA, Google, YouTube, BitTorrent, etc.
Numbers can be integers or floating point values: 42, 3.14159
Strings can be enclosed in single or double quotes, and can contain any printable character: “test” ‘Hello, world!’
To create a raw string, in which backslashes are not interpreted as escape sequences, specify r before the opening single quote or double quote that encloses the string:
rawstr = r”This is a \raw \string \that \contains four backslashes”
A variable can be any combination of letters, digits and underscore characters. The first character cannot be a digit. Variables in Python are case sensitive: variable and VARIABLE are not the same.
x _LabName RESULT2 VaRiAbLe
Use an assignment to store a value in a variable:
patientid = 42113
patientstatus = “Admitted”
The None Object
Python defines a special object, called None, that can be used to specify the empty value:
value = None
By default, the Python None object is disabled in VMD files. See Disabling/Enabling the Python None Object in the manual for more details.
String and Number Conversion
Use int, float and str to convert numbers to strings and vice versa:
integertemp = int(“37”)
floattemp = float(“98.6”)
stringtemp = str(98.6)
print displays values on the screen or in a log file:
print ‘The patient ID is’, patientid
You can use %s with print to display the values of variables as part of a string:
print ‘The patient IDs are %s and %s’ % (patientid1, patientid2)
Everything after the # character is treated as a comment and ignored:
# this is a comment
temperature = 98.6 # this is also a comment
Use \ to continue a statement on more than one line:
Python supports the standard arithmetic operations on integers and floating point numbers:
y = x + 1 # addition y = x — 1 # subtraction
y = x * 1.8 # multiplication y = x / 1.8 # division
y = 33 % 4 # remainder from division, or modulo; y is 1 in this example
y = 2 ** 5 # exponentiation, or x to the power y; 32 in this example
Operations are normally performed in this order: **, then *, / and %, then + and -. Use parentheses to specify an order of operation.
You can use the + and * operators with strings:
patientid = “4” + “2” + 2 * “1” + “3” # patientid is assigned ‘42113’