# Difference between revisions of "Sequence (Python)"

(Created page with 'In Python, '''sequence''' is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important. Lists are …') |
(→Sequence Operations) |
||

Line 10: | Line 10: | ||

*+ combines two sequences in a process called '''concatenation'''. For example, <tt>[1,2,3]+[4,5]</tt> will evaluate to <tt>[1,2,3,4,5]</tt>. | *+ combines two sequences in a process called '''concatenation'''. For example, <tt>[1,2,3]+[4,5]</tt> will evaluate to <tt>[1,2,3,4,5]</tt>. | ||

*<nowiki>*</nowiki> repeats a sequence a (positive integral) number of times. For example, <tt>[1,11]*3</tt> will evaluate to <tt>[1,11,1,11,1,11]</tt>. | *<nowiki>*</nowiki> repeats a sequence a (positive integral) number of times. For example, <tt>[1,11]*3</tt> will evaluate to <tt>[1,11,1,11,1,11]</tt>. | ||

− | *<tt>x '''in''' mySeq</tt> will return True if x is an element of mySeq, and False otherwise. | + | *<tt>x '''in''' mySeq</tt> will return True if x is an element of mySeq, and False otherwise. You can negate this statement with either <tt>not (x in mySeq)</tt> or <tt>x not in mySeq</tt>. |

*<tt>mySeq[i]</tt> will return the i'th character of mySeq. Sequences in Python are '''zero-indexed''', so the first element has index 0, the second has index 1, and so on. | *<tt>mySeq[i]</tt> will return the i'th character of mySeq. Sequences in Python are '''zero-indexed''', so the first element has index 0, the second has index 1, and so on. | ||

*<tt>mySeq[-i]</tt> will return the i'th element from the end of mySeq, so <tt>mySeq[-1]</tt> is the last element of mySeq, <tt>mySeq[-2]</tt> is the second-to-last element, etc. | *<tt>mySeq[-i]</tt> will return the i'th element from the end of mySeq, so <tt>mySeq[-1]</tt> is the last element of mySeq, <tt>mySeq[-2]</tt> is the second-to-last element, etc. |

## Revision as of 17:39, 19 April 2011

In Python, **sequence** is the generic term for an ordered set. There are several types of sequences in Python, the following three are the most important.

Lists are the most versatile sequence type. The elements of a list can be any object, and lists are **mutable** - they can be changed. Elements can be reassigned or removed, and new elements can be inserted.

Tuples are like lists, but they are **immutable** - they can't be changed.

Strings are a special type of sequence that can only store characters, and they have a special notation. However, all of the sequence operations described below can also be used on strings.

## Sequence Operations

- + combines two sequences in a process called
**concatenation**. For example,`[1,2,3]+[4,5]`will evaluate to`[1,2,3,4,5]`. - * repeats a sequence a (positive integral) number of times. For example,
`[1,11]*3`will evaluate to`[1,11,1,11,1,11]`. `x`will return True if x is an element of mySeq, and False otherwise. You can negate this statement with either**in**mySeq`not (x in mySeq)`or`x not in mySeq`.`mySeq[i]`will return the i'th character of mySeq. Sequences in Python are**zero-indexed**, so the first element has index 0, the second has index 1, and so on.`mySeq[-i]`will return the i'th element from the end of mySeq, so`mySeq[-1]`is the last element of mySeq,`mySeq[-2]`is the second-to-last element, etc.- All sequences can be sliced.

## Useful Functions

, short for length, returns the number of elements in the sequence mySeq.**len**(mySeq)

#### Searching

`mySeq.`returns the index of the first occurrence of x in mySeq. Note that if x isn't in mySeq index will return an error.**index**(x)`mySeq.`does the same thing as**find**(x)`index`, but returns -1 if x isn't an element, rather than throwing an error.and**min**(mySeq)return the smallest and largest elements of mySeq, respectively. If the elements are strings this would be the first and last elements in lexicographic order (the order of words in a dictionary). Note that if any two elements in mySeq are incomparable (a string and a number, for example), min and max will return errors.**max**(mySeq)`mySeq.`and**rindex**(x)`mySeq.`(short for right index and right find) return the**rfind**(x)*last*occurrence of x in mySeq.`mySeq.`returns the number of occurrences of x in mySeq (that is, the number of elements in mySeq that are equal to x).**count**(x)