This post is also available as a PDF, a Jupyter Notebook, and a py file. Continue reading “83. Automatically passing unpacked lists or tuples to a function (or why do you see * before lists and tuples)”
Accessing date and time
It may sometimes be useful to access the current date and/or time. As an example, when writing code it may be useful to access the time at particular stages to monitor how long different parts of code are taking.
To access date and time we will use the datetime module (which is held in a package that is also, a little confusingly called datetime!): Continue reading “19. Accessing date and time, and timing code”
Lambda functions are a Pythonic way of writing simple functions in a single line.
They may be applied to a list using the map statement.
The function filter offers a concise way to filter out all the elements of a list based on the results of a function.
The function reduce continually applies a function to a sequence until a single value is returned. Continue reading “18. Python basics: Lambda functions (one line functions), and map/filter/reduce”
Functions are (or should be) short sections of code that may be called from within the main programme, or from another function. They usually take some inputs (arguments) and return one or more results.
Functions may be used for two purposes:
1) To avoid repeating code when the same code needs to be run in two or more locations in the main programme.
2) To structure the code, breaking the code down into smaller pieces. In functional programming the whole programme would be broken down into functions Continue reading “17. Python basics: functions”
For data analytics we will usually be using two libraries called NumPy and Pandas which have their own simple methods for importing data, but here is the standard Python method. This standard method may sometimes have an advantage that data may be written and read one line at a time without having all data held in memory. This may be useful for very large data files. Continue reading “16. Python basics: Read and write files”
Python allows a lot of control over formatting of output. But here we will just look at controlling decimal places of output.
There are some different ways. Here is perhaps the most common (because it is most similar to other languages). Continue reading “15. Python basics: decimal places in output”
In perfect code all eventualities will be anticipated and code written for all eventualities. Sometimes though we might want to allow for more general failure of code and provide an alternative route. Continue reading “14. Python basics: try …. except (where code might fail)”