Hacking Secret Ciphers with Python

(Ann) #1

108 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Type the following into the interactive shell:





animals = ['aardvark', 'anteater', 'antelope', 'albert']
animals
['aardvark', 'anteater', 'antelope', 'albert']





The animals variable stores a list value, and in this list value are four string values. The
individual values inside of a list are also called items. Lists are very good when we have to store
lots and lots of values, but we don't want variables for each one. Otherwise we would have
something like this:





animals1 = 'aardvark'
animals2 = 'anteater'
animals3 = 'antelope'
animals4 = 'albert'





This makes working with all the strings as a group very hard, especially if you have hundreds,
thousands, or millions of different values that you want stored in a list.


Many of the things you can do with strings will also work with lists. For example, indexing
and slicing work on list values the same way they work on string values. Instead of individual
characters in a string, the index refers to an item in a list. Try typing the following into the
interactive shell:





animals = ['aardvark', 'anteater', 'antelope', 'albert']
animals[0]
'aardvark'
animals[1]
'anteater'
animals[2]
'antelope'
animals[3]
'albert'
animals[1:3]
['anteater', 'antelope']





Remember, the first index is 0 and not 1. While using slices with a string value will give you a
string value of part of the original string, using slices with a list value will give you a list value of
part of the original list.

Free download pdf