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) |