Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lesson 7: Introduction to functions notebook

def ratio(x, y):
    """The ratio of `x` to `y`."""
    return x / y
def ratio(x, y):
    """The ratio of `x` to `y`.
    return the result"""
    result = x / y
    return result
help(ratio)
Help on function ratio in module __main__:

ratio(x, y)
    The ratio of `x` to `y`.
    return the result

ratio?
? ratio
ratio ?
  Cell In[7], line 1
    ratio ?
          ^
SyntaxError: invalid syntax
?ratio
?print
type(ratio)
function
ratio(4 / 2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 ratio(4 / 2)

TypeError: ratio() missing 1 required positional argument: 'y'
ratio(4, 2)
2.0
ratio(4, 2)
print("I hope you liked your result")
I hope you liked your result
def ratio2(x, y):
    """The ratio of `x` to `y`."""
    print(x / y)
ratio(4, 2)
2.0
def ratio(x, y):
    """The ratio of `x` to `y`."""
    return x / y
ratio(4, 2)
2.0
result = ratio(4, 2)
print(result)
2.0
result = ratio2(4, 2)
print(result)
2.0
None
result
ratio(10, 7)
1.4285714285714286
def answer_to_the_ultimate_question_of_life_the_universe_and_everything():
    """Simpler program than Deep Thought's, I bet."""
    return 42
answer_to_the_ultimate_question_of_life_the_universe_and_everything
<function __main__.answer_to_the_ultimate_question_of_life_the_universe_and_everything()>
answer_to_the_ultimate_question_of_life_the_universe_and_everything()
42
ratio
<function __main__.ratio(x, y)>
ratio(5, 3)
1.6666666666666667
answer_to_the_ultimate_question_of_life_the_universe_and_everything()
42
print('Hello world')
Hello world
print()

pi = 3.14159
pi = 22/7
pi
3.142857142857143
def pi():
    return 3.14159
pi()
3.14159
def greet(name):
    print('Hello', name)
result = greet('Jonathan')
Hello Jonathan
result
int = float
int('4')
4.0
int == __builtin__.int
False
int == __builtin__.float
True
int = __builtin__.int
int('4')
4
def int(name):
    print('Hello', name)
int('Peter')
Hello Peter
int = __builtin__.int
def complement_base(base):
    """Returns the Watson-Crick complement of a base."""
    if base in 'Aa':
        return 'T'
    elif base in 'Tt':
        return 'A'
    elif base in 'Gg':
        return 'C'
    elif base in 'Cc':
        return 'G'
    else:
        return 'N'
complement_base('N')
'N'
complement_base('c')
'G'
seq = 'GACAGACTCCATGNNNNNNNCACGTGGGUATCTGTC'
complement_base(seq)
'N'
def complement(seq):
    """Complement an entire sequence of DNA"""
    complement_seq = ''
    for base in seq:
        complement_seq += complement_base(base)
    return complement_seq
complement(seq)
'CTGTCTGAGGTACNNNNNNNGTGCACCCNTAGACAG'
list(reversed(seq))
['C', 'T', 'G', 'T', 'C', 'T', 'A', 'U', 'G', 'G', 'G', 'T', 'G', 'C', 'A', 'C', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'G', 'T', 'A', 'C', 'C', 'T', 'C', 'A', 'G', 'A', 'C', 'A', 'G']
reversed(seq)
<reversed at 0x7fda3860b970>
next(reversed(seq))
'C'
seq[::-1]
'CTGTCTAUGGGTGCACNNNNNNNGTACCTCAGACAG'
def rev_complement(seq):
    """Reverse complement an entire sequence of DNA"""
    complement_seq = ''
    for base in reversed(seq):
        complement_seq += complement_base(base)
    return complement_seq
seq
'GACAGACTCCATGNNNNNNNCACGTGGGUATCTGTC'
rev_complement(seq)
'GACAGATNCCCACGTGNNNNNNNCATGGAGTCTGTC'
seq = 'GATACCA'
rev_complement(seq)
'TGGTATC'
complement_base()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[63], line 1
----> 1 complement_base()

TypeError: complement_base() missing 1 required positional argument: 'base'
def complement_base(base, moltype='DNA'):
    """Returns the Watson-Crick complement of a base.
    base: str - a single character base
    moltype: str - can be 'DNA' or 'RNA', the type of biomolecule
    """
    print('moltype is', moltype)
    if base in 'Aa':
        if moltype == 'DNA':
            return 'T'
        elif moltype == 'RNA':
            return 'U'
    elif base in 'TtUu':
        return 'A'
    elif base in 'Gg':
        return 'C'
    elif base in 'Cc':
        return 'G'
    else:
        return 'N'
complement_base('U', moltype='RNA')
moltype is RNA
'A'
complement_base('A')
moltype is DNA
'T'
complement_base('A', moltype='DNA')
moltype is DNA
'T'
?print
def greet(*names):
    print("names:", type(names))
    
greet('Peter')
names: <class 'tuple'>
def greet(*names):
    for name in names:
        print('Hello', name)
    
greet('Peter')
Hello Peter
greet('Peter', 'Thabo')
Hello Peter
Hello Thabo