Functions & Keywords - KeithChamberlain/PythonJourney GitHub Wiki

A Note on Spaces...

Spaces, not tabs. If I use tabs, setup my tabs to insert exactly 4 spaces! Lest the python developer community erupt on me!

This one is funny. Unlike the arguments about how to pronounce the word 'tuple', there doesn't seem to be any debate about this one. I was on slack at Galvanize Data Science prep discussing the pronunciation of 'tuple' and a developer dropped in with 'what about spaces versus tabs?', and then python coders started saying things like 'no you didn't!' and dropping images of nuclear explosions in the thread. This one is a sore topic for python developers. Inevitably, there will be some junior developer that doesn't know better that tries to use tabs and wonders why their code won't execute. Worse still, everything will look just fine! Thus, by all accounts the code should execute when it is run, but it doesn't! So, yeah, spaces, not tabs.

PythonJourney: Functions & Keywords

Functions and keywords are covered before anything else because they are essential to doing anything useful in python. One of the worst things I have run into was not knowing a simple keyword, such as in to test membership, that I should have known. I think these are so important that I document them before covering the data structures in python. Some data structures will come up implicitly, however, they are not the main focus.

Python Keywords

First of all... RTFM. The manual may be terse and make little sense at first, but that's why I'm here. Just take a moment and read a few specs.

  • Search google: "python keywords" for python 3.6 & above
  • Briefly preview any unfamiliar ones.
  • Know the following
    • Boolean keywords: True, False, and, or, not
    • Function keywords: def, return, continue, break, None, *args, **kwargs, & pass (only because I'll see it)
    • Control flow keywords: if, elif, else, if else, try, except
    • Loop keywords: for, while
    • Importing modules: from, import, as

keyword Library

A list of python keywords supported in one's version of python can be listed out in the following code:

import keyword
print(keyword.kwlist)

Functions

Functions are the bread & butter of python. They permit abstraction and reusability of code. Functions are defined with the def keyword, followed by a function name. Then, in parens, a list of attributes. After the parens, the colun delimits the end of the definition. Note that when the function is called, and the attributes are supplied in the function call, they are then called parameters. Everything inside the function is indented (usually) 4 spaces for the base level, whereas flow control and loops will cause further indention. The function is terminated with return statements or it returns the None type (a special Null indicator) by default.

# A Function skeleton
def functionName(variable1, variable2="default value"):
    # do stuff with variable1 and variable2
    return "Hello"

To call the function, I must use something that outputs, to the console, for example, such as print(). The above function prints "Hello" and doesn't use the positional arguments, though it does require a value for variable1.

In functionName(), variable1 is required and has no defaults. Variable2 is not required and has a default. Required variables must come before non-required variables in python. I can use the * or ** keywords, followed by a name of my choosing, in the arguments list to accept indefinite lists of * unnamed, or ** named arguments.

print(functionName("Hi")) # Note that variable2 can be omitted because of the default value

Hello

# This function returns the factorial of an integer from 1 to n inclusive.
def factorial(n=3):
    product = 1
    # range(from, to, by); start at 2 since already initialized at 1
    for i in range(2, n+1, 1):
        product *= i
    return product
print(factorial(5))

120