Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Functions III - returning values

Returning values from a function

Programmers rarely use functions to print to the screen as the print() function already does that for us. Normally functions are used to do something to the argument that is passed to it then return the result of that something.

Consider the len() function:

seq_len = len( 'ACGTGGGTA' )

In this code we pass a DNA sequence as a string to len() which returns its length which we can assign to a variable, in this case called seq_len.

This is a more versatile use of functions than just printing because we can now use the value of seq_len for other purposes.

In the following code is a function called square_it() that takes a number, squares it and returns the square.

Run it to see what happens.
def square_it(x): """Return the square of the number x""" square = x**2 return square # Call square_it() with the argument 9. # Assign the returned value (which is 81) to the variable y. y = square_it(9.) print(y)
81.0

The function square_it() takes a number and stores it in the parameter x. x is squared (remember ** is the power operator, i.e., x to the power 2 is x squared.). The square of x is assigned to the variable square. Finally, the function returns the value in square back to the main program where it is assigned to the variable y, and then printed.

Functions can return any type of variable

In the last example our function returned a float. But a function can return any type of variable: a string, a list, a dictionary.

The following code takes a list of DNA sequences and returns a list of their lengths.

def sequence_lengths(sequences): """Return a list of lengths of DNA sequences""" # Create an empty list to store the sequence lengths. seq_lengths = [] # Loop through the sequences appending each sequence's length to "seq_lengths". for seq in sequences: seq_lengths.append( len(seq) ) return seq_lengths dna_sequences = [ 'ACGTAATTACGTAACGT','ATGATATATCAT','ATTAGTACACCGAAAAG','GGACAAACCGTTTA','AAAGATATCTTT' ] print( sequence_lengths(dna_sequences) )
[17, 12, 17, 14, 12]

Functions can return more than one value

The above function returned just a single value, but functions can return as many values as we like.

The function count_purines_pyrimidines() in the following code takes a DNA sequence and returns the number of purines (A and G) and the number of pyrimidines (C and T).

def count_purines_pyrimidines(dna_seq): """Return the number of purines and pyrimidines in a DNA sequence""" purines = dna_seq.count('A') + dna_seq.count('G') pyrimidines = dna_seq.count('C') + dna_seq.count('T') return purines, pyrimidines # Assign a DNA sequence to variable dna_seq. dna_seq = 'ACTTTGAGGATTA' # Call the function to count the number of purines and pyrimidines in dna_seq. num_purines, num_pyrimidines = count_purines_pyrimidines(dna_seq) print(f'{num_purines} purines') print(f'{num_pyrimidines} pyrimidines')
7 purines 6 pyrimidines

The function returns both values stored in the variables purines and pyrimidines simultaneously back to the main program.

Exercise Notebook

Next Notebook