AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright ©...

11
AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation ght © 2009–2012 National Academy Foundation. All rights reserved.

Transcript of AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright ©...

Page 1: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

AOITIntroduction to Programming

Unit 3, Lesson 10

Advanced Sequence Manipulation

Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Page 2: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Methods and functions make sequences more powerful

Examples:

String methods can test strings:

"abCde".isalpha() returns True

String methods can change strings:

"abCde".lower() returns "abcde"

List and tuple functions can return information:

Given mytuple = ("horse","cow","sheep")

len(mytuple) returns 3

If you applied the len() function to the suit_string tuple in the Shuffle Cards program, what would you get?

Page 3: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

String-testing methods test to see whether a string has a certain form

Prior Program: New Password

New Program: Hangman

Key Characteristics: Returns True or False

Examples:

string.isalnum()

string.isalpha()

string.isnumeric()

# string contains only letters or numbers

# string contains only letters

# string contains only numbers

What do you suppose string.islower() does?

Page 4: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Case-changing methods change the case of a string

Prior Programs: Menu and Vacation

New Program: Hangman

Key Characteristics: Operates on (makes a change in) a string

Examples:

string.capitalize()

string.lower()

string.upper()

# capitalize the first character

# convert to lowercase

# convert to uppercase

What do you suppose string.swapcase() does?

Page 5: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

String-formatting methods format string (text) output

Prior Program: Menu

New Program: Hangman

Key Characteristics: Formats printed output

Examples:

string.center(width)

string.ljust(width)

You will use these methods in the text art for Hangman.

# center string within a given width

# left-justify string

What is the method to right-justify? What does right-justify mean?

Page 6: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Escape sequences are useful in formatting text

Note: sequence here means “a string of characters”

• Backslashes (\) are used in Python programs to insert special characters into strings.

• An example you have seen before (in the Menu and Vacation programs) is \t, which moves the cursor forward one tab stop.

• Another example is \", which prints a double quote.

• You might find escape sequences useful in the text art for Hangman.

What is the escape sequence to print a single quote?

Page 7: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

find() is the most useful string-searching method

• The find() method finds the first occurrence of a substring in a string.

• find() returns the lowest index where the substring is found, and it returns -1 if it is not found.

• The format is string.find(substring).

• An example is "abcd".find("a"), which returns 0.

What do you think "abcd".find("c") returns?

What do you think "abcd".find("e") returns?

Page 8: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

The string-stripping and string-replacement methods change strings

Prior Program: Vacation

New Program: Hangman

Key Characteristics: Makes changes to strings

Examples:

"cat".replace("t","b") returns 'cab'

" 12A B ".strip() returns '12A B'

String-stripping is used in Hangman.

What do you think "Happy Birthday! ".strip() returns?

Page 9: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Hangman uses several list and tuple methods and functions

Prior Program: Shuffle Cards

New Program: Hangman

Names: append(), pop(), join(), and len()

Examples (from Shuffle Cards):

cards.append(card_string)

return deck.pop(0)

How would you return the last card in the deck?

# append each card string in turn# to the end of the list of cards

# return the first card in the deck# to the calling function# and remove it from the list

Page 10: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Hangman uses several list and tuple methods and functions (continued)

Examples (from Hangman):

print(''.join(guesses))

word_len = len(theword)

If the word is antelope, what value is assigned to word_len?

# print the letters correctly guessed

# if the word is hyena and the player has guessed a

# then join() returns '_ _ _ _a' (four underscores and an a)

# assign the length of the word

# to the variable word_len

Page 11: AOIT Introduction to Programming Unit 3, Lesson 10 Advanced Sequence Manipulation Copyright © 2009–2012 National Academy Foundation. All rights reserved.

Methods and functions are used extensively with sequences

• You have used many methods and functions in prior programs.

• You will be using a number of methods and functions in the Hangman minor project and in your culminating project.

• It is helpful to categorize methods and functions based on what they do and on what kind of sequences they work with.

• For more information about methods and functions, see the course’s QuickStart Guide.