python--Data structure sequence

Source: Internet
Author: User

What is a sequence

The sequence is the basic data structure of python, and each element in the sequence is assigned an ordinal index, starting at 0.

Two common types of sequences: Lists and tuples.

The difference between a list and a tuple: The list can be modified and tuples cannot be modified.

General sequence Operations

1, Index: index 0 points to the first element, index-1 points to the last element.

2, Program: Enter the year, month (1-12), Day (1-31), and then print out the corresponding date of the month name.

code Explanation: months=[]: Defines a months sequence.

endings=[' st ', ' nd ', ' rd ']+17*[' th ']\

+[' st ', ' nd ', ' rd ']+7*[' th ']\

+[' St ': Defines a endings sequence that represents the abbreviation for 1-31, 1st,2nd,3rd,4-20th,21st,22nd,23rd,24-30th,31st.

Code Run output Result:

3. Shard: Extracts an element within a range of a sequence.

Shard Tag[9:30]: The first index number 9 is contained within the Shard, and the second index number 30 is not contained within the Shard.

4. What if you want to go to the last element in the sequence?

the last element can be obtained by placing an empty, last index. The first element can be obtained by placing the last index in the empty position. You can have both sides empty to get the entire element.

5. Step Length:

Positive step: Extracts the element to the right from the head of the sequence until the last element.

Negative step: Extracts an element to the left from the end of the sequence until the first element.

6. Sequence Operation

Sequence addition: The sequence is connected, and the list and string cannot be joined together.

sequence Multiplication: The number x is multiplied by a sequence to generate a new sequence, that is, the original sequence is repeated x times.

None, empty list and initialize: Initializes a list of length 10.

Code Analysis: Print a box of characters on the screen that is centered on the screen and automatically resized according to the sentences entered by the user.

membership: In operator, enter a Boolean operator to return True when the condition is true, false to return false.

the above code in a UNIX system, you can check the file writable and executable permissions of the script.

The above code can check whether the user name entered is present in the user list.

The above code can be used as part of the filtering spam message.

Code Analysis: View user entered user name, whether the password exists in the database, print ' Access granted ' if it exists

program Run Result:

built-in function: Len: Returns the number of elements contained in the sequence, min: Returns the smallest element in the sequence, Max: Returns the largest element in the sequence.

python--Data structure sequence

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.