These are the two major ways of saving yourself programming time, by reusing the code you or other people have already written (and hopefully tested). Small functions will make, while some judiciously-chosen includes will save you hours and hours of rewriting.
6.1 Functions
Functions are probably the most useful way of saving yourself time. Most programs involve repeatedly performing some simple operation. Putting this calculation into a function and then calling the function results in code that is easier to read, less buggy (you only need to debug it once) and faster to write. A function looks like this:
def functionname(argument1, argument2):
#code
return returnvalue
6.1.1. Return values
The primary way of getting data out of a function is with the return value. The return value can be an array, which allows you to return multiple things to the main program.
Return values are optional; a function's output may not need any further processing (for example, if you had a function which merely printed out a warning message, you wouldn't need to do any further calculation).
You can accomplish the same task with global variables, but this is messier.
Here's an example of getting multiple return values:
def listexample(input):
num = input
numsquared = num*input
numcubed = numsquared*input
return num, numsquared, numcubed
(five, twentyfive, onetwentyfive) = listexample(5) # gives 5, 25, 125 to the corresponding variables
6.1.2. Arguments
The primary way of getting input to a function is with arguments. Like return values, these are not required (although most useful functions, like most useful programs, take some input and produce some output). In the example above, the variable 'input' (with the given value 5) is the argument.
You can make a function more flexible by assigning default values to arguments:
def sum(a, b, c=0):
return (a+b+c)
a = 2
b = 3
print(sum(a,b)) # prints 5
print(sum(a,b,10)) # prints 15
Note that specifying a default value for c allows you to skip it when the default value is correct.
6.1.3. Global variables
Python is much looser on 'scope' than many other programming languages. That said, to modify one of your program's variables inside a function, you will need to use the 'global' operator:
fruit = "apples"
def gotomarket():
global fruit
fruit = "oranges"
return
gotomarket() # converts apples into oranges
print("I like to eat", fruit, "!") #prints "I like to eat oranges!"
In the above program, fruit (originally defined as 'apples') is converted to 'oranges' by the function 'gotomarket()', and then printed.
As an aside, global variables are deprecated in computer science. Global variables tend to introduce errors in your code and make it hard to udnerstand the flow of the code because variables can be changed anywhere. Cleaner, less-buggy code results from strict scoping-- i.e., only passing data between the main program and subroutines via return values and arguments:
def gotomarket(foodin): #passes data to the function via an argument
foodout = "oranges"
return foodout # passes data back to the main program via a return value
fruit = "apples"
fruit = gotomarket(fruit) # converts apples into oranges
print("I like to eat ", fruit, "!") #prints "I like to eat oranges!"
6.1.4. Recursion
A neat trick for making functions simpler (or at least, shorter!) is to include the function inside itself. This is probably best explained by an example:
def fact(n):
if (n<=1):
return 1
else:
return n*fact(n-1)
When you call fact(3), the following happens:
- fact(3) is called by the code.
- Inside fact the value of n=3 is > 1, so 3*fact(2) is calculated
- fact(2) is called by the code.
- Inside fact the value of n=2 is > 1, so 2*fact(1) is calculated
- fact(1) is called by the code.
- Inside fact, n==1, so 1 is returned.
- back inside fact(2), 2*1 is returned
- back inside fact(3), 3*2 is returned
- The value 6 is returned.
When writing recursive functions, it is very important to ensure that the recursion ends somewhere! Note that although the function keeps calling itself over and over, it ends once it reaches the number 1.
Python does have a built-in recursion limit (1000 recursions) but this may be modified via the sys module. You should also be careful if a recursion function does something 'expensive' like uses a lot of memory, or opens a file, as that will slow down your program a lot.
6.2. Includes
One of Python's strengths is its vast quantity of community-created content. available to be inserted into your code in the form of modules. These are typically added using one of the following lines:
from module import *
or
import module
In general it is better to use the second form, or only import those methods you need. For example:
from math import cos
Importing only those functions you need will result in faster, smaller and less buggy code.
The functions you've imported can be used like regular functions:
cos(0) # 1
6.2.1. Some common includes
- math - Mathematical operations
- sys - python built-in system settings and variables (such as the recursion limit, program arguments, etc)
- numpy - numeric routines
- scipy - scientific routines
- RS - R / S-Plus bindings for Python
- matplotlib - Plotting functions
- getopt - basic command-line argument processing
- os - Operating system commands (path manipulation, etc.)
- random - Better way to generate random numbers (random.random) [can also be done with os.urandom()]
- time - Time functions (get current time, calculate time later, etc)
- datetime - Date & time functions
- pickle - Data pickling
6.2.2. Some common 'data science' includes
- pandas - A package that emulates 'comma separated files' in memory and allows you to average, sort, and perform other operations.
- scikit-learn - Machine learning, regression, and model fitting (not deep learning)
- statsmodels - Statistical models and functions for robust regression. The Python equivalent of a subset of R routines
- seaborn - A wrapper around matplotlib to give you cleaner figures and newer plot types
- ggplot - An implementation of the R package of the same name, to make plots using simple commands and a 'grammer' of graphics.
- bokeh - A javascript compatible plotting library, that allows you to make interactive plots in a webpage.
- Up to Index
Previous: 5. Data types | Next: 7. Miscellanea