Python tuple, List intercept

Source: Internet
Author: User

First, the introduction of the meta-group

A tuple of 1.Python is similar to a list, except that elements of a tuple cannot be modified.

2. Tuples use parentheses, and the list uses square brackets. For example:tup = (1, 2, 3, 4, 5 );

3. Tuple creation is simple, just add elements in parentheses and separate them with commas.

4. When you include only one element in a tuple, you need to add a comma after the element.

5. Tuple is similar to string, subscript index starting from 0, can be intercepted, combined and so on.

Two, the use of tuples

1. Value (using subscript)

Tup1[0]

Tup2[1:5] #顾头不顾尾, obtained is subscript 1 to subscript 4 element

2. Modify, the elements in the tuple are not modifiable, but the tuples can be joined together

Tup3 = tup1 + tup2;

3. Delete: del tup

4. Convert a list to a tuple: tuple (list)

Iii. slicing (a method of listing, string, and range values)

1.list[:3] #顾头不顾尾, if the beginning of the subscript is not written, then it is starting from the front

2.list[-1] #-1 represents the last element

3.list[1:] #如果下标不写的话, the representative takes the last

4.list[:] #开头的下标和结尾的下标都不写的话, representing the entire list

5.list[-6:-10:-1] #第二个冒号后面值代表步长, is a number of elements to take once, the step is negative, from right to left to take a value

6.list[::-1] #将列表里的元素都取出来了, but the order is reversed because the step is negative

Python tuple, List intercept

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.