Python(Functional abstraction)

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()

image

  • 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.
    image

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值