Head to the official Python website and download the relevant installation package for your system. There are usually two production versions available. Either is fine.
Commands can be written and executed in the Python interpretor, or placed in a program file with the file extension of .py
. Double-clicking on the Python file will then run it. Generally, lines starting with, and immediately following, >>>
are written in the interpretor.
First key point to remember is that Python does not need semi-colons at the end of expressions, and that whitespace is very important! Be sure to indent text when required, especially in statements and functions.
Start single line comments with the hash key #
. For multi-line comments use three consecutive double quotes, i.e.
Numbers (integers or floats) behave similar to other programming languages, though take care with integer division. For example,
Mathematical operators are the usual + - * / %
, with **
to denote the exponential operator, such as
Python variables are loosely-typed and case-sensitive. Be sure to capitalize the first letter of your booleans.
Use the +
key to concatenate strings, or string literals. The print
command prints strings:
The str()
function performs explicit string conversion, so explicitly converts anything that isn’t a string, into a string. Implicit string conversion is literally putting quotes around a sequence of characters.
Another function is len()
which returns the length of a sequence of characters. Other basic string-specific methods are .upper()
, .lower()
, noting that these use dot notation.
Remember that strings in Python are immutable meaning that you cannot change them once they have been created. Strings can be formatted with %
via the following syntax
where s
represents a string. For example,
where name
and like
are variables of string type.
Methods exist to convert variables between datatypes, e.g. int()
, float()
, and str()
.
To ask for user input use the following commands:
Syntax for conditional statements are
Note: Remember the colons!
There are six comparators: ==
, !=
, <
, <=
, >
, and >=
.
There are two kep looping constructs
A while loop:
and a for loop:
To print to stdout, we use the print
command (Python 2)
or the print function (Python 3)
Opening a file
To read data
To write text to a file
Reading a file one line at a time
Generally, there are three parts to a function in Python
To call the function, just type name_of_function()
. Parameters may be placed, as a comma separated list, in the parentheses.
Use splat arguments if you do not know how many arguments a function will take:
To specific an optional argument, or provide a default value for a parameter, define it in the function header:
A tuple is a read-only collection of related values grouped together
You can then unpack the tuple in variables
A dictionary is a collection of values indexed by ‘keys’
A list is an ordered sequence of items (usually of the same type. Python does not care, but we do not normally do this.)
We can create a new list by applying an operation to each element of a sequence. This is called list comprehensions
A set is an unordered collection of unique items, useful for detecting duplicates or related tasks.
A module is a file that contains definitions — including variables and functions — that you can use. You can import modules by using the import
command, such as for math
library
and call a function such as math.sqrt()
, where the math.
prefix tells python to look for the function sqrt()
in the math
module.
Note that in your program, you can assign a custom name to a function. For example,
You can also just get a single function from a module by function import such as just the sqrt
function
You can then call the function by sqrt()
, noting that we do not need the math.
prefix.
To import everything from a module use an asterisk
but be careful with this. You have been warned.
To read data from the web, we can import the urllib
module.
Parsing a document into a tree
Useful methods include findall('elname')
, findtext('elname')
-i
flag to enter an interactive session after running a Pythoon script file. Useful when debugging. i.e. python -i helloworld.py
There are a lot of third-party libraries which provide many out-of-the-box functionality. e.g.