Introduction to Python - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Introduction to Python

Description:

Exceptions are thrown (or raised) and caught ... An exception will be caught by any type higher up in the hierarchy. Exceptions. Exception ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 20
Provided by: chadh8
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Python


1
Introduction to Python
  • Recursion
  • Exceptions

2
Recursive Function
  • Definition
  • A function which calls itself
  • Basic Example
  • Calculate factorial
  • Example
  • def factorial(n)
  • if n 1
  • return 1
  • else
  • return nfactorial(n-1)

3
Recursive Example
  • Factorial

factorial(5)
120
24
6
2
1
4
Recursion
  • Key parts
  • Termination Condition
  • Call to itself

def factorial(n) if n 1 return
1 else return nfactorial(n-1)
Termination Condition
Call to itself
5
Uses
  • Traversing Trees

def inorder(node) if node None
return inorder(node.left) print
node.value, inorder(node.right) 1 3 6 7 9 10 12
15
6
Uses
  • Traversing Trees

def preorder(node) if node None
return print node.value, inorder(node.left)
inorder(node.right) 10 3 1 7 6 9 12 15
7
Uses
  • Sorting
  • Quick Sort
  • Bubble Sort

def quicksort(values, low, high) if (high
low) pivot partition(values, low,
high) quicksort(values, low, pivot-1) quicksor
t(values, pivot1, high)
8
Uses
  • Puzzles
  • Tower of Hanoi
  • Fractals
  • Koch Snowflake

9
Exceptions
  • Definition
  • Errors detected during execution
  • Basic Example
  • Divide by zero
  • 1 / 0
  • Traceback (most recent call last)
  • File "", line 1, in ?
  • 1 / 0
  • ZeroDivisionError integer division or modulo by
    zero

10
Exceptions
  • Motivation
  • Move error handling code away from main code
    block
  • Deal with exceptional cases separately
  • How it works
  • Exceptions are thrown (or raised) and caught
  • Control exits the current code block when the
    exception is thrown
  • An exception can then be caught by a catching
    code block

11
Exceptions
  • Throwing
  • Many common operations may throw an exception
  • List index out of bounds
  • Invalid type conversions
  • Exceptions can be thrown manually using the raise
    keyword
  • raise ValueError, "Bad Value
  • Catching
  • Thrown exceptions must be caught
  • If the exception is never caught the progam will
    terminate

12
Exceptions
  • Handling Syntax
  • try
  • except
  • except
  • except
  • else

13
Exceptions
  • Example
  • try
  • x 1 / 0
  • except ZeroDivisionError
  • print 'Divide by zero error'
  • Divide by zero error

14
Exceptions
  • Example
  • try
  • x 1 / 0
  • except IOError
  • print 'Input/Output error'
  • except
  • print 'Unknown error'
  • Unknown error

15
Exceptions
  • Types of Exceptions
  • There is a hierarchy of exceptions
  • All built-in exceptions are derived from
    Exception
  • An exception will be caught by any type higher up
    in the hierarchy

Exception
StandardError
ArithmeticError
ZeroDivisionError
SystemExit
ValueError
OverflowError
StopIteration
LookupError
IndexError
KeyError
16
Exceptions
  • Example
  • try
  • x 1 / 0
  • except Exception
  • print 'Exception caught'
  • Exception caught

17
Exceptions
  • Propagation
  • If no proper except block can be found in the
    current block, the exception propagates back to
    the calling function
  • def func1()
  • try
  • a 1 / 0
  • except ValueError
  • print 'first'
  • def func2()
  • try
  • func1()
  • except
  • print 'second'
  • func2()
  • second

18
Exceptions
  • Example
  • try
  • x 1 / 0
  • except Exception
  • print 'Exception caught'
  • Exception caught

19
Command Line Arguments
  • Variables can be passed to the program via
    command line arguments
  • Values are stored in sys.argv
  • First value is sys.argv is always the program
    name
  • Preferable to prompting user for input after
    program has begun
  • Example

Command Line
test.py
  • python test.py
  • 'test.py'
  • python test.py 2 3 words
  • 'test.py', '2', '3', 'words'
Write a Comment
User Comments (0)
About PowerShow.com