Basic python tutorial _ Study Notes: sequence-1

Source: Internet
Author: User

Sequence

Data structure: a collection of data elements that are organized together in a certain way. These data elements can be numbers or characters,It can even be another data structure.


In python, the most basic data structure is sequence.

Each element in the sequence is assigned a sequence number, that is, the position of the element, also known as the index. The first index is 0, the second index is 1, and so on.

Sequence Overview

Python contains six built-in sequences: List, tuples, strings, Unicode strings, buffer objects, and xrange objects.

The main difference between a list and a group is that the list can be modified, and the tuples cannot.

The reason for using the latter is generally technical, and it is related to the internal operation mode of python. This is also why built-in functions may return tuples.

In general, in almost all cases, the list can replace the tuples. One of the exceptions needs to be noted: Use tuples as Dictionary keys. In this case, the list cannot be used because the key cannot be modified.

General sequence operations

All sequence types can perform certain operations. These operations include indexing, partitioning, adding, multiplication, and checking whether an element belongs to a Sequence member (membership ). In addition, python also provides built-in functions for calculating the sequence length and finding the maximum and minimum elements.

Note: Another important operation is iteration, which will be available in the future.

Index

All the elements in the sequence are numbered-starting from 0. These elements can be accessed separately by serial numbers. When a negative index is used, python starts counting from the right, that is, the last element. The position number of the last element is-1 (remember: Not 0)

String

>>> Greeting = 'hello'

>>> Greeting [0]

'H'

>>> Greeting [-1]

'O'

>>> Greeting [1]

'E'

>>> Greeting [-2]

'L'

The string literal value can also be indexed.

>>> 'Hello' [-1]

'O'

>>> 'Hello' [-2]

'L'

List

>>> Greeting = ['hello', 'World', 'you', 'and', 'I']

>>> Greeting [0]

'Hello'

>>> Greeting [-1]

'I'

Tuples

>>> Greeting = ('hello', 'World', 'you', 'I ')

>>> Greeting [0]

'Hello'

>>> Greeting [-2]

'You'

If a function call returns a sequence, you can index the returned results directly.

>>> Forth_num = raw_input ("Please input a num (lenght> 4):") [3]

Please input a num (lenght> 4): 20140508

>>> Forth_num

'4'

Parts

You can use the sharding operation to access elements within a certain range and use two indexes separated by colons.

String

>>> Greeting = 'hello, world! '

>>> Greeting [0: 3]

'Hel'

List

>>> Greeting = ['hello', 'World', 'you', 'and', 'I']

>>> Greeting [1: 3]

['World', 'you']

Tuples

>>> Greeting = ('hello', 'World', 'you', 'I ')

>>> Greeting [0: 3]

('Hello', 'World', 'you ')

We can see that the first index in a shard is the number of the first element to be extracted, and the second index is the number of the remaining 1st elements after the shard. In short, the implementation of the sharding operation requires two indexes as the boundary. The first index is included in the shard, and the first index is not included in the shard.

Elegant shortcuts

What are the last elements of the access sequence? Of course, you can perform the following operations explicitly:

>>> Greeting = ['hello', 'World', 'you', 'and', 'I']

>>> Greeting [1: 5]

['World', 'you', 'and', 'I']

The elements pointed to by index 5 do not exist. This method is feasible. But what if we need to start counting from the end of the list?

>>> Greeting [-4:-1]

['World', 'you', 'and']

>>> Greeting [-4: 0]

[]

This is not the expected result.

In fact, as long as the leftmost index of a shard appears later than the rightmost index, the result is an empty sequence. You can use a shortcut: if the part obtained contains the elements at the end of the sequence, leave the last index blank:

>>> Greeting [-4:]

['World', 'you', 'and', 'I']

Elements that can also be used for sequence start:

>>> Greeting [: 3]

['Hello', 'World', 'you']

Similarly, to copy the entire sequence, you can leave two indexes blank (the colon cannot be omitted ):

>>> Greeting [:]

['Hello', 'World', 'you', 'and', 'I']

Larger step size

For sharding, the start and end points of the sharding must be specified. Another parameter-step size-is usually set implicitly. In a common part, the step size is 1-the part Operation traverses the elements of the sequence one by one based on the step size, and then returns all elements between the start and end points.

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

>>> Numbers []

[1, 3, 5, 7, 9]

Shortcuts still apply here. Extract the first of every four elements:

>>> Numbers [: 4]

[1, 5, 9]

The step size cannot be 0, because it cannot be performed downward:

>>> Numbers [: 0]

Traceback (most recent call last ):

File" ", Line 1, in

Numbers [: 0]

ValueError: slice step cannot be zero

The step size can be a negative number, that is, elements are extracted from the right to the left:

>>> Numbers [8: 3:-1]

[9, 8, 7, 6, 5]

>>> Numbers [:-2]

[10, 8, 6, 4, 2]

>>> Numbers [: 5:-2]

[10, 8]

>>> Numbers [5:-2]

[6, 4, 2]

>>> Numbers [3: 8:-1]

[]

Remember: The leftmost index of a shard appears in the sequence later than the rightmost index, and the result is an empty sequence.

When a negative number is used as the step size, the start point must be greater than the end point. When the start and end points are not explicitly specified, the use of positive and negative numbers may cause some confusion.

In short, for a positive step, python advances the element from the header of the sequence to the right until the last element;

For negative step numbers, extract elements from the end of the sequence to the left until the first element.

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.