Programming with Python
Built-in functions are always available to use (without additional
libraries).
Use help(thing) to view help for something.
You may have seen some error messages already, they provide
information about what has gone wrong with your code and where.
Basic data types in Python include integers, strings, and
floating-point numbers.
Use variable = value to assign a value to a variable in
order to record it in memory.
Variables are created on demand whenever a value is assigned to
them.
Use print(something) to display the value of
something.
Use # some kind of explanation to add comments to
programs.
[value1, value2, value3, ...] creates a list, (this
process does not have to be manual).
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.
Dictionaries are indexed with the key (e.g.,
dictionary[‘first_entry’])
Some objects are mutable (e.g., lists).
Some objects are immutable (e.g., strings).
Different data structures exist because they support different ways
of organising and accessing information.
Libraries give us access to code that other people have already
written.
We import libraries so we can use their tools.
We sometimes rename them with as to make our code shorter and easier
to work with.
We use environments to manage package versions as our projects get
more complicated.
Remember array indices start at 0, not 1.
Remember low:high to specify a slice that
includes the indices from low to high-1.
It’s good practice, especially when you are starting out, to use
comments such as # explanation to explain what you are
doing.
We have shown some simple examples but you could slice your data in
much more complicated ways depending on your requirements.
It is hard to get an understanding of the data by just reading the
raw numbers.
We can easily visualise data using matplotlib.
There are other libraries that are popular (e.g., seaborn).
Getting figures “paper ready” can take a bit of time and
effort.
Use for variable in sequence to process the elements of
a sequence one at a time.
Don’t forget to indent.
You can use len(thing) to determine the length of
something that contains other values.
Use if condition to start a conditional statement,
elif condition to provide additional tests, and
else to provide a default.
Use == to test for equality and = for
assignment.
In Python, some values are treated as false in conditions, including
\0, '', [], and
None.
The body of a function must be indented.
The scope of variables defined within a function can
only be seen and used within the body of the function.
Variables created outside of any function are called global
variables.
Within a function, we can access global variables.
Variables created within a function override global variables if
their names match.
Put docstrings in functions to provide help for that function.
Specify default values for parameters when defining a function using
name=value in the parameter list.
Parameters can be passed by matching based on name, by position, or
by omitting them (in which case the default value is used).
Put code whose parameters change frequently in a function, then call
it with different parameter values to customize its behavior.
Be aware of your current working directory
One of the biggest struggle for importing your data into Python is
getting the paths correct.
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 when trying to use a variable
that does not exist. P
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.
Know what code is supposed to do before trying to debug
it.
Make it fail every time.
Make it fail fast.
Change ONLY one thing at a time, and for a reason.
Keep track of what you’ve done.
Be humble and patient.
Use help.