Python learning notes: Python sequence

Source: Internet
Author: User

The Python sequence consists of a string, a list, and a tuple of three parts, and the following is a general list of some of the operators and built-in functions that are common to the Python sequence.

First, the Python sequence sequence type operator

A standard type of operator can generally be applied to all sequence types, where the sequence type operator is mentioned.

1. Member relationship operator (in, not in)

The member relationship operator is used to determine whether an element belongs to a sequence. Specific syntax:

Object [NOT] in sequence

2. Connection operator (+)

This operator allows us to concatenate a sequence with another sequence of the same type, with specific syntax:

Sequence1 +sequence2

3. Repeat operator (*)

Produces multiple copies of a sequence, with specific syntax:

Sequence * Copies_int

4. Slice operator

A sequence type is a type of data structure whose elements are placed sequentially, which allows a data element to be obtained by specifying a subscript, or by specifying a subscript range to obtain a set of elements of a sequence. This way of accessing the sequence is called slicing. The specific syntax is:


Both the start and end indexes are optional, and if none is provided or indexed , the slice operation starts at the beginning of the sequence or ends at the end of the sequence.

For positive index: 0 <= index <= len (sequence)-1

For negative indexes:-len (sequence) <= index <=-1

The difference between positive and negative indexes is that the positive index starts with the beginning of the sequence and the negative index begins with the end of the sequence.

5. Slicing with step index for extended operation

s = ' abcdef ' s[::-1] #可以视作 "Flip" Operation S[::2]  #隔一个取一个的操作

6. More content of the slice index

Prints a string pyramid with the number of characters decreasing by one, such as String s= ' ABCDE '

>>> s = ' ABCDE ' >>> for I in range (Len (s), 0,-1): ...     Print S[:i]...abcdeabcdabcaba

If you use a negative index, you might have a situation like this:

>>> s = ' ABCDE ' >>> i = -1>>> for i in range ( -1,-len (s),-1): ...     Print S[:i]...abcdabcaba

This makes it not possible to print all the strings first. The solution is to use none as an index to represent the beginning or end of a string.

>>> s = ' ABCDE ' >>> i = -1;>>> for i in [None] + range ( -1,-len (s),-1): ...     Print S[:i]...abcdeabcdabcaba
Sequence type built-in functions

Type conversion function

List (ITER): Converts an Iterative object to a list.

STR (obj): Converts the Obj object to a string (the string representation of the object).

Unicode (obj): Converts an object to a Unicode string.

Tuple (): Converts an iterative object into a Narimoto group object.

operable functions

Enumerate (ITER): Accepts an iterative object as a parameter that returns a enumerate object that generates a tuple of index and item values for each element of ITER.

Len (seq): Returns the length of the seq.

Max or min: Returns the maximum value in ITER, if the specified key, then key must be a parameter that can be passed to the sort () method for comparison of the callback function.

Reversed (seq): Reverses a sequence.

Sorted (): Accepts an object that can be iterated, returning an ordered list.

sum () and zip () functions.

Python learning notes: Python sequence

Related Article

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: 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.