Hacking Secret Ciphers with Python

(Ann) #1
Chapter 8 – The Transposition Cipher, Encrypting 111

'dog'





spam[0][1]
'cat'
spam[1][0]
1
spam[1][1]
2





The double index brackets used for spam[0][0] work because spam[0] evaluates to
['dog', 'cat'] and ['dog', 'cat'][0] evaluates to 'dog'. You could even use
another set of index brackets, since string values also use them:





spam = [['dog', 'cat'], [1, 2, 3]]
spam[0][1][1]
'a'





Say we had a list of lists stored in a variable named x. Here are the indexes for each of the items
in x. Notice that x[0], x[1], x[2], and x[3] refer to list values:


Figure 8-1. A list of lists with every item’s index labeled.

Practice Exercises, Chapter 8, Set C


Practice exercises can be found at http://invpy.com/hackingpractice 8 C.


Using len() and the in Operator with Lists


We’ve used the len() function to tell us how many characters are in a string (that is, the length
of the string). The len() function also works on list values and returns an integer of how many
items are in the list.

Free download pdf