Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

Overview of Sequences

All the varieties of sequences (strings, tuples and lists) have some common characteristics. We'll identify the common features first, and then move on to cover these in detail for each individual type of sequence. This section is a road-map for the following three sections that cover strings, tuples and lists in detail.

Literal Values. Each sequence type has a literal representation. The details will be covered in separate sections, but the basics are these: strings are quoted: "string"; tuples are in ()'s: (1,'b',3.1); lists are in []'s: [1,'b',3.1]. A tuple or a list is a sequences of various types of items. A string is a sequence of characters only.

Operations. Sequences have three common operations: + will concatenate sequences to make longer sequences. * is used with a number and a sequence to repeat the sequence several times. Finally, the [] operator is used to select elements from a sequence.

The [ ] operator can extract a single item, or a subset of items by slicing. There are two forms of []. The single item format is sequence [ index ] . Items are numbered from 0. The slice format is sequence [ start : end ] . Items from start to end -1 are chosen to create a new sequence as a slice of the original sequence; there will be end start items in the resulting sequence.

Positions can be numbered from the end of the string as well as the beginning. Position -1 is the last item of the sequence, -2 is the next-to-last item. Here's how it works: each item has a positive number position that identifies the item in the sequence. We'll also show the negative position numbers for each item in the sequence. For this example, we're looking at a four-element sequence like the tuple (3.14159,"two words",2048,(1+2j)).

forward position 0 1 2 3
reverse position -4 -3 -2 -1
item 3.14159 "two words" 2048 (1+2j)

Why do we have two different ways of identifying each position in the sequence? If you want, you can think of it as a handy short-hand. The last item in any sequence, S can be identified by the formula S[ len(S)-1 ]. For example, if we have a sequence with 4 elements, the last item is in position 3. Rather than write S[ len(S)-1 ], Python lets us simplify this to S[-1].

You can see how this works with the following example.

>>> 
a=(3.14159,"two words",2048,(1+2j))

>>> 
a[0]

3.1415899999999999
>>> 
a[-3]

'two words'
>>> 
a[2]

2048
>>> 
a[-1]

(1+2j)
            

Built-in Functions.  len, max and min apply to all varieties of sequences. len returns the length of the sequence. len("Wednesday") is 9. max returns the largest value in the sequence. max( (1,2,3) ) is 3. min, analogously, returns the smallest value in the sequence. min( [19,9,99] ) is 9.

Comparisons. The standard comparisons (<, <=, >, <=, ==, !=) apply to sequences. These all work by doing item-by-item comparison within the two sequences. The item-by-item rule results in strings being sorted alphabetically, and tuples and lists sorted in a way that is similar to strings.

There are two additional comparisons: in and not in . These check to see if a single value occurs in the sequence. The in operator returns a True if the item is found, False if the item is not found. The not in operator returns True if the item is not found in the sequence.

Methods. The string and list classes have method functions that operate on the object's value. For instance "abc".upper() executes the upper method belonging to the string literal "abc". The result is 'ABC'. The exact dictionary of methods is unique to each class of sequences.

Statements.  tuples and lists are central to certain Python statements, like the assignment statement and the for statement. These were details that we skipped over in the section called “The Assignment Statement” and the section called “Iterative Processing: For All and There Exists”. The additional tuple-specific details of these statements will be covered in Chapter 13, Tuples .

Modules. There is a string module with several string-specific functions. Most of these functions are now member functions of the string type, except for a special-purpose function used to create translation tables. Additionally, this module has a number of constants to define various subsets of the ASCII character set, including digits, printable characters, whitespace characters and others.

Factory Functions. There are also built-in factory (or conversion) functions for the sequence objects.

repr ( object ) → string

Return the canonical string representation of the object. For most object types, eval(repr(object)) == object.

str ( object ) → string

Return a nice string representation of the object. If the argument is a string, the return value is the same object.

list ( sequence ) → list

Return a new list whose items are the same as those of the argument sequence.

tuple ( sequence ) → tuple

Return a new tuple whose items are the same as those of the argument sequence. If the argument is a tuple, the return value is the same object.


 
 
  Published under the terms of the Open Publication License Design by Interspire