What is Lists, tuples and strings in Python?
Categories: Python
What is Lists, tuples and strings in Python?
Tuples and lists are among the most basic and versatile data structures in Python. Lists contain any kind of data at all, and the elements can be of different types (floats, int, strings, even other tuples or lists). Many functions return tuples or lists. Try out the following examples in the interpreter Heres an example showing two ways defining a list and getting at an element:
a = [1,’two’]
a[0]
a[1]
b = [ ]
b.append(1)
b.append(’two’)
b[0]
b[1]
In the second part of the example, note that a list, like everything else in Python, is in fact an ”object” with actions (called ”methods”) which you can perform by appending the method name to the object name. The mod operator is useful for making circular lists, which begin over from the first element when one reaches the end. For exammple, if a is any list, a[i%len(a)] will access the list as if it were bent around in a circle. The same trick works for any integer-indexed object.
Python distinguishes between lists and tuples. These are similar, except that lists can be modified but tuples cannot. Lists are denoted by square brackets, whereas tuples are denoted by parentheses. The above example is a list rather than a tuple. You can define a tuple, but once defined you cannot modify it in any way, either by appending to it or changing one of its elements. There are a very few cases
where Python commands specifically require a tuple rather than a list, in which case you can turn a list (say, mylist) to a tuple by using the function tuple(mylist).
Strings are also objects, with their own set of useful methods. For example:
a = ’Five gallons of worms in a 3 gallon barrel!’
a.split()
b = a.split()
print b[0],b[3],b[4]
Note that the split() method returns a list,whose elements are strings. By the way, in Python, you can use either single quotes or double quotes to enclose a string, as long as you use them consistently within any one string. There is no difference in the behavior of single quoted and double quoted strings. For strings, the + operator is concatenation, i.e. a+b is the concatenation of the two strings a and b.
It is very often useful to be able to build strings from numerical values in your script. This need often arises in formatting printout of results to look nice, or in generating filenames. Suppose a = 2 and b = 3. Then, the following example show how you can insert the values into a string:
s = ’%d + %d = %d’%(a,b,a+b)
print s
note that the ”input” to the format string must be a tuple, not a list; recall, however, that if L is a list, the function call tuple(L) will return a tuple whose elements are those of the list input as the argument. If the tuple has only one element, you can leave off the parentheses. The format code %d (or equivalently %i) converts an integer into a string. You use %f for floating point numbers, and %e for floats in scientific notation. There are other options to these format codes which give you more control over the appearance of the output, and also several additional format codes.