Python Basics-General sequence operations

Source: Internet
Author: User

Python continues

Python contains 6 built-in sequences, each of which are: list, tuple, string, Unicode string, buffer object, and Xrange object. We are going to introduce it gradually.

Today, we mainly introduce the general sequence operation. Universal.

1 Index
All programs apes know that index subscripts start from scratch. The same is true for Python, where index 0 points to the first element in a sequence.

But compared to C + +. Python also adds a negative index . The index of the last 1 elements is-1, right-to-left is-2. -3 ...

String literals can be indexed directly:

>>>‘Hello‘[1]e

2 Shards
Shards are implemented by separating two indexes with a colon.

>>>str‘write.blog.csdn.net‘>>>str[6,10]blog

It is important to note that the first index is the number of the first element to extract, and the last index is the number of the first element of the remainder after the Shard.

An elegant shortcut:
A sequence of known, three elements of access

>>>number = [1,2,3,4,5,6,7,8,9,10]

Law One:

>>>number[7:10][8,9,10]

Law II (failure):

>>>number[-3:-1][8,9] >>>number[-3:0][]

Description: The leftmost index in the Shard is now in the sequence that is later than its right, and the result is an empty sequence.

Fahsarm

>>>number[-3:][8,9,10]

Description: Assuming that the portion of the Shard contains the element at the end of the sequence, the last index will be empty.

The first index is empty:

>>>number[:3][1,2,3]

Two indexes are empty, copying the entire sequence:

>>>number[:][1,2,3,4,5,6,7,8,9,10]

shards of three parameters:
The third parameter is the step size

>>>number[::4][1,5,9]

3 Sequence Addition
Don't add different types of sequences

>>>[1,2,3]+[4,5,6][1,2,3,4,5,6]

4 Sequence Multiplication
The number x is multiplied by a sequence, resulting in a new sequence that repeats the original sequence x times:

>>>‘Python‘*5‘PythonPythonPythonPythonPython‘

5 Membership
Checks whether a value is in the sequence. In operator , returns TRUE or False

‘rw‘>>>‘w‘in permissionsTrue

6 length, minimum value, maximum value
Built-in Len min Max is particularly useful.


There is no difference between this and C + +, do not repeat it.

Python Basics-General sequence operations

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