Python tuples and python group operations

Source: Internet
Author: User

Python tuples and python group operations

Reference document: click here

Tuples: the python tuples (Tuple) are similar to the List. The difference is that the tuples cannot be modified. The tuples use parentheses and the List uses square brackets.

Tuples:Unchangeable

Create a tuples. The instance is as follows:

1 tup1 = ("hello", "world", "ni hao") 2 tup2 = ("1", "2", "3") 3 tup3 = "", "B", "c" 4 tup4 = () # Empty tuples

Access tuples. The instance is as follows:

1 tup1 = ("hello","world","ni hao")2 tup2 = ("1","2","3")3 print tup1[1]4 print tup2[0:]5 print tup2[1:2]6 print tup2[-1]

Instance output result:

1 'world'2 ('1','2','3')3 ('2',)4 ('3')

Modify tuples:

Tuples can only be connected and combined because they cannot be modified.

1 a=(1,2,3)2 b=(2,3,4)3 a+b

Output result:

1 (1, 2, 3, 2, 3, 4)

Delete tuples:

The entire tuples can only be deleted because they cannot be modified.

1 a=(1,2,3,4)2 print a3 del a4 print a

Output result:

Tuples:

1. Iteration

1 tup = (1, 2, 3) 2 # iteration 3 for a in tup: 4 print a 5 6 # determine whether the element exists 7 a = 3 8 if a in tup: 9 print 'element % d is exist' % (a) 10 else: 11 print 'element % d is not exist' % () 12 13 copy 14 tup1 = tup * 415 16 get length 17 len (tup)

2. built-in functions

 

Method Name Method Overview Result
Cmp (1, 2, 3), (2, 3, 4 )) Returns 0 if the two tuples are consistent, and-1 if the two tuples are inconsistent. -1
Len (1, 2, 3 )) Obtain the length of a tuples 3
Max (1, 2, 3 )) Obtains the maximum element of a tuples. 3
Min (1, 2, 3 )) Obtain the minimum element of a tuples. 1
Tuple ('000000 ') Convert string into tuples (1, 2, 3)

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.