Introduction to Python
|
Python is a general purpose programming language that allows you to get a computer to do almost anything
Generally, Python implementations are interpreted rather than compiled
It is particularly useful at analysing data
The ipython interpreter can use tab completion and keeps a history of the commands you run
Use Ctrl-D to exist an IPython session
Python code is case sensitive
|
Types, Objects, Values and Variables
|
Types are how python should interpret data in a memory location (‘object’) and what operations can be performed by it
Objects are defined areas of memory that holds data (‘Values’) associated with a Type
Values are the actual data/bits in memory interpreted by the ‘Type’
Variables are a flag or name for a particular area of memory (‘Object’)
Variable names must only contain letters, numbers and underscores. They are case-sensitive.
You can assign values to a variable using the = operator
The basic (non collection) data types are: integers, floating point numbers, strings and booleans.
|
Operators
|
Operators are used to perform actions on objects
They have different behaviour depending on the objects that are being acted on
There is nothing special about operators - they are just shorthand for running other bits of code
|
Storing Multiple Values in Collections
|
[value1, value2, value3, ...] creates a list.
Lists can contain any Python object, including lists (i.e., list of lists).
Lists are indexed and sliced with square brackets (e.g., list[0] and list[2:9]), in the same way as strings and arrays.
Lists are mutable (i.e., their values can be changed in place).
Strings are immutable (i.e., the characters in them cannot be changed).
{ key1:value1, key2:value2, ...} creates a dictionary
The keys are used to reference and retrieve the values
As with lists, they are mutable - their values can be changed in place
|
Writing Python Scripts
|
Python commands can be written and stored in a plain text file
This file can then be run by using the python <scriptname>
Python scripts generally use the .py extension
You should try to use a syntax-highlighting text editor to edit your Python scripts
Use comments to improve the readability of your code and aid understanding for both other people and yourself in the future
|
Making Choices
|
Use if condition to start a conditional statement, elif condition to provide additional tests, and else to provide a default.
The bodies of the branches of conditional statements must be indented.
Use == to test for equality.
X and Y is only true if both X and Y are true.
X or Y is true if either X or Y , or both, are true.
Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.
True and False represent truth values.
|
Repeating Actions with Loops
|
Use for variable in sequence to process the elements of a sequence one at a time.
The body of a for loop must be indented.
Use len(thing) to determine the length of something that contains other values.
|
Using External Modules
|
To import a module, simply call import <module>
You can import particular elements of a module using from <module> import <func>
Rename a module in your code using import <module> as <myname>
|
Creating Functions
|
Define a function using def function_name(parameter) .
The body of a function must be indented.
Call a function using function_name(value) .
Variables defined within a function can only be seen and used within the body of the function.
If a variable is not defined within the function it is used, Python looks for a definition before the function call
Put code whose parameters change frequently in a function, then call it with different parameter values to customize its behavior.
|
Errors and Exceptions
|
Tracebacks can look intimidating, but they give us a lot of useful information about what went wrong in our program, including where the error occurred and what type of error it was.
An error having to do with the ‘grammar’ or syntax of the program is called a SyntaxError . If the issue has to do with how the code is indented, then it will be called an IndentationError .
A NameError will occur if you use a variable that has not been defined, either because you meant to use quotes around a string, you forgot to define the variable, or you just made a typo.
Containers like lists and strings will generate errors if you try to access items in them that do not exist. This type of error is called an IndexError .
Trying to read a file that does not exist will give you an FileNotFoundError . Trying to read a file that is open for writing, or writing to a file that is open for reading, will give you an IOError .
|
Reading in Tabular Data
|
Use the numpy library to work with arrays in Python.
The expression array.shape gives the shape of an array.
Use array[x, y] to select a single element from a 2D array.
All the indexing and slicing that works on strings and lists also works on arrays.
Use numpy.mean(array) , numpy.max(array) , and numpy.min(array) to calculate simple statistics.
Use numpy.mean(array, axis=0) or numpy.mean(array, axis=1) to calculate statistics across the specified axis.
|
Producing Plots
|
|
Analyzing Data from Multiple Files
|
Use glob.glob(pattern) to create a list of files whose names match a pattern.
Use * in a pattern to match zero or more characters, and ? to match any single character.
|
Filtering Data
|
|
Tidying up the Analysis
|
|
Command-Line Programs
|
The sys library connects a Python program to the system it is running on.
The list sys.argv contains the command-line arguments that a program was run with.
Avoid silent failures.
The pseudo-file sys.stdin connects to a program’s standard input.
The pseudo-file sys.stdout connects to a program’s standard output.
|