23
CSC 401 NOTES LECTURE #4 Yosef Mendelsohn String Formatting Methods There are many useful techniques for formatting output so that it appears in ways that are meaningful. The techniques for doing so take a little getting used to, but it is definitely worth getting to know the basics. We can exert some control over the apperance of output using the format() method. It can be a bit confusing to describe, so we won't spend much time doing so now. Instead, we will demonstrate with a few examples. Your textbook will, of course, give you plenty of detail to explain how it works. Briefly, the format() method is invoked by a string that represents the format of the output. The arguments to the format function are the objects to be printed. Study the examples below: def format_strings_practice(): fn1 = 'Bob' ln1 = 'Smith' fn2 = 'Lisa' ln2 = 'Chen' print( 'Your first name is {0}'.format(fn1) ) print('Your full name is: {1}, {0}.'.format(fn2, ln2)) Outputs: Your first name is Bob Your full name is: Chen, Lisa.

String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

Embed Size (px)

Citation preview

Page 1: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

CSC 401 NOTESLECTURE #4

Yosef Mendelsohn

String Formatting Methods There are many useful techniques for formatting output so that it appears in ways that are meaningful. The techniques for doing so take a little getting used to, but it is definitely worth getting to know the basics.

We can exert some control over the apperance of output using the format() method.

It can be a bit confusing to describe, so we won't spend much time doing so now. Instead, we will demonstrate with a few examples. Your textbook will, of course, give you plenty of detail to explain how it works.

Briefly, the format() method is invoked by a string that represents the format of the output. The arguments to the format function are the objects to be printed.

Study the examples below:

def format_strings_practice(): fn1 = 'Bob' ln1 = 'Smith' fn2 = 'Lisa' ln2 = 'Chen'

print( 'Your first name is {0}'.format(fn1) ) print('Your full name is: {1}, {0}.'.format(fn2, ln2))

Outputs:Your first name is BobYour full name is: Chen, Lisa.

>>> day = 25>>> month = 1>>> year = 2017

>>> '{1}/{0}/{2}'.format(day, month, year)'1/25/2017'

#Let’s format using the European system:>>> '{0}/{1}/{2}'.format(day, month, year)‘25/1/2017’

Page 2: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

Start by looking at the arguments to the format() method. In the above examples, there are 3 of them. These arguments can be referred to using {0} for the first argument (day), {1} for the second (month) and {2} for the third (year).

Notice that we invoke the format() method using a string. However, inside that string, we can refer to any of the arguments using the braces: { and }.

You can also use a call to the format() method inside of a call to the print function.

>>> weekday = "Wednesday">>> month = "January">>> day = 18>>> year = 2012>>> hour = 1>>> minute = 45>>> second = 33

>>> print( '{}, {} {}, {} at {}:{}:{}'.format(weekday, month, day, year, hour, minute, second) )Wednesday, January 18, 2012 at 1:45:33

You can line up data in columns. Let’s consider how to line up integers using the field width within the format string. We will discuss lining up non-integer values at a later point.

>>> print('{}'.format(45))45

>>> print('{:10d}'.format(45)) 45#the ‘d’ is needed...

>>> print('{:20d}'.format(45)) 45>>> print('{:3d}'.format(45)) 45

Decision structures reviewSometimes we need to perform alternative actions depending on the condition. We have already discussed using 'if'. For example:

n = 14 if n < 0: print("n is negative")

print('Goodbye')

Page 3: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

In the above example, if n is negative, we do something. If the condition is false, we simply skip the block. In the above example, the program would simply output 'Goodbye'.

Sometimes, however, we may wish to do an action if the condition is false.

n = 14

if n < 0: print("n is negative") else: print("n is not negative")

More generally, the if-else statement has the following format:

if <condition>:<yes body>

else:<no body>

<yes body> is an indented fragment of code that is executed if <condition> evaluates to True.

<no body> is an indented fragment of code that is executed if <condition> evaluates to False.

To see the full functionality of how the if-else statement works, see the following function:

def test2(n): if n < 0: print("n is negative") print("I like negative numbers.") else: print("n is not negative")

print("Goodbye")

Run the function using negative values (e.g. -4), positive values (e.g. 4). Also be sure to test using 0 – what happens there?

Practice problem: Write a function checkHeat that takes one input argument, a temperature, and prints “It is hot!” if the temperature is 86 or more or “It is not hot” if the temperature is less than 86.

>>> checkHeat(98)It is hot!

>>> checkHeat (78)

Page 4: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

It is not hot

See the solution in this week's solutions file.

Three-way (and more) decisionsThe most general form of an if statement allows for multi-way decisions.

For example, consider the following (in the examples file):def test3(n): if n < 0: print("n is negative") elif n > 0: print("n is positive") else: print("n is 0")

This code has three alternative executions depending on whether n is positive, negative, or 0, seen below:

>>> test3(-4)n is negative

>>> test3(0)n is 0

>>> test3(4)n is positive

The general form of an if statement is:

if <condition1>:<body1>

elif <condition2>:<body2>

elif <condition3>:<body3>

…else:

<last body>

Each condition has an associated body that is the block of code to be executed if the condition evaluates to True.

Note that conditions do not have to be mutually exclusive, in which case the order in which they appear will make a difference.

Consider checkHeat2:

def checkHeat2(temp): if temp >= 86:

Page 5: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

print("It is hot") elif temp > 0: print("It is cool") else: print("It is freezing")

The above program is correct because of the order in which conditions are evaluated in Python.

Why would the version of the program below be incorrect? What value would produce unexpected results?

def checkHeat2(temp): if temp > 0: print("It is cool") elif temp >= 86: print("It is hot") else: print("It is freezing")

The 'else' block is optional. Sometimes you need an 'else' block, and sometimes you don't.

As an example, suppose you evaluate the number of hours a person practiced their piano in a week. If they practiced more than 20 hours, you say "Wow, hard worker!". If they didn't, you decide not to say something critical, and choose instead not to say anything at all. So in this case, you would not include an 'else' block.

hours_practiced = 14

if hours_practiced > 20: print("Wow, hard worker!")

Review: Branching practice problemWrite a function called checkLogin that asks a user to input a login id, a string, and then checks whether the login id input by the user is in the list [‘emily’, ‘nick’, ‘mark’, ‘julie’]. If so, your function should print “You’re in!”. Otherwise it should print “User unknown”. In both cases, your function should print “Done.” before terminating.

>>> checkLogin()Enter login id: julieYou're in!Done.

>>> checkLogin()Enter login id: yosefUser unknown.Done.

Page 6: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

ModulesA module in Python is simply a file that contains Python code.

For example, the ‘.py’ files you’ve been writing for your last couple of labs / assignments are modules.

The purpose of "modules" is no different from the idea of developing and storing programs in a file: namely code reuse. Modules provide a way to organize the components (i.e. smaller pices) that make up larger software programs.

Let's look at the 'math' module that is built in to Python. A built-in (or predefined) module is one that is automatically installed when you install Python. The typically installation of Python includes many such built-in modules. For example, all installations of Python will include the ‘math’ module.

The math moduleThe core Python language (i.e. without even requiring the math module) supports basic mathematical operators.

Those include: Comparison operators: <, <=, >, >=, ==, != Algebraic operators: +, -, *, /

For other mathematical operators, such as the square root function, trigonometric functions, and many others, we can use the math module. This module is a library of mathematical functions, as well as some well-known constants (e.g. PI).

In order to use the math module, the module must be explicitly imported into the current "execution environment" where you are programming.

If we neglect to import a module, and try to use one of the functions inside that module, we get an error. For example, here we try to invoke the sqrt() function, but without having first imported the math module:

>>> sqrt(3)Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> sqrt(3)NameError: name 'sqrt' is not defined

A module is imported using the import statement with the general format:import <name of module>

Page 7: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

Unfortunately just importing the module isn’t always sufficient, as the following example illustrates:

>>> import math

>>> sqrt(3)Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> sqrt(3)NameError: name 'sqrt' is not defined

The square root function is defined in the math module, but the execution environment isn’t aware of this. We must explicitly tell the program where (i.e. in which module) this 'sqrt' function can be found:

>>> math.sqrt(3)1.7320508075688772

Other functions: The math module includes all kinds of other functions such as exp (for exponents), log (for logarithms), cos (for cosine), fabs (for absolute value), etc, etc.

Constants: In addition to all kinds of mathematical functions, the math module also defines two well-known constants, 'pi', and 'e'. As with the functions, we must notify our program that these constants are contained inside the math module.

>>> print(math.pi)3.141592653589793

>>> print(math.e)2.718281828459045

To summarize, the math module is just a file containing a bunch of functions and constants. As shown above, the module is imported using an import statement which allows us to access the functions defined in the module, although we must use the name of the module in front of the function and constant names.

Do a web search to find the API for Python’s math module. Experiment with some of the functions and constants.

File processing (I/O)Much of computing involves the processing of data stored in files.

A file is a sequence of data stored on a secondary memory device such as the hard drive on a computer, a USB drive, magnetic tape storage, etc.

Page 8: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

A text file contains a sequence of characters. You can think of a text file as one very, very long string.

One important thing to be aware of when working with text files, is that every new line (e.g. for example, every time the user typing the file pressed ‘enter’) is separated from the next line by the special character \n . Note: This newline character does not apply to text editors where word-wrap is turned on. It only applies to actual new lines in the file.

Processing a file includes the following:1. Opening a file for reading (or writing)2. Reading from the file and (or writing to the file)3. Closing the file

Why is it necessary to explicitly open and close files? The short answer is that the operating system needs to manage files so that they remain consistent. One very important part of this is making sure that two programs aren’t working on the same file at the same time. Imagine if two programs were wrting information to the same file at the same time. This would almost certainly cause the file to have wildly inconsistent data.

Forcing programs to explicitly open and close files allows the operating system to ensure that such conflicts can not occur – or at least to force us to resolve these conflicts so that we don’t corrupt the data contained in the files.

Reading a fileLet's open a file called example.txt for reading. That is, we are only planning on reading information from the file. We are not planning on outputting text to the file (yet).

The second argument, 'r', is what tells our program that we are only going to be reading from the file.

infile = open(‘example.txt’, ‘r’)

The variable infile is very important. It is our reference to the opened file.

That is, rather than refer to the file by its filename, we will refer to it by this reference. This is typical in programming.

Common identifiers for a file reference of files we are reading from include: fin, in_file, inputFile, etc, etc.

For output files we often use: fout, outfile, etc

There are three methods commonly used to read from a file: read()

Page 9: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

readline() readlines()

Note that we need to use our reference (e.g. ‘infile’) in order to invoke these methods:

infile.read(): Returns a single string containing all of the remaining (i.e. unread) contents of the file.

o Example: If you invoke this method immediately after opening a file, then you will get back the entire file as one very long string.

o If you invoke when the cursor (discussed shortly) is halfway through the file, you will get all fo the remainder of that file.

infile.readline(): Returns the next line of the file i.e. the text up to and including the newline character ‘\n’.

o Example: If you invoke this method imediately after opening a file, you will get back the very first line of the file.

infile.readlines(): Returns a list whose items are the remaining, unread lines of the file.

To close a file associated with the variable infile, you write:

infile.close()

Note: You should always close a.ny file once you are done using it. The helps prevent the possibility of corrupting the file.

Examples: There are four functions all called sample() (with nuumbers 1 through 4) in the file file_functions.py . All four do the same thing – open a file for reading and then read and output the content of the file.

Let’s consider each function as run on the file example.txt.

Here are some additional practice problems:I may demonstrate some of these in class. However, you should be sure to work on replicating these functions on your own before looking at the answer.

1. Write a function called numLines() that takes one input argument, a file name, and returns the number of lines in the corresponding file.

It would be used as follows:>>> numLines(‘example.txt’)4

2. Write a function called numChars() that takes one input argument, a file name, and returns the number of characters in the file.

It would be used as follows:

Page 10: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

>>>numChars('example.txt')89

3. Write a function called stringCount() that takes 2 string inputs, a file name and a target string, and returns the number of occurrences of the target string in the file.

It would be used as follows'>>>stringCount('example.txt’, 'line')5

Hint: There is a string function that lets you count the number of times a substring appears in a string.

Try the above, but search for the string ‘is’. What do you notice? How might you fix this?

See this week’s examples file for the solutions.

Writing to a fileTo write to a file, the file must be opened, but this time, for writing. We indicate this with the 'w ' argument as shown here:

>>> outfile = open('out.txt', 'w')

How does this work? If there is no file out.txt in the current working directory, the above

open() function will create it. If a file out.txt exists, then any content that was previously inside that file

will be truncated (erased)! In other words, you’ll want to be careful here! If your file has some important stuff in it, then you will completely blow away all of that “old” content! For example:

outfile = open('phd_thesis.txt','w')#oops...

The ‘cursor’ When you first open a file (whether reading or writing), Python places an invisible pointer in the file called the "cursor".

This invisible pointer keeps track of where you are in the file. Not surprisingly, when you first open a file, the cursor is placed at the very beginning of the file. If, for example, you read a single line of the file, then after that command has been executed, the cursor will be placed at the beginning of the second line.

Once a file is opened for writing, the write() function can be used to write strings to it.

Page 11: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

The write() function will write the string at the cursor position. Recall that when you first open a file, the cursor (that sort of 'invisible pointer') is always placed at the very beginning of that file.

>>> outfile.write('This is the first line. ')23

Question: What’s up the the “23” above?Answer: The write() function, in additiion to writing the information to the file, also returns the number of characters that was written. Since the previous example was demonstrated using the interactive console, we see the value returned by the function is outputted to the screen.

This is a very good example of how some functions return a value – that you may not need to use. That is, the write() function is used mainly for outputting information to the stream. However, the function also gives you a little something extra in terms of the returned length “just to be nice”! We don’t have to use it, it’s just there for possible convenience.

Back to the cursor: After you invoke commands to write to a file, the cursor will move. After the above command, the cursor will now point to the position after the period. The next write command will execute starting at that position.

>>> outfile.write(' Still the first line ... \n')28

Everything written up until the new line character \n is written to the same line.

The ‘\n’ character: What if you want to begin a new line? Recall that there is a special character that refers to a newline: \n Writing this character to the file creates a new line. So if you write in a \n character, anything that follows will go to the next line.

Okay, since we included the \n character in the previous line. Anything we write now will go on the following line.

>>> outfile.write('Now we are in the second line.\n')31

Another useful character is \t. This character writes a tab.

Note: The write() function only writes strings. To write to a file content that is a data type that is not a string, we must first convert that content to a string.

For example, what if we simply wanted to output the number 5? Answer: Recall that we can convert floats and integers to strings with the str() function.

Page 12: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

>>> outfile.write('Outputting a 5: ' + str(5) + '\n')50

Here is where the string format() function is helpful. For example, we can print an exact copy of the previous line using the format() function:

>>> outfile.write('Non string values like {0} must be converted first.\n'.format(5))50

Important: Recall that if you open a file to write, and that file currently exists, all of the information in that file willl be erased! So, if we want to write to an output file without removing the existing contents, we must open the file in a different way:

>>> outfile = open('out.txt', 'a')

This will open the output file out.txt and append new information to the end of the file.

Any information already in the file will not be altered.

ExceptionsThere are two basic types of errors that can occur when running a Python program.

Syntax errors are errors that are due to the incorrect format of a Python statement.

Syntax errors occur while the statement or program is being parsed (read) but before the code actually gets executed.

A Python Virtual Machine tool called a "parser" discovers these errors.>>> (3+4]SyntaxError: invalid syntax

>>> if x == 5SyntaxError: invalid syntax

>>> print 'hello'SyntaxError: invalid syntax

>>> lst = [4;5;6]SyntaxError: invalid syntax

Exceptions occur during the execution of the statement or program.

Page 13: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

Exceptions do not occur because of a malformed Python statement or program but rather because the program execution gets into an erroneous state.

For example, the following exception is generated when our program attempts to divde by 0:

>>> x = 4>>> y = 0

>>> x/yTraceback (most recent call last): File "<pyshell#7>", line 4, in <module> 4/0ZeroDivisionError: int division or modulo by zero

The following is an exception generated by an invalid list index:

>>> lst = [14, 15, 16]>>> lst[3]Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> lst[3]IndexError: list index out of range

This is an exception generated by an unassigned variable name:

>>> x+5Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> x+5NameError: name 'x' is not defined

The following is an exception generated by an illegal value:

>>> int('4.5')Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> int('4.5')ValueError: invalid literal for int() with base 10: '4.5'

In each case an error occurs because the statement execution got into an invalid state. When this happens, we say that the Python interpreter "raises an exception".

When an exception is raised, a special 'exception object' gets created. This object will contain all the information relevant to the error.

Page 14: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

For example, it will contain the error message that indicates what happened and the program (module) line number at which the error occurred.

When an error occurs, Python's default behavior is for the program to “crash”. At this point, a message containing all of the exception object's information is typically printed to the console.

Every exception object has a specific type that is related to the type of error. Examples include those demonstrated above: ZeroDivisionError, IndexError, NameError, TypeError, ValueError. There are many other types of exceptions out there.

Catching and handling exceptionsAt this stage in your programming careers, if you are working on code your program crashes, no damage is done. You simply go back to your code and work on fixing the error.

But imagine if you are writing code that runs the anesthesia machines at a hospital. If an exception occurs, the last thing you want is for the program to crash and the machine to stop functioning!

Or suppose the software crashed for banking ATMs, international trading, the International Space Station, the Mars Rover right before landing, etc, etc. All of these could have devastating ramifications.

Even a small company can fail if their software crashes resulting in the compromise or loss of user data.

Fortunately, the whole point of the exception paradigm is that it allows the programmer the opportunity to "handle" the exception and to try and resolve it in such a way that prevents the program from crashing.

When an exception object gets created, the following takes place:1. The normal execution flow of the program stops2. The execution switches to the so-called "exceptional control flow"

The default exceptional control flow is to stop the program and print the error message contained in the exception object.This isn't much different than a "crash" since your program will stop working.

However, we as programmers can (and should!) change this default exception behavior. We do this using a set of clauses called "try and except":

Look at the following code:

>>> strAge = input('Enter your age: ')Enter your age: 22

Page 15: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

>>> age = int(strAge)

The above example will be fine – as long as the user types something that Python can successfully convert to an int.

But what if it can't? Look at what happens if the user provides something the program can't handle:

>>> strAge = input('Enter your age: ')Enter your age: fifteen

>>> age = int(strAge)

Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> age = int(strAge)ValueError: invalid literal for int() with base 10: 'fifteen'

Fortunately, we can use 'exception handling' to deal with exceptions that arise –without allowing our program to crash.

Here is a far better version of the above code that handles the exception that was generated:

strAge = input('Enter your age: ')

try:age = int(strAge)

except:print('Enter your age using digits 0-9!')

Let's run this code:Enter your age: fifteen

Enter your age using digits 0-9!

At least our program didn't crash this time! Very soon we will learn how to do things like loop our code until the user gives us a valid input for age.

Multiple except clausesExceptions come in all kinds of types and variations.

Here is an even better version of our earlier example In this version we test for a specific type of exception:

>>> strAge = input('Enter your age: ')Enter your age: fifteen

Page 16: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

try:age = int(strAge)

except ValueError:print('Enter your age using digits 0-9!')

Enter your age using digits 0-9!

Recall the comment above that refers to the idea that the type of error that is generated should match the name listed after one of the exception statements.

The point is that a try statement may have more than one except clause. This is useful as it allows us to specify different handlers depending on the type of exception that is generated.

Also, if you want, you can have a single except statement name multiple exceptions as a parenthesized tuple:

except (RuntimeError, TypeError, NameError):#code to handle these exceptions...

When an exception occurs, only one handler is allowed to be executed. Once one handler is executed, any remaining exception blocks are skipped.

If you have a bunch of named exception clauses, you might want to leave the final excepetion clause un-named. This allows you to have code that will kick in if an exception of some type that you didn't anticipate is generated. Remember that if an exception is generated, the program crashes – unless you find a way to "handle" it. So having this "wild-card" exception can be useful.

Even if you can't write code to save your program, you can at least use this last exception clause to print an error message and, perhaps, re-raise the exception. We will discuss this idea of re-rasing an exception at a later point.

try:f = open('mfile2.txt', 'r')s = f.readline()i = eval(s)

except (IOError, FileNotFoundError):print('There was an error accessing the file.')

except ValueError:print('Could not convert data to a number.')

except:print('An unexpected error has occurred.')

Summary of how exception handling works:When an exception is encountered while executing the body of a try statement, the Python interpreter will jump to the body of the except statement. We call this block of code the exception "handler".

Page 17: String Formatting Methods - condor.depaul.educondor.depaul.edu/ymendels/401/l4.docx  · Web view1/25/2017 · CSC 401 Notes. Lecture #4Yosef Mendelsohn. String Formatting Methods

A try/exception block works as follows: First, the try clause is executed.

o Note: The try clause refers to the statements(s) inside the try block. If no exception occurs, the except clause is skipped and the execution of the try

statement is finished. If an exception occurs during execution of the try block, the remainder of the try

block is skipped. At this point, if the type of error that was generated matches the exception named after the except keyword, the except block is executed and then execution continues after the try statement.

If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements. (We will discuss what is meant by 'outer' try statements later). If no handler is found, it is an unhandled exception and execution stops with a generic message.