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 …')
 
(eaggw)
 
(4 intermediate revisions by one other user not shown)
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.
Line 16: Line 16:
  
 
==Useful Functions==
 
==Useful Functions==
*<tt>'''len'''(mySeq)</tt>, short for length, returns the number of elements in the sequence mySeq.
+
*<tt>'''len'''(mySeq)</tt>, short for length, [https://artofproblemsolving.com/wiki/index.php/TOTO_SLOT_:_SITUS_TOTO_SLOT_MAXWIN_TERBAIK_DAN_TERPERCAYA TOTO SLOT] returns the number of elements in the sequence mySeq.
 
====Searching====
 
====Searching====
*<tt>mySeq.'''index'''(x)</tt> returns the index of the first occurrence of x in mySeq.  Note that if x isn't in mySeq index will return an error.
+
*<tt>mySeq.'''index'''(x)</tt> returns the index of the first occurrence of x in mySeq.  Note that if x isn't in mySeq index will return an error. (Use <tt>'''in'''</tt> with an if statement first to avoid this.)
*<tt>mySeq.'''find'''(x)</tt> does the same thing as <tt>index</tt>, but returns -1 if x isn't an element, rather than throwing an error.
 
 
*<tt>'''min'''(mySeq)</tt> and <tt>'''max'''(mySeq)</tt> 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.
 
*<tt>'''min'''(mySeq)</tt> and <tt>'''max'''(mySeq)</tt> 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.
*<tt>mySeq.'''rindex'''(x)</tt> and <tt>mySeq.'''rfind'''(x)</tt> (short for right index and right find) return the ''last'' occurrence of x in mySeq.
 
 
*<tt>mySeq.'''count'''(x)</tt> returns the number of occurrences of x in mySeq (that is, the number of elements in mySeq that are equal to x).
 
*<tt>mySeq.'''count'''(x)</tt> returns the number of occurrences of x in mySeq (that is, the number of elements in mySeq that are equal to x).
  

Latest revision as of 16:53, 19 February 2024

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 in mySeq will return True if x is an element of mySeq, and False otherwise. You can negate this statement with either 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

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

Searching

  • mySeq.index(x) returns the index of the first occurrence of x in mySeq. Note that if x isn't in mySeq index will return an error. (Use in with an if statement first to avoid this.)
  • min(mySeq) and max(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.
  • mySeq.count(x) returns the number of occurrences of x in mySeq (that is, the number of elements in mySeq that are equal to x).

See Also

Python 3.2 Documentation