An Introduction to Python Part IV - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

An Introduction to Python Part IV

Description:

Creating 2-D Lists. To create a 2-D list L, with C columns and R ... b.spam('gumby') 6/23/05. Introduction to Python Part IV. 7. Python Documentation Sources ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 12
Provided by: lanct9
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Python Part IV


1
An Introduction to Python Part IV
  • Dr. Nancy Warter-Perez
  • June 23, 2005

2
Overview
  • Creating 2-D Lists (already covered)
  • Scopes
  • Modules
  • Doc Strings
  • Debugging
  • Project Development Time

3
Creating 2-D Lists
  • To create a 2-D list L, with C columns and R rows
    initialized to 0
  • L empty 2-Dlist
  • L 0 for col in range(C) for row in range(R)
  • To assign the value 5 to the element at the 2nd
    row and 3rd column of L
  • L23 5

4
Scopes
  • Scopes divine the visibility of a variable
  • Variables defined outside of a function are
    visible to all of the functions within a module
    (file)
  • Variables defined within a function are local to
    that function
  • To make a variable that is defined within a
    function global, use the global keyword

Ex 2 x 5 def fnc() global x x 2
print x, fnc() print x gtgtgt 2 2
Ex 1 x 5 def fnc() x 2 print
x, fnc() print x gtgtgt 2 5
5
Modules
  • Why use?
  • Code reuse
  • System namespace partitioning (avoid name
    clashes)
  • Implementing shared services or data
  • How to structure a Program
  • One top-level file
  • Main control flow of program
  • Zero or more supplemental files known as modules
  • Libraries of tools

6
Modules - Import
  • Import used to gain access to tools in modules
  • Ex
  • contents of file b.py
  • def spam(text)
  • print text, 'spam'
  • contents of file a.py
  • import b
  • b.spam('gumby')

7
Python Documentation Sources
  • comments In-file documentation
  • The dir function Lists of attributes
    available on objects
  • Docstrings__doc__ In-file documentation
  • attached to objects

8
Dir and DocString Example
  • Ex b.py
  • Internal comment
  • """Module Docstring comment """
  • def fn()
  • """Function Docstring comment """
  • gtgtgt import b
  • gtgtgt dir(b)
  • '__builtins__', '__doc__', '__file__',
    '__name__', 'fn'
  • gtgtgt print b.__doc__
  • Module Docstring comment
  • gtgtgt print b.fn.__doc__
  • Function Doctring comment

9
Debugging
  • Two types of bugs
  • Syntax errors easy to find
  • Logical errors harder to find
  • Can be a problem in your algorithm
  • Can be a problem in your coding
  • Debugging a program is like solving a puzzle
  • Must first understand what your program is
    supposed to do
  • Logically trace what is happening in your program
  • What do you expect to happen versus what happened
  • Follow the flow of data
  • Follow the flow of control

10
Debugging Tools/Methods
  • Can use print statements to manually debug
  • Can use debugger in PythonWin
  • In Class Example

11
Workshop
  • Separate your LCS functions into a different
    module
  • Import the module and call the functions from
    within your while loop (continually prompting the
    user if they want to continue)
  • Trace through your program
  • Use print statements to view the score and trace
    back matrices as they are formed
  • Using the debugger to view the score and trace
    back matrices
Write a Comment
User Comments (0)
About PowerShow.com