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

50 %
50 %
Information about Python Programming - IV. Program Components (Functions, Classes,...
Technology

Published on November 24, 2013

Author: ranelpadon

Source: slideshare.net

Description

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

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

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

THE TARGET SCENARIO

THE BUILDING BLOCKS

DIVISION OF LABOR

DIVISION OF LABOR

THE ASSEMBLY LINE

DIVIDE-AND-CONQUER every problem can be broken down into smaller/more manageable sub-problems

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

PYTHON PROGRAM COMPONENTS  functions  classes  modules  collection of functions & classes  packages  collection of modules

PYTHON PROGRAM COMPONENTS Function Package Class Module

FUNCTIONS collection or block of statements that you can execute whenever and wherever you want in the program

FUNCTIONS

FUNCTIONS

WHY FUNCTIONS?  avoids duplicating code snippets  saves typing  easier to change the program later

PYTHON MODULES groups related functions & classes

PYTHON MODULES       mathematical calculations string manipulations character manipulations web programming graphics programming …

PYTHON MODULES STANDARD LIBRARY  collection of Python modules  found in C:Python27Lib folder

PYTHON MODULES STANDARD LIBRARY * found in C:Python27Lib

PYTHON MODULES STANDARD LIBRARY  familiarize yourself with the Standard Library  don’t reinvent the wheel

PYTHON PACKAGES  groups related modules  code calling in several locations  prevents name collision

PYTHON PACKAGES (site-packages)

PYTHON PACKAGE (numpy)

PYTHON SUB-PACKAGE (polynomial) showing the Modules of the polynomial package.

PYTHON MODULE (polynomial.py) showing a part of the content of the polynomial module

PYTHON FUNCTIONS * groups related modules * code calling in several locations * prevents name collision

PYTHON FUNCTIONS * groups related modules * code calling in several locations * prevents name collision

PYTHON FUNCTIONS (math MODULE) * groups related modules * code calling in several locations * prevents name collision

PYTHON FUNCTIONS (math MODULE)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (DEFINITION)

PYTHON FUNCTIONS (return KEYWORD)

PYTHON FUNCTIONS (return KEYWORD)

PYTHON FUNCTIONS (return TUPLES)

PYTHON FUNCTIONS def sumDiff(x, y): return (x+y), (x-y) sum, diff = sumDiff(2, 3) print sum print diff

PYTHON FUNCTIONS

PYTHON FUNCTIONS (random MODULE)

PYTHON FUNCTIONS (random MODULE)

PYTHON FUNCTIONS (random MODULE)

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

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

VARIABLE SCOPE

VARIABLE SCOPE

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

PYTHON FUNCTIONS (PARAMS vs ARGS) Function Parameters Function Arguments

PYTHON FUNCTIONS (ARGUMENTS) Required/Mandatory Arguments passed to a function in correct positional order

PYTHON FUNCTIONS (ARGUMENTS) Keyword Arguments the caller identifies the arguments by the parameter name as keywords, with/without regard to positional order

PYTHON FUNCTIONS (ARGUMENTS) Default/Optional Arguments assumes a default value if a value is not provided in the function call for that argument.

PYTHON FUNCTIONS (ARGUMENTS) Variable-Length Arguments can handle no-argument, 1-argument, or many-arguments function calls

PYTHON FUNCTIONS (ARGUMENTS) Combining the Argument Types

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

NAMESPACES (Importing a Package) current snapshot of the default and imported namespaces

NAMESPACES (Importing Packages)

NAMESPACES (Importing Functions)

NAMESPACES (Importing Functions)

NAMESPACES (Built-In Functions)

NAMESPACES (Importing All Functions)

NAMESPACES (Importing All Functions)

NAME BINDING they are used for better readability, faster coding, or simply just for convenience

NAME BINDING

NAME BINDING

NAME BINDING

NAME BINDING

HANDLING MODULES

HANDLING MODULES (Error?)

HANDLING MODULES (Debug)

HANDLING MODULES (Correct Import)

HANDLING MODULES (Correct Import)

HANDLING MODULES (Alternative)

HANDLING MODULES (Will Work)

HANDLING MODULES (Will Not Work) It could not locate the sum() function.

HANDLING PACKAGES The __init__.py files are required to make Python treat the directories as containing packages

HANDLING PACKAGES (The GE file) The geodetic_engineering.py file/module, located in the engineering and diliman parent folders/packages.

HANDLING PACKAGES (Will Not Work) The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.

HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.

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.

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

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.

Divide-and-Conquer is Powerful!

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. 

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Learn Python The Hard Way

Classes Are Like Modules. You can think about a module as a specialized dictionary that can store Python code so you can access it with the . operator.
Read more

Python - Chapter 28. Modules - On-line Linux and Open ...

Part IV. Components, Modules and Packages ... group Python classes, functions and ... contains Python programming. A module can be brought into ...
Read more

6. Modules — Python 2.7.11 documentation

6.2. Standard Modules¶ Python comes with a library of standard modules, described in a separate document, the Python Library Reference (“Library ...
Read more

Modules and Packages - Learn Python - Free Interactive ...

Modules and Packages. Modules in Python are simply Python files with the .py extension, ... When we find the function in the module we want to use, ...
Read more

Welcome to Python.org

The core of extensible programming is defining functions. Python allows mandatory and optional arguments, ... and advance the Python programming language, ...
Read more

Python-Tutorial: Modularierung mit Modulen

Modularisierung in Python: Module und Pakete, ... Modular Programming and Modules Python 2.x ... All our Python classes are available in English as well: ...
Read more

Overview — Python 3.5.1 documentation

Python 3.5.1 documentation. ... Modules installing from the Python Package Index ... to all modules. General Index all functions, classes, ...
Read more

PyPI - the Python Package Index : Python Package Index

... the Python Package Index. The Python Package Index is a repository of software for the Python programming language. ... A Python Steganography module.
Read more