A brief introduction to the sequence of Python sequences

Source: Internet
Author: User

Introduction to the sequence of Python sequences 1. Introduction to Sequences

Sequences are an important part of Python and a data structure, so what is a sequence ? By definition, a sequence is an object in which all elements are arranged in a certain order, and one or several elements of the object can be accessed by subscript index.
So what does the sequence include ? In Python, the sequence value consists of three types: string, list, and tuple. Note that the numeric object is not part of the sequence, and we say that the sequence is one of these three. Also note: We say that the sequence is not a single list, but should be included in the list, in accordance with the two principles of the definition: 1, in order, 2, through the subscript index, you can access the members

2. Operation Operations of sequences

Now that we've covered what a sequence is, and what objects are sequences, then we're going to talk about what we can do with the sequence.

2.1 Standard type Operators

Objects worthy of logical comparison, object identity through "= =" comparison, Boolean comparison

2.2 Sequence Type operators

The sequence type operator refers to the various operations that apply to a sequence, just as we have a cell phone called a sequence in our hands, what can we do with a phone called a sequence? The following will be described in order of precedence, and it is worth noting that all of the operations here are appropriate for all types within the sequence.
member relationship operator: In, not in
The member relation operator is used to determine whether an object is contained within another object, and the result is a Boolean value that returns Truewhen it is contained, and returns Falsewhen it is not included.

>>>"a" inch "AB"
True
>>>"a" not inch "BC"
True

Connect and repeat operator "+", "*"
These two operators are interesting, the first "+", is the join operator, is the addition of a series, combined into a sequence, the value supports the same type of addition, that is, the string can only be added to the string, the string cannot be added to the list.
The second "*" is to copy many copies of a Sequence object, noting the difference between the * and the value of the sequence.
slice operator [], [:], [::]
The slice operation returns one or more members based on the parameters inside. You can receive up to 3 parameters. When the parameter is only 1: sequence[i] returns the first I+1 element of the sequence named sequence. When there are two arguments, Sequence[2:5] Returns a sequence of three elements starting from 2 to 4, starting with the first argument and ending before the second argument. When the parameter is three, Sequence[1:9: 2] Returns a sequence that ends with a step of 2, starting from 1 to 9.

>>>ls = [1,2,3,4,5,6,7,8,9]
>>>ls[5]
6
>>>ls[2:5]
[3,4,5]
>>>ls[1:9:2]
[2,4,6,8]

The same applies ls to a string and a tuple, where the operation is the same as the value returned.

2.3 Built-in functions

Type Conversions
The sequence contains only strings, lists, tuples of these three, so the sequence inside the type conversion is only three, respectively: the list (ITER) to convert the iterative object into a list; str (obj) converts the object obj to a string; Tuple (ITER) converts an iterative object into a tuple
Operatioal
Sequences can also have operations, which are built-in functions that can be directly called
Len (seq): Returns the length of the SEQ
Max (ITER) min (ITER): Returns the maximum value of an object that can be iterated, the minimum value
Zip ([it0, It1, it3]): Returns a list of tuples that are composed of elements at the same location as each parameter
Reversed (SEQ): Receives a sequence as a parameter, returns a sequence in reverse order
Enumerate (): Pairs iterate over each item in an iterative object, the first is a number, the second is the item

>>>s =' Foobar '
>>>len (s)
6
>>>max (s)
R
>>>t =' mother '
>>>zip (S, t)
[(' F ',' m '), (' O ',' O '), (' O ',' t '), (' B ',' h '), (' A ',' E '), (' R ',' R ')]
>>> forI, tinchEnumerate (s):
PrintI, t
0F
1O
2O
3B
4A
5R

A brief introduction to the sequence of Python sequences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.