Word Jumble Game in Python
Word Jumble Game in Python
Python
Chapter Four
for Loops, Strings, and Tuples: The Word
Jumble Game
Objectives
Construct for loops to move through a sequence
Use the range() function to create a sequence of
integers
Treat strings as sequences
Use tuples to harness the power of sequences
Use sequence functions and operators
Index and slice sequences
loop
10
11
12
13
14
Takes a sequence
Returns the number of elements
In strings, every character counts spaces and
punctuation
15
16
Indexing Strings
Sequential access: Access in order
Random access: Direct access to any element
Indexing: Process used to access a specific
element of a sequence
Member: An element of a sequence
Python allows for random access to sequences
(such as strings) via indexing
17
18
19
20
21
String Immutability
>>> word = "game"
>>> word[0] = "l"
TypeError: object does not support item assignment
Mutable: Changeable
Immutable: Unchangeable
Strings are immutable sequences; cant be
changed
But can create new strings from existing ones (like
through concatenation)
Guide to Programming with Python
22
23
24
25
Constants
VOWELS = "aeiou"
26
27
Slicing Strings
Slice: Copy of continuous section of a sequence
Can make slices (copies) of continuous sections of
sequence elements
Can slice one element or multiple, continuous part
of sequence
Can even create a slice that is copy of entire
sequence
28
29
None
Representing nothing
Makes a good placeholder for a value
Evaluates to False when treated as a condition
30
Slicing
QuickTime and a
deco mpre ssor
are needed to see this picture.
QuickTime and a
decompressor
are nee ded to se e this picture.
31
Slicing (continued)
>>> word = "pizza"
>>> print word[0:5]
pizza
>>> print word[1:3]
iz
>>> print word[-4:3]
iz
32
Slicing (continued)
>>> word = "pizza"
>>> word[:4]
'pizz'
>>> word[2:]
'zza'
>>> word[:]
'pizza'
33
Creating Tuples
Tuple: Immutable sequence of values of any type
Could have tuple of integers for a high score list,
for example
Tuples elements don't need to all be of same type
34
35
Tuple Basics
Creating an Empty Tuple
inventory = ()
36
37
Using Tuples
Tuples are a kind of sequence (like strings) so can:
38
39
40
Indexing Tuples
41
Slicing Tuples
QuickTime and a
decompressor
are needed to see this picture.
QuickTime and a
decompressor
are needed to see this picture.
QuickTime and a
decompressor
are needed to see this picture.
QuickTime and a
decompressor
are needed to see this picture.
QuickTime and a
decompressor
a re needed to see this picture.
42
Tuple Immutability
>>> inventory = ("sword", "armor", "shield",
"healing potion")
>>> inventory[0] = "battleax"
TypeError: object doesn't support item assignment
43
Concatenating Tuples
>>> inventory = ("sword", "armor", "shield",
"healing potion")
>>> chest = ("gold", "gems")
>>> inventory += chest
>>> print inventory
('sword', 'armor', 'shield', 'healing potion',
'gold', 'gems')
44
Review word_jumble.py
45
Summary
An ordered list of elements is called what?
A sequence
46
Summary (continued)
If I use the in operator to test for element membership in a
tuple, what does it return if the element is there?
True
47
Summary (continued)
String concatenation adds onto an existing string, true or false?
False, it creates brand-new strings
48