Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

84
PYTHON PROGRAMMING Engr. Ranel O. Padon IV. Program Components

description

Feel free to download the material for offline viewing later, better images' resolutions, and crispier fonts.

Transcript of Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

Page 1: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PROGRAMMING

Engr. Ranel O. Padon

IV. Program Components

Page 2: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PROGRAMMING TOPICS

I • Introduction to Python Programming

II • Python Basics

III • Controlling the Program Flow

IV • Program Components: Functions, Classes, Modules, and Packages

V • Sequences (List and Tuples), and Dictionaries

VI • Object-Based Programming: Classes and Objects

VII • Customizing Classes and Operator Overloading

VIII • Object-Oriented Programming: Inheritance and Polymorphism

IX • Randomization Algorithms

X • Exception Handling and Assertions

XI • String Manipulation and Regular Expressions

XII • File Handling and Processing

XIII • GUI Programming Using Tkinter

Page 3: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

THE TARGET SCENARIO

Page 4: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

THE BUILDING BLOCKS

Page 5: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

DIVISION OF LABOR

Page 6: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

DIVISION OF LABOR

Page 7: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

THE ASSEMBLY LINE

Page 8: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

DIVIDE-AND-CONQUER

every problem can be broken down into smaller/more

manageable sub-problems

Page 9: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

DIVIDE-AND-CONQUER ALGORITHM

most computer programs that solve real-world problems are

complex/large

the best way to develop and maintain a large program is to

construct it from smaller pieces or components

Page 10: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PROGRAM COMPONENTS

functions

classes

modules

collection of functions & classes

packages

collection of modules

Page 11: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

Package

Module

PYTHON PROGRAM COMPONENTS

Function

Class

Page 12: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

FUNCTIONS

collection or block of statements that you can execute

whenever and wherever you want in the program

Page 13: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

FUNCTIONS

Page 14: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

FUNCTIONS

Page 15: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

WHY FUNCTIONS?

avoids duplicating code snippets

saves typing

easier to change the program later

Page 16: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULES

groups related functions & classes

Page 17: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULES

mathematical calculations

string manipulations

character manipulations

web programming

graphics programming

Page 18: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULES

STANDARD LIBRARY

collection of Python modules

found in C:\Python27\Lib folder

Page 19: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULES

STANDARD LIBRARY

* found in C:\Python27\Lib

Page 20: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULES

STANDARD LIBRARY

familiarize yourself with the Standard Library

don’t reinvent the wheel

Page 21: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PACKAGES

groups related modules

code calling in several locations

prevents name collision

Page 22: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PACKAGES (site-packages)

Page 23: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON PACKAGE (numpy)

Page 24: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON SUB-PACKAGE (polynomial)

showing the Modules of the polynomial package.

Page 25: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON MODULE (polynomial.py)

showing a part of the content of the polynomial module

Page 26: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS

* groups related modules

* code calling in several locations

* prevents name collision

Page 27: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS

* groups related modules

* code calling in several locations

* prevents name collision

Page 28: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (math MODULE)

* groups related modules

* code calling in several locations

* prevents name collision

Page 29: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (math MODULE)

Page 30: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (DEFINITION)

Page 31: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (DEFINITION)

Page 32: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (DEFINITION)

Page 33: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (return KEYWORD)

Page 34: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (return KEYWORD)

Page 35: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (return TUPLES)

Page 36: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS

def sumDiff(x, y):

return (x+y), (x-y)

sum, diff = sumDiff(2, 3)

print sum

print diff

Page 37: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS

Page 38: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (random MODULE)

Page 39: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (random MODULE)

Page 40: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (random MODULE)

Page 41: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

VARIABLE SCOPE

all variables in a program may not be accessible at all locations

in that program

the scope of a variable determines the portion of the program

where you can access a particular variable

Page 42: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

VARIABLE SCOPE

variables that are defined inside a function body have a local

scope, and those defined outside have a global scope

inside a function, a local variable takes precedence over a

global variable of the same name

possible workaround:

change the variable names to avoid collision

Page 43: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

VARIABLE SCOPE

Page 44: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

VARIABLE SCOPE

Page 45: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

You can call a function by using the

following types of formal arguments:

Required Arguments

Default Arguments

Keyword Arguments

Variable-Length Arguments

Page 46: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (PARAMS vs ARGS)

Function Parameters

Function Arguments

Page 47: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

Required/Mandatory Arguments

passed to a function in correct positional order

Page 48: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

Keyword Arguments

the caller identifies the arguments by the parameter name as

keywords, with/without regard to positional order

Page 49: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

Default/Optional Arguments

assumes a default value if a value is not provided in the function

call for that argument.

Page 50: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

Variable-Length Arguments

can handle no-argument, 1-argument, or many-arguments function

calls

Page 51: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PYTHON FUNCTIONS (ARGUMENTS)

Combining the Argument Types

Page 52: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES

refers to the current snapshot of loaded

names/variables/identifiers/folders

functions must be loaded into the memory before you could call

them, especially when calling external functions/libraries

Page 53: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing a Package)

current snapshot of the default

and imported namespaces

Page 54: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing Packages)

Page 55: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing Functions)

Page 56: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing Functions)

Page 57: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Built-In Functions)

Page 58: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing All Functions)

Page 59: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAMESPACES (Importing All Functions)

Page 60: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAME BINDING

they are used for better readability, faster coding,

or simply just for convenience

Page 61: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAME BINDING

Page 62: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAME BINDING

Page 63: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAME BINDING

Page 64: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

NAME BINDING

Page 65: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES

Page 66: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Error?)

Page 67: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Debug)

Page 68: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Correct Import)

Page 69: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Correct Import)

Page 70: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Alternative)

Page 71: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Will Work)

Page 72: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING MODULES (Will Not Work)

It could not locate the sum() function.

Page 73: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES

The __init__.py files are required to make Python treat the

directories as containing packages

Page 74: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (The GE file)

The geodetic_engineering.py file/module, located in the

engineering and diliman parent folders/packages.

Page 75: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Will Not Work)

The demo.py file importing the geodetic_engineering.py file/module.

Page 76: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

Page 77: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

Page 78: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

Page 79: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Will Work)

The demo.py file importing the geodetic_engineering.py file/module.

Page 80: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

HANDLING PACKAGES (Summary)

IMPORTATION INVOCATION

import p1.p2.m p1.p2.m.f1()

from p1.p2 import m m.f1()

from p1.p2.m import f1 f1()

Where p means package, m means module, f means a function/class.

Page 81: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PRACTICE EXERCISE 1

Compute the factorial of a number n:

• n is a number inputted by the user

• make a factorial function and call it to solve

the factorial of n

Page 82: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

PRACTICE EXERCISE 2

Compute the sum of a number range, say, from a to b, inclusive:

• a, b are numbers inputted by the user

• make a sum_range(a, b) function and call it to solve the

sum of all numbers from a to b, including a and b.

Page 83: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

Divide-and-Conquer is Powerful!

Page 84: Python Programming - IV. Program Components (Functions, Classes, Modules, Packages)

REFERENCES

Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

Disclaimer: Most of the images/information used here have no proper source

citation, and I do not claim ownership of these either. I don’t want to reinvent the

wheel, and I just want to reuse and reintegrate materials that I think are useful or

cool, then present them in another light, form, or perspective. Moreover, the

images/information here are mainly used for illustration/educational purposes

only, in the spirit of openness of data, spreading light, and empowering people

with knowledge.