ASIDE: Length of a Python Line - DePaul University
CSC 401 NotesLecture #4Yosef MendelsohnASIDE: Length of a Python LineNote the length of the following line of Python code:print('Non string values like ' + str(3.14) + ' or ' + str(5) + ' must be converted first.\n')This line is 95 characters long. Python programmers typically try to ensure that their lines of code do not extend beyond 79 characters. There are various reasons for this, but one simple one is that we don’t want our code extending way off to the right of the screen. The 79 character limit may appear arbitrary, but it is a convention that is accepted by many Python programmers. There is a series of stylistic programming conventions and proposals known as “Python Enhancement Proposals” or PEPs. The 79 character limit is discussed in PEP-8. You can learn about PEP-8 here: . The section discussing the 79 character limit can be found under the heading ‘Maximum Line Length’. There are a few different ways of breaking up lines. Inside parentheses: If you are inside a set of parentheses, you may do so, albeit with certain restrictions. We won’t get into all of the nuances here, but let’s look at a couple of examples:>>> print('hi', 'bye',(4+7), 3) hi bye 11 3In the above example, because we had not typed the closing parenthesis to the print() function (highlighted), Python allowed us to break the line into multiple lines.Important: Also note that we do must not break between code and a comma. Backslashes: Another way is to place a backslash:>>> (3+2) * \ (4/7) - int('3') + \ 4 3.8571428571428568So, for our long statement discussed earlier, we might instead have preferred to do this:print('Non string values like ' + str(3.14) + ' or ' + str(5) +' must be converted first.\n')Because we are inside a set of parentheses, we were able to break the line up. (We didn’t even have to use a backslash). Had we not been inside parentheses, we would have needed the backslash. Without it, we would get an error:>>> s = 'Non string values like ' + str(3.14) + ' or ' + str(5)+ ' must be converted first.\n' SyntaxError: multiple statements found while compiling a single statementHere is the corrected version: >>> s = 'Non string values like ' + str(3.14) + ' or ' + str(5) \+ ' must be converted first.\n'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')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)It is not hotdef checkHeat(temperature): '''docstring''' if temperature>=86: print('It is hot') else: print('It is not hot')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 positiveThe 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: 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 = 14if 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. Also, the function should allow for upper or lower case.>>> checkLogin()Enter login id: julieYou're in!Done.>>> checkLogin()Enter login id: yosefUser unknown.Done.def checkLogin(name): '''let's user know if they have authenticated''' legitUsers = ['emily','nick','mark','julie'] if name.lower() in legitUsers: print('You\re in!') else: print('User unknown') print('Done.')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 definedA module is imported using the import statement with the general format:import <name of module>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 definedThe 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.7320508075688772Other 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.718281828459045To 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.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:Opening a file for reading (or writing)Reading from the file and (or writing to the file)Closing the fileWhy 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, etcThere are three methods commonly used to read from a file: read()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. Example: If you invoke this method immediately after opening a file, then you will get back the entire file as one very long string. 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’. 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 any 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. 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’)4Write 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:>>>numChars('example.txt')89Write 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')3Hint: 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 the file file_functions.py for the solutions.A Commonly Encountered ErrorSometimes, when trying to open a file in your Python code, you may encounter a UnicodeDecodeError.If you run into this, try including the argument encoding='utf-8'to your open() function. This will typically take care of that problem. If that doesn't work, another thing to try is: encoding='latin1'?For example: infile = open('filename.txt','r',encoding='utf-8')This will typically fix that particular error.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.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. ')23Question: 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')28Everything 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')31Another 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.>>> outfile.write('Outputting a 5: ' + str(5) + '\n')50Here 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))50Important: 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 syntaxExceptions occur during the execution of the statement or program.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 zeroThe 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 rangeThis 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 definedThe 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.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:The normal execution flow of the program stopsThe 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>>> 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: fifteenEnter 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: fifteentry: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". A try/exception block works as follows:First, the try clause is executed.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. ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- length of a circumference
- length of a line calculator
- arc length of a curve calculator
- arc length of a vector calculator
- arc length of a vector
- length of a curve formula
- new york times length of a foot
- length of a string javascript
- java get the length of a string
- length of a column in matlab
- length of a curve calculus
- find the length of a line segment