Curriculum.naf.org



AOIT Introduction to ProgrammingLesson 9User-Defined Functions and SequencesTeacher ResourcesResourceDescriptionTeacher Resource 9.1Python Programs: define_cards.py, olympic_rings.py, olympic_rings_function.py, program_template.py, and shuffle_cards.py (separate ZIP file)Teacher Resource 9.2Presentation and Notes: Sequences—Lists and Tuples (includes separate PowerPoint file)Teacher Resource 9.3Quiz: User-Defined Functions and SequencesTeacher Resource 9.4Answer Key: User-Defined Functions and Sequences QuizTeacher Resource 9.5Key Vocabulary: User-Defined Functions and SequencesTeacher Resource 9.6Bibliography: User-Defined Functions and SequencesTeacher Resource 9.2Presentation Notes: Sequences—Lists and TuplesBefore you show this presentation, use the text accompanying each slide to develop presentation notes. Writing the notes yourself enables you to approach the subject matter in a way that is comfortable to you and engaging for your students. Make this presentation as interactive as possible by stopping frequently to ask questions and encourage class discussion.This presentation does the following: Reviews concepts already learned about sequences (strings, lists, and tuples) Explains the formal programming relationship among strings, lists, and tuples Provides examples of lists and tuples in Python Explains how sequences will be used in programming projects to come Presentation notesSequences are ordered collections of values, and strings are one type of sequence.In many programming languages, sequences are called arrays.You have used strings in many different programs in this course.So far, you have not thought of strings as “ordered collections of values”; you have probably thought of them instead as whole strings or “words.” Strings are “immutable,” which means they cannot be changed.In what sense could strings be thought of as collections of values?Presentation notesAlthough in the firstname.py program you were interested only in the complete first name, Emily, in some instances you might be interested in the individual letters or characters in the name. In that case, it would be useful to split up the name and refer only to the character of interest (for example, the i).Presentation notesWith indexing you can identify the individual letters or characters in a given string.What is called positive indexing (moving from left to right in the string) begins with 0. Therefore, firstname[0] in the example is used to identify the E in Emily. (In mathematics, this is called a subscript, and you can read it as “firstname subzero.”)The value of m in the string is firstname[1] or “firstname sub-one.”If you were writing a program to play hangman and the mystery words were girls’ names, you might use indexing to identify the individual letters in the names.Negative indexing (moving from right to left in the string) also exists in Python, but it is not used in this course. Negative indexing moves from right to left in the string, starting with -1 and, in this example, going through -5. (The index for the E in Emily would be firstname[-5].)Presentation notesIn the game of hangman, you need to know the mystery word, the number and position of the letters in the word, and the user’s guess. When you play the game, you need to figure out whether the user’s current letter guess is in the mystery word and, if so, where in the word it is located.Indexing is an important construct in the algorithm.In the current scenario, the a (the user’s guess) occupies the second position in the word. That would translate to letters[1] and mystery_word[1].If the mystery word were antelope, the guess of p occupies the seventh position in the word, which would translate to letters[6] and mystery_word[6]. Presentation notesIndexing with strings is useful in programming, but it is probably even more useful with lists, which are sequences of 0 or more values. Lists are always enclosed in square brackets ([ ]).List indexes (that is, positive indexes) always begin with 0 and move from left to right, just like string indexes.Lists are mutable, which means they can be changed.The value of favorite_foods[1] is hamburgers.Numbers and favorite_foods are lists because they are collections of information elements. Such collections are usually related in some way (which should be obvious here).Lists can contain multiple data types; for example, both 20 and hamburgers could be in the same list.Presentation notesThese collections of informational elements are all lists. The first might be times achieved in running 100 yards. The Shuffle Cards example might be the order of a list of 52 cards that have been shuffled (the range is 0 to 51). The my_pets example names types of pets. The word_length example could be used to initialize a variable that will contain a list of values to null.These examples are all examples of lists that might change over time. For example, if the runner improves her time in the 100-yard dash, the next set of results might contain smaller numbers. If the snake should happen to eat the hamster, the list of pets will be different.Therefore, it is appropriate that these sequences are lists.Presentation notesTuples are very similar to lists in Python, but they cannot be changed. They are used to hold constant data.Python would give you an error message if you tried to code animal_words[1] = "fish".The rank_string and suit_string tuple will always be the same for a standard deck of cards.The animal words (that is, possible choices for the mystery words) may be altered by the programmer over time, but they will not change while the program is running.One important difference between tuples and lists is that tuples are enclosed in parentheses (curved brackets) and lists are enclosed in square brackets ([ ]).If you did a text art “library” for the hangman game (for example, one piece of text art in which only the figure's left arm is showing and another in which both arms are showing), you would probably make it a tuple, because it would not change during the game.Note that in other programming languages, this method of defining constants is used frequently. Another example is PHP, which adds const as a prefix to the variable name. Presentation notesIt is very important to use the correct kind of brackets (or parentheses) for lists and tuples. Tuples are always enclosed in parentheses (curved brackets) and lists in square brackets ([ ]). Incorrect usage will generate a syntax error in Python.It is also important to note that indexing (of strings, lists, and tuples) is always done using square brackets ([ ]). An example is suit_string[3], which translates to spades.Presentation notesThere are a number of functions that make sequences more powerful. Some functions can be used with all three kinds of sequences (strings, lists, and tuples), but not all can.One example that can be used with all three is the test for membership, which means “is this element in this sequence?”You will have a chance to use a number of these functions in the programming projects during the remaining lessons of this course.Presentation notesPython has three kinds of sequences: strings, lists, and tuples.Sequences are very powerful programming constructs, and they will play an important role in the remaining programs in this course.One of the most common of the sequence operations is indexing (also called subscripting), which allows you to identify and operate on a single element in a collection of elements.You will be using both lists and tuples in the very next program, which is called Define Cards. You will also use them in Shuffle Cards (in this lesson) and Hangman (in the next lesson).Presentation notesTeacher Resource 9.3Quiz: User-Defined Functions and Sequences Explain the difference between a built-in function and a user-defined function.When you wrote the user-defined function for the Olympic Rings program (you called the new program olympic_rings_function.py), which code in the original program were you replacing in the new program? Why is it considered a better programming practice to use the user-defined function instead of the original code?Describe how to call a user-defined function in a Python program.What are sequences in Python? What three kinds of sequences are we studying in this course? What are sequences typically called in other programming languages?What is the purpose of indexing in sequences?Write a Python assignment statement containing a tuple with five strings and a variable called cities.Write an index element reference to access the fifth string in your assignment statement.Teacher Resource 9.4Answer Key: User-Defined Functions and Sequences QuizExplain the difference between a built-in function and a user-defined function.A built-in function is part of the Python programming language (or any programming language). A user-defined function is created by a programmer for a specific program or programming purpose.When you wrote the user-defined function for the Olympic Rings program (you called the new program olympic_rings_function.py), which code in the original program were you replacing in the new program? Why is it considered a better programming practice to use the user-defined function instead of the original code?The purpose of the user-defined function in the new Olympic Rings program was to replace the five drawing constructs in the original program. It is considered a better programming practice (for efficiency and understandability) to use a user-defined function for this purpose.Describe how to call a user-defined function in a Python program.You call a user-defined function by coding its name and any parameters (also called arguments) required by the function. The “call” is usually in the main processing section of the program, but functions can call other functions.What are sequences in Python? What three kinds of sequences are we studying in this course?Sequences are ordered collections of values, assigned to a programming variable. The values are the individual elements in the sequence. They are grouped inside quotation marks (in the case of strings), square brackets (for lists), or parentheses (for tuples). They are generally ordered (indexed) left to right, but right-to-left ordering (called negative indexing) is also possible. What are sequences typically called in other programming languages?In other languages, sequences are typically called arrays.What is the purpose of indexing in sequences?Indexing allows us to reference a particular element in the sequence “collection.”Write a Python assignment statement containing a tuple with five strings and a variable called cities.cities = ("Detroit","New York","Atlanta","Portland","Denver")Write an index element reference to access the fifth string in your assignment statement.cities[4]Teacher Resource 9.5Key Vocabulary: User-Defined Functions and SequencesTermDefinitionargumentSee parameter.arrayIn programming, a group of data elements that are accessed individually by indexing. In Python, arrays are called sequences. See also sequence, index.functionA portion of code (possibly within a larger program) that performs a specific task and is relatively independent of the remaining code. The code within the function is called its body. function callRunning the code in a function after substituting values for all the parameter variables in the function definition.immutableSomething that cannot be changed after it is created is immutable, or unchangeable.Example: In Python, tuples are immutable.See also mutable.indexAn integer used to select a particular item in a sequence.listA list is a sequence in which each item in the sequence can be changed. In Python, lists are defined like this: z=['a',3,5.5].modular programmingBreaking a program into its base functionalities that are independent of each other and coding these as user-defined functions, then tying the functionalities together into short, centralized programs. Using modular programming greatly speeds up development time, since after a while all the functionality a programmer needs already exists in an importable function somewhere.mutableSomething that can be changed after it is created.Example: In Python, lists are mutable.See also immutable.parameterA special kind of variable that refers to data that a function receives to operate on.Example: turtle.forward(20)In the above example, the parameter is 20 (which stands for 20 pixels). Calling the function moves the turtle forward 20 pixels.sequenceA group of data elements that are accessed individually by indexing. In many other programming languages, sequences are called arrays.The most common type of Python sequences are strings, lists, and tuples. Each element of a sequence has a number or index used to reference its value.Example: if x = "abc", then x[0] is 'a'. The index used is 0.See also array, index.tupleA tuple is a sequence in which each item in the sequence cannot be changed. Example: x = (1,2,3,'z'). Python will refuse to execute the statement x[1]='xx', because it would change an item in the tuple.See also immutable.user-defined functionA function created by a programmer for a specific program or programming purpose. Other types of functions are system functions and native functions, which are the functions packaged within the Python language.Teacher Resource 9.6Bibliography: User-Defined Functions and SequencesThe following sources were used in the preparation of this lesson and may be useful for your reference or as classroom resources. We check and update the URLs annually to ensure that they continue to be useful.PrintDawson, Michael. Python Programming, 2nd ed. Boston: Thompson Course Technology PTR, 2006.Donaldson, Toby. Python, 2nd ed. Berkeley, CA: Peachpit Press, 2009.Downey, Allen, Jeffrey Elkner, and Chris Meyers. How to Think Like a Computer Scientist: Learning with Python. Wellesley, MA: Green Tea Press, 2002.Guzdial, Mark. Introduction to Computing and Programming in Python (A Multimedia Approach). Upper Saddle River, NJ: Pearson Education, 2005.Hetland, Magnus Lie. Beginning Python: From Novice to Professional, 2nd ed. Berkeley, CA: Apress, 2008.Lutz, Mark. Learning Python, 3rd ed. Sebastopol, CA: O’Reilly Media, 2008.Miller, Bradley N., and David L. Ranum. Python: Programming in Context. Sudbury, MA: Jones and Bartlett Publishers, 2009.Sande, Warren, and Carter Sande. Hello World!: Computer Programming for Kids and Other Beginners. Greenwich, CT: Manning Publications, 2009.Online“Array.” Wikipedia, (accessed March 17, 2014).“Efficient Arrays of Numeric Values.” The Python Software Foundation, (accessed December 17, 2014). ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download