Libraries, modules, namespaces
- Library is a generic term for a collection of (useful) functions, data structures, etc.
- In python, libraries are called modules.
- Importing a module, eg:
import math
import robot
- Imported names are prefixed with the module name, as in math.pi, robot.lift up, etc.
- How does python find modules?
- Standard modules (e.g., math) are installed in a specific location on the file system.
- Non-standard modules (e.g., robot) must be in the current working directory (cwd).
Functional abstraction
- In programming, a function (also known as “procedure” or “subroutine”) is a piece of the program that is given a name.
- The function is called by its name.
- A function is defined once, but can be called any number of times. - Why use functions?
- Abstraction: To use a function, we only need to know what it does, not how.
- Break a complex problem into smaller parts.
Function definition in python
def move_to_next_stack():
robot.drive_right()
robot.drive_right()
- def is a python keyword (“reserved word”).
- The function’s name is followed by a pair of parentheses and a colon.
- Inside the parentheses are the function’s parameters (more on this in coming lectures). - The function suite is the sequence of statements that will be executed when the function is called.
- In python, a suite is delimited by indentation.
- All statements in the suite must be preceded by the same number of spaces/tabs(standard is 4 spaces).
Syntax
- The syntax of a (programming) language is the rules that define what is a valid program.
- A python program is a sequence of statements:
- defining a function:
def move_to_next_stack():
robot.drive_right()
robot.drive_right()
- calling a function:
move twice()
robot.lift_up()
- importing a module:
import math
Whitespace
- Spaces, tabs and end-of-line are known as whitespace.
- The whitespace before a statement is called indentation.
- In python, whitespace has two special roles:
- end-of-line marks the end of a statement(some exceptions, more later in the course);
- indentation defines the extent of a suite of statements. - Other than this, whitespace is ignored.
Permitted names in python
- A function name in python may contain letters, numbers and underscores ( ), but must begin with a letter or undescore.
- Reserved words cannot be used as names.
- Names are case sensitive: upper and lower case letters are not the same.
Comments
- A hash sign (#) marks the beginning of a comment; it continues to end-of-line.
- Comments are ignored by the interpreter.
- Comments are for people.
- Use comments to state what is not obvious. - If it was hard to write, it’s probably hard to read. Add a comment. (Punch & Enbody, Rule 6)
Testing and debugging
- How do we know our program works?
- Specify the assumptions under which the program (or function) is meant to work.
- Test it with a variety of cases that fall under those assumptions.
- Particularly, “edge cases”. - Some common errors:
- SyntaxError:
You have broken the rules of python syntax.
- NameError or AttributeError:
You have used a (function) name that doesn’t exist. Check for typos.
- IndentationError:
Too much or too little indentation.
* - All statements in a function suite must have the same indentation.
* - All statements outside function definitions must have no indentation.