Where Python strings are limited to characters, Python lists have no limitations. Python lists are ordered sequences of arbitrary Python objects, including other lists. In addition, you can insert, delete and replace elements in a list. Lists are written as a series of objects, separated by commas, inside of square brackets. Let's look at some lists, and some operations you can perform on lists.
>>> bases = ['A', 'C', 'G', 'T']
>>> bases
['A', 'C', 'G', 'T']
>>> bases.append('U')
>>> bases
['A', 'C', 'G', 'T', 'U']
>>> bases.reverse()
>>> bases
['U', 'T', 'G', 'C', 'A']
>>> bases[0]
'U'
>>> bases[1]
'T'
>>> bases.remove('U')
>>> bases
['T', 'G', 'C', 'A']
>>> bases.sort()
>>> bases
['A', 'C', 'G', 'T']
In this example we created a list of single characters that we called bases. Then we added an element to the end, reversed the order of all the elements, retrieved elements by their index position, removed an element with the value'U'
, and sorted the elements. Removing an element from a list illustrates a situation where we need to supply theremove()
method with an additional piece of information, namely the value that we want to remove from the list. As you can see in the picture below, PyCrust takes advantage of Python's ability to let us know what is required for most
operations by displaying that information in a call tip pop-up window.
0 comments:
Post a Comment