Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
PYTHON PROGRAMMING
IV. Program Components

Engr. Ranel O. Padon
PYTHON PROGRAMMING TOPICS
I

• Introduction to Python Programming

II

• Python Basics

III

• Controlling the Program Flo...
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 devel...
PYTHON PROGRAM COMPONENTS

 functions
 classes
 modules
 collection of functions & classes
 packages
 collection of ...
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
graphi...
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 ...
VARIABLE SCOPE
variables that are defined inside a function body have a local
scope, and those defined outside have a glob...
VARIABLE SCOPE
VARIABLE SCOPE
PYTHON FUNCTIONS (ARGUMENTS)

You can call a function by using the
following types of formal arguments:





Required ...
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...
PYTHON FUNCTIONS (ARGUMENTS)
Default/Optional Arguments
assumes a default value if a value is not provided in the function...
PYTHON FUNCTIONS (ARGUMENTS)
Variable-Length Arguments
can handle no-argument, 1-argument, or many-arguments function
call...
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...
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 fo...
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.p...
PRACTICE EXERCISE 1

Compute the factorial of a number n:
• n is a number inputted by the user
• make a factorial function...
PRACTICE EXERCISE 2

Compute the sum of a number range, say, from a to b, inclusive:
• a, b are numbers inputted by the us...
Divide-and-Conquer is Powerful!
REFERENCES
 Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

 Disclaimer: Most of the images/inf...
Nächste SlideShare
Wird geladen in …5
×

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

6.002 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Technologie
  • Hi All, We are planning to start new devops online batch on this week... If any one interested to attend the demo please register in our website... For this batch we are also provide everyday recorded sessions with Materials. For more information feel free to contact us : siva@keylabstraining.com. For Course Content and Recorded Demo Click Here : http://www.keylabstraining.com/devops-online-training-tutorial
       Antworten 
    Sind Sie sicher, dass Sie …  Ja  Nein
    Ihre Nachricht erscheint hier

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

  1. 1. PYTHON PROGRAMMING IV. Program Components Engr. Ranel O. Padon
  2. 2. 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
  3. 3. THE TARGET SCENARIO
  4. 4. THE BUILDING BLOCKS
  5. 5. DIVISION OF LABOR
  6. 6. DIVISION OF LABOR
  7. 7. THE ASSEMBLY LINE
  8. 8. DIVIDE-AND-CONQUER every problem can be broken down into smaller/more manageable sub-problems
  9. 9. 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
  10. 10. PYTHON PROGRAM COMPONENTS  functions  classes  modules  collection of functions & classes  packages  collection of modules
  11. 11. PYTHON PROGRAM COMPONENTS Function Package Class Module
  12. 12. FUNCTIONS collection or block of statements that you can execute whenever and wherever you want in the program
  13. 13. FUNCTIONS
  14. 14. FUNCTIONS
  15. 15. WHY FUNCTIONS?  avoids duplicating code snippets  saves typing  easier to change the program later
  16. 16. PYTHON MODULES groups related functions & classes
  17. 17. PYTHON MODULES       mathematical calculations string manipulations character manipulations web programming graphics programming …
  18. 18. PYTHON MODULES STANDARD LIBRARY  collection of Python modules  found in C:Python27Lib folder
  19. 19. PYTHON MODULES STANDARD LIBRARY * found in C:Python27Lib
  20. 20. PYTHON MODULES STANDARD LIBRARY  familiarize yourself with the Standard Library  don’t reinvent the wheel
  21. 21. PYTHON PACKAGES  groups related modules  code calling in several locations  prevents name collision
  22. 22. PYTHON PACKAGES (site-packages)
  23. 23. PYTHON PACKAGE (numpy)
  24. 24. PYTHON SUB-PACKAGE (polynomial) showing the Modules of the polynomial package.
  25. 25. PYTHON MODULE (polynomial.py) showing a part of the content of the polynomial module
  26. 26. PYTHON FUNCTIONS * groups related modules * code calling in several locations * prevents name collision
  27. 27. PYTHON FUNCTIONS * groups related modules * code calling in several locations * prevents name collision
  28. 28. PYTHON FUNCTIONS (math MODULE) * groups related modules * code calling in several locations * prevents name collision
  29. 29. PYTHON FUNCTIONS (math MODULE)
  30. 30. PYTHON FUNCTIONS (DEFINITION)
  31. 31. PYTHON FUNCTIONS (DEFINITION)
  32. 32. PYTHON FUNCTIONS (DEFINITION)
  33. 33. PYTHON FUNCTIONS (return KEYWORD)
  34. 34. PYTHON FUNCTIONS (return KEYWORD)
  35. 35. PYTHON FUNCTIONS (return TUPLES)
  36. 36. PYTHON FUNCTIONS def sumDiff(x, y): return (x+y), (x-y) sum, diff = sumDiff(2, 3) print sum print diff
  37. 37. PYTHON FUNCTIONS
  38. 38. PYTHON FUNCTIONS (random MODULE)
  39. 39. PYTHON FUNCTIONS (random MODULE)
  40. 40. PYTHON FUNCTIONS (random MODULE)
  41. 41. 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
  42. 42. 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
  43. 43. VARIABLE SCOPE
  44. 44. VARIABLE SCOPE
  45. 45. 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
  46. 46. PYTHON FUNCTIONS (PARAMS vs ARGS) Function Parameters Function Arguments
  47. 47. PYTHON FUNCTIONS (ARGUMENTS) Required/Mandatory Arguments passed to a function in correct positional order
  48. 48. PYTHON FUNCTIONS (ARGUMENTS) Keyword Arguments the caller identifies the arguments by the parameter name as keywords, with/without regard to positional order
  49. 49. PYTHON FUNCTIONS (ARGUMENTS) Default/Optional Arguments assumes a default value if a value is not provided in the function call for that argument.
  50. 50. PYTHON FUNCTIONS (ARGUMENTS) Variable-Length Arguments can handle no-argument, 1-argument, or many-arguments function calls
  51. 51. PYTHON FUNCTIONS (ARGUMENTS) Combining the Argument Types
  52. 52. 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
  53. 53. NAMESPACES (Importing a Package) current snapshot of the default and imported namespaces
  54. 54. NAMESPACES (Importing Packages)
  55. 55. NAMESPACES (Importing Functions)
  56. 56. NAMESPACES (Importing Functions)
  57. 57. NAMESPACES (Built-In Functions)
  58. 58. NAMESPACES (Importing All Functions)
  59. 59. NAMESPACES (Importing All Functions)
  60. 60. NAME BINDING they are used for better readability, faster coding, or simply just for convenience
  61. 61. NAME BINDING
  62. 62. NAME BINDING
  63. 63. NAME BINDING
  64. 64. NAME BINDING
  65. 65. HANDLING MODULES
  66. 66. HANDLING MODULES (Error?)
  67. 67. HANDLING MODULES (Debug)
  68. 68. HANDLING MODULES (Correct Import)
  69. 69. HANDLING MODULES (Correct Import)
  70. 70. HANDLING MODULES (Alternative)
  71. 71. HANDLING MODULES (Will Work)
  72. 72. HANDLING MODULES (Will Not Work) It could not locate the sum() function.
  73. 73. HANDLING PACKAGES The __init__.py files are required to make Python treat the directories as containing packages
  74. 74. HANDLING PACKAGES (The GE file) The geodetic_engineering.py file/module, located in the engineering and diliman parent folders/packages.
  75. 75. HANDLING PACKAGES (Will Not Work) The demo.py file importing the geodetic_engineering.py file/module.
  76. 76. HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.
  77. 77. HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.
  78. 78. HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.
  79. 79. HANDLING PACKAGES (Will Work) The demo.py file importing the geodetic_engineering.py file/module.
  80. 80. 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.
  81. 81. 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
  82. 82. 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.
  83. 83. Divide-and-Conquer is Powerful!
  84. 84. 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. 

×