Title: Introduction to Python
1Introduction to Python
2Recursive 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)
3Recursive Example
factorial(5)
120
24
6
2
1
4Recursion
- 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
5Uses
def inorder(node) if node None
return inorder(node.left) print
node.value, inorder(node.right) 1 3 6 7 9 10 12
15
6Uses
def preorder(node) if node None
return print node.value, inorder(node.left)
inorder(node.right) 10 3 1 7 6 9 12 15
7Uses
- 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)
8Uses
- Puzzles
- Tower of Hanoi
- Fractals
- Koch Snowflake
9Exceptions
- 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
10Exceptions
- 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
11Exceptions
- 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
12Exceptions
- Handling Syntax
- try
-
- except
-
- except
-
- except
-
- else
-
13Exceptions
- Example
- try
- x 1 / 0
- except ZeroDivisionError
- print 'Divide by zero error'
- Divide by zero error
14Exceptions
- Example
- try
- x 1 / 0
- except IOError
- print 'Input/Output error'
- except
- print 'Unknown error'
-
- Unknown error
15Exceptions
- 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
16Exceptions
- Example
- try
- x 1 / 0
- except Exception
- print 'Exception caught'
-
- Exception caught
17Exceptions
- 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
18Exceptions
- Example
- try
- x 1 / 0
- except Exception
- print 'Exception caught'
-
- Exception caught
19Command 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'