Introduction to Programming
Lesson 10 Advanced Sequence Manipulation
AOIT Introduction to Programming
Lesson 10
Advanced Sequence Manipulation
Teacher Resources
Resource
Description
Teacher Resource 10.1
Python Programs: hangman.py and hangman_stage.py (separate ZIP
file)
Teacher Resource 10.2
Presentation and Notes: Advanced Sequence Manipulation (includes
separate PowerPoint file)
Teacher Resource 10.3
Rubric: Complex Programming Project
Teacher Resource 10.4
Unit Test: Independent Programming Assignment
Teacher Resource 10.5
Answer Key: Unit Test
Teacher Resource 10.6
Key Vocabulary: Advanced Sequence Manipulation
Teacher Resource 10.7
Bibliography: Advanced Sequence Manipulation
Teacher Resource 10.2
Presentation Notes: Advanced Sequence Manipulation
Before 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 and categorizes the key Python methods and functions
used with sequences (strings, lists, and tuples) that have already
been introduced
· Introduces new string methods and functions that will be used
in programming projects yet to come in this course and puts them
into the correct categories
· Provides an opportunity for additional analysis and practice
for the methods and functions that will be used in the minor
project (Hangman program)
Presentation notes
In this presentation, you’ll learn how to use both methods and
functions to make sequences more powerful. To begin, let’s review
the difference between a method and a function.
Methods use object-oriented notation. For example,
string.isalnum() is invoking the isalnum method of the String
class. This works with "124".isalnum() because of a concept called
inheritance. Because "124" is a string, it inherits all of the
methods that belong to the String class. If you did this:
test='124'
test.isalnum()
It would also work, because the assignment statement set test to
a string, which means that test has inherited all of the methods
for the String class.
A function is not part of a class, so you don’t need to specify
class in the call, which means you’re not using object-oriented
notation. So, for example, len('124') is all you would need.
Presentation notes
Sequences (strings, lists, and tuples) are common constructs in
Python programs. It’s the methods and functions applied to these
constructs that make them powerful.
This presentation reviews and categorizes some of the methods
and functions you have used in past programs, and it introduces a
few new methods and functions you will be using in the Hangman
program.
Presentation notes
String-testing methods test to see whether a string has a
certain form, and they return True or False.
You have seen these methods before in the New Password program,
where you tested the user’s proposed password to be sure it
followed the password formation rules.
You will see the string.isalpha() method again in the Hangman
program.
The method string.islower() tests to see whether the string is
all lowercase.
Presentation notes
Case-changing methods change the case of a string. Rather than
simply testing, like the string-testing methods, they operate on
the string and return the changed string.
You have seen these methods before in the Menu and Vacation
programs, where you changed the case of some of the text.
You will see the string.lower() method again in the Hangman
program.
The method string.swapcase() swaps the case from lowercase to
uppercase, or vice versa.
No method ever changes a string in place. Rather, it returns a
new string derived from the old one.
Presentation notes
String formatting methods format string (or text) output, either
to the Python IDLE screen or to printed output.
You have seen these methods before in the Menu program, and you
might use them again in formatting the text art in Hangman.
The method to right-justify is string.rjust(width). To
right-justify means to line up the string (text) with the right
side of the available print space.
Presentation notes
Escape sequences are used in Python programs to print characters
that have a special meaning (for example, quotation marks that are
used to delimit strings) or that cannot be indicated with single
keyboard characters or words (like \n to direct Python to put
printed output on a new line).
Escape sequences were introduced in the Vacation and Menu
programs, and they might be useful in the Hangman text art.
The escape sequence to print a single quote is \'.
Presentation notes
The most common and useful string-searching method is find(),
which finds the first occurrence of a substring in a string.
The find() method is used in Hangman to check whether the letter
just guessed by the user is in the mystery word. If the letter is
in the word, the method will return the zero-based position of the
letter. If the letter is not in the word, the method will return
-1. The program branches one way or another based on the return
value.
Presentation notes
In the Hangman program, you want to be sure the letter guess
doesn’t have any leading or trailing blanks (that is, blanks at the
beginning or the end of the string) that the player inadvertently
typed in.
To ensure that this is true, you use the strip() string method
to strip off the blanks (if they are there). (The strip() method
doesn’t return an error if the blanks are not there; it only strips
off whatever blanks happen to be there.)
"Happy Birthday! ".strip() returns this:
Happy Birthday!
This type of function is more useful than it may appear. A lot
of data that is read in from files will have blank spaces at the
beginning or end of the string. This can cause many problems for
comparisons. The string-stripping and string-replacement methods
help to easily remove the blanks.
Presentation notes
You have seen several list and tuple methods and functions
recently in the Shuffle Cards program.
The append() function was used in Shuffle Cards to append each
card string in turn to the end of the list of cards.
You used the pop() built-in list method to return the first card
in the deck to the calling function and remove it from the
list.
If you wanted to return the last card in the deck instead, you
would code this:
return deck.pop()
Presentation notes
In the Hangman program, the current state of the mystery word
with the player’s correct guesses inserted is constantly being
printed out. For example, if the mystery word is hyena and the
player has just guessed a, the player needs to get a printout of “_
_ _ _ a.” If the mystery word is cat and the player has already
correctly guessed c and a, the user needs to see “c a _.”
The join() method has a format of string.join(list). In the
Hangman program, in the example on this slide, the string is
re-created each time the user guesses, so there is no string to
join to. Therefore, the string is designated as a null string
('').
In the second example on the slide, the word (theword) is a
tuple, and its length is being assigned to variable word_len.
If the mystery word is antelope, the value assigned to word_len
is 8.
Presentation notes
String methods have been used extensively in prior programs. In
future programs, especially the minor project (Hangman) and the
culminating projects, string methods, plus list and tuple methods
and functions, will play a major role.
To keep track of all these common and powerful programming
constructs, it is helpful to categorize them by what they do and
how they operate.
This course’s QuickStart Guide contains additional information
about these methods and functions.
Presentation notes
Teacher Resource 10.3
Rubric: Complex Programming Project
Student Names:
______________________________________________Date:
________________
Exemplary
Solid
Developing
Needs Attention
Required elements
All required elements are included in the assignment. Some
additional elements are included to enhance the assignment.
All required elements are included in the assignment.
One or two of the required elements are missing.
More than two required elements are missing.
Output design and results
The assignment is designed according to the best programming
practices taught in the course. The program runs without errors and
always produces the intended results. The output shows an excellent
understanding of the intended audience.
The assignment mostly follows the best programming practices
taught in the course. The program runs without significant errors
and produces the intended results. The output shows a good
understanding of the intended audience.
The assignment does not always follow the best programming
practices taught in the course. The program runs without errors but
occasionally does not produce the intended results. The output does
not show a good understanding of the intended audience.
The assignment does not follow the best programming practices
taught in the course. The program generally runs without errors but
sometimes does not produce the intended results. The output shows
little or no understanding of the intended audience.
Code design and organization
The code is clearly organized, with a logical flow of connected
elements. The code follows the best programming practices taught in
the course.
The code is organized, and most elements are well connected. The
code mostly follows the best programming practices taught in the
course.
The code is not well organized. The code does not always follow
the best programming practices taught in the course.
The code is sloppy, with some serious logic errors. The code
does not follow the best programming practices taught in the
course.
Coding
The code is clean and obvious, with no mistakes. The program is
documented with informative comments. The comments show an
outstanding grasp of the technical concepts.
The code is clean, with only a few minor mistakes or problems.
The program is documented with informative comments. The comments
show a good grasp of the technical concepts.
The code is a little sloppy, with one serious problem and/or a
number of minor mistakes or problems. The code has comments, but
they might not be well understood by peer programmers.
The code is sloppy, with many serious and minor mistakes and
problems. The code is not commented or is inadequately
commented.
Test plan
The test plan contains a comprehensive set of test cases highly
appropriate for the program objectives.
The test plan contains a minimal set of test cases appropriate
for the program objectives.
The test plan contains a few test cases, most of which are
appropriate for the program objectives.
The test plan does not contain enough appropriate test cases to
adequately test the program.
Mechanics
There are no grammatical, spelling, or punctuation errors in the
program messages or code comments.
There are few grammatical, spelling, or punctuation errors in
the program messages or code comments.
There are some grammatical, spelling, or punctuation errors in
the program messages or code comments.
There are many grammatical, spelling, or punctuation errors in
the program messages or code comments.
Additional
Comments:_____________________________________________________________________________
_____________________________________________________________________________
Teacher Resource 10.4
Unit Test: Independent Programming Assignment
Student Name:
______________________________________________________________
Date:
_______________________________________________________________________
Directions: The problem statement and requirements below define
your assignment for this unit test.
First answer the questions following the problem statement and
requirements. Then write an algorithm to satisfy the problem
statement and requirements, and a Python program to satisfy the
problem statement, requirements, and your algorithm. In doing the
test, be sure to follow the programming practices you have learned
so far in this course.
Problem Statement and Requirements
Write a Python program that reads in an integer and calculates
the sum of all the integers from 0 to the number given and prints
out the sum.
Questions
Answer the questions in the spaces below.
1. A while-statement executes the statements in a loop until
some condition is satisfied. If you have a variable n, write a
while-statement that starts a loop that executes until n is greater
than 100.
2. If you have a string variable s, which string method would
you use to test if s contains only digits: isalnum() or
isdigit()?
3. Write an if-statement that tests to see if the variable s has
a length greater than 4.
Algorithm
In the following space, write an algorithm that satisfies the
problem statement and requirements.
Python Program
In the space below, write a Python program that satisfies the
problem statement, requirements, and your algorithm. Be sure to
comment your program appropriately.
Teacher Resource 10.5
Answer Key: Unit Test
Problem Statement and Requirements
Write a Python program that reads in an integer and calculates
the sum of all the integers from 0 to the number given and prints
out the sum.
Questions
Answer the questions in the spaces below.
1. A while-statement executes the statements in a loop until
some condition is satisfied. If you have a variable n, write a
while-statement that starts a loop that executes until n is greater
than 100.
while n <= 100:
2. If you have a string variable s, which string method would
you use to test if s contains only digits: isalnum() or
isdigit()?
isdigit()
3. Write an if-statement that tests to see if the variable s has
a length greater than 4.
if len(s) > 4:
Algorithm
In the following space, write an algorithm that satisfies the
problem statement and requirements.
Prompt the user for an integer and store the string result in a
variable number.
Test to see if the number contains only digits. If it doesn’t,
print an error message and exit the program.
If the number contains only digits, calculate the sum from 0 to
number using a for-loop.
Print the sum of the numbers.
Python Program
In the space below, write a Python program that satisfies the
problem statement, requirements, and your algorithm. Be sure to
comment your program appropriately.
# read in an integer from the user
n = input ("Enter an integer: ")
# test to see if n contains only the digits 0-9
if n.isdigit():
# initialize loop variables for calculating the sum
sum = 0
number = 0
# convert n from a string to an integer
limit = int(n)
# add up the numbers from 0 to n
for i in range(0,n):
sum = sum+i
# print out the sum
print("The sum is", sum)
else:
# print an error message if n wasn't a number
print("Error,",n,"is not an integer")
Teacher Resource 10.6
Key Vocabulary: Advanced Sequence Manipulation
Terms
Definition
escape sequence
Allows you to put special characters into a string. Such a
character is defined by a backslash and another character.
Examples: \t for the tab character and \n for the newline
character
multiline block string
A string whose value spans multiple lines in a source file.
Teacher Resource 10.7
Bibliography: Advanced Sequence Manipulation
The following sources were used in the preparation of this
lesson and may be useful for your reference or as classroom
resources.
Print
Dawson, 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.
Copyright © 2009–2015 NAF. All rights reserved.
Copyright © 2009–2015 NAF. All rights reserved.