CSCE 110 1nd EditionLecture 9Outline of Last Lecture:I. More on FunctionsII. Function VariablesA. Global VariablesB. Local VariablesIII. Built-In FunctionsIV. ListsOutline of Current Lecture:I. Review of Basic ProgramsA. Basic OperationsB. If/elseC. WhileD. User-defined functionII. Additional NotesCurrent Lecture:I. Review of Basic Programs A. Here is a program that contains some basic mathematical operations:>>> num = 12>>> print num / 12>>> print 123 % 100>>> print 8 + 3 * 7>>> print num / 13>>> print (0 == 1) and (2 < 3)>>> print ((num > 3) or (0 == 1)) and (6 < 7)The output of this program will be:>>> 1>>> 23>>> 29>>> 0>>> False>>> TrueB. Here is a program with an if/else statement:1 >>> x = 32 >>> if x < 2:These notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.3 >>> print "First"4 >>> else:5 >>> print "Second"6 >>> if x < 2:7 >>> print "Third"8 >>> print "Fourth"9 >>> print "Fifth"The order of line execution in this program is 1, 2, 4, 5, 6, 8, 9. Notice that line 7 is arbitrary. It will never be read, because if line 6 were True, then line 2 would be as well, making lines 5-8 obsolete under "else." The output here is:>>> Second>>> Fourth>>> FifthIf we were to change x = 1 in line 1, then the order of line execution would be 1, 2, 3, 9, and the output would be:>>> First>>> FifthUsing our knowledge from the last set of notes, we can recognize that this program does not have any local variables, only global (x). If there is not a user-defined function in the program, there will not be any local variables. C. Here is an example of a program with a while loop:1 >>> s = 02 >>> i = 33 >>> while i > 0:4 >>> s += 15 >>> i -= 16 >>> print s7 >>> print iIt may help to keep track of the changing variables by making a table. The order of line execution here is 1, 2, 3, 4, 5, 3, 4, 5, 3, 4, 5, 3, 6, 7. The last time line 3 is read is after the while loop's condition became False. The output of the program is:>>> 3>>> 0D. Here is an example of a program with a user-defined function:1 >>> def fun(n,m):2 >>> x = 43 >>> return m - n + x4 >>> def main():5 >>> x = 106 >>> y = 207 >>> print fun(10,7)8 >>> print x9 >>> print fun(fun(1,2),3)10 >>> main()Notice how in line 9, we can call a function within calling a function. The order of line execution in this program is 10, 4, 5, 6, 7, 1, 2, 3, 7, 8, 9, 1, 2, 3, 9, 1, 2, 3, 9. The output of this program is:>>> 1>>> 10>>> 2Note that even if we inserted a new line after line 3, it would never be read because line 3 immediately returns a value. Function fun() has local variables n, m, and x. Function main() has local variables x and y. II. Additional NotesNotice in Part C's program, we used "s += 1" instead of "s = s + 1." We can do this with the othermathematical operators as well. For example, we could have "s *= 2," which is equivalent to "s =s * 2," or "s/= 2," which is equivalent to "s = s / 2."When taking the test, assume that all programs provided to you contain no errors, unless there is a question that specifically indicates there may be a possible
View Full Document