Python tuples
A python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
The following example:
Tup1 = (' Physics ', ' Chemistry ', 1997, n); tup2 = (1, 2, 3, 4, 5); tup3 = "A", "B", "C", "D";
Create an empty tuple
Tup1 = ();
When you include only one element in a tuple, you need to add a comma after the element
Tup1 = (50,);
Tuples are similar to strings, and subscript indexes start at 0 and can be intercepted, combined, and so on.
accessing tuples
Tuples can use the subscript index to access the values in the tuple, as in the following example:
#!/usr/bin/pythontup1 = (' Physics ', ' Chemistry ', 1997, 3); tup2 = (1, 2,, 4, 5, 6, 7);p rint "tup1[0]:", Tup1[0]print "Tup2[1:5]:", Tup2[1:5]
The result of the above example output:
Tup1[0]: physicstup2[1:5]: [2, 3, 4, 5]
modifying tuples
The element values in the tuple are not allowed to be modified, but we can combine the tuples with the following example:
#!/usr/bin/pythontup1 = (34.56); tup2 = (' abc ', ' xyz '); # The following modification of tuple element operations is illegal. # Tup1[0] = 100;# Create a new tuple Tup3 = Tup1 + Tup2;print tup3;
The result of the above example output:
(34.56, ' abc ', ' XYZ ')
Delete a tuple
element values in tuples are not allowed to be deleted, but we can use the DEL statement to delete an entire tuple, as in the following example:
#!/usr/bin/pythontup = (' Physics ', ' Chemistry ', 1997, $);p rint Tup;del tup;print "after deleting tup:" Print tup;
When the above instance tuple is deleted, the output variable will have exception information, the output is as follows:
(' Physics ', ' Chemistry ', 1997, 2000) After deleting Tup:traceback (most recent call last): File "test.py", line 9, in <module> print tup; Nameerror:name ' tup ' is not defined
Tuple operators
As with strings, you can use the + and * numbers to perform operations between tuples. This means that they can be combined and copied, and a new tuple is generated after the operation.
Python expressions
Results
Describe
Len ((1, 2, 3)) 3 counts the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Connect
[' hi! '] * 4 (' hi! ', ' hi! ', ' hi! ', ' hi! ') copy
3 in (1, 2, 3) True whether the element exists
For x in (1, 2, 3): Print x, 1 2 3 iterations
Tuple index, intercept
Because tuples are also a sequence, we can access the elements at the specified location in the tuple, or we can intercept an element in the index as follows:
Meta-group:
L = (' spam ', ' spam ', ' spam! ')
Python expressions
Results
Describe
L[2] ' spam! ' reads a third element
L[-2] ' Spam ' reverse read; Read the second last element of the countdown
L[1:] [' Spam ', ' spam! '] intercept elements
No close delimiter
Arbitrarily unsigned objects, separated by commas, are tuples by default, as in the following example:
#!/usr/bin/pythonprint ' abc ', -4.24E93, 18+6.6j, ' xyz '; x, y = 1, 2;print "Value of X, y:", x, y;
The above example allows the result:
abc-4.24e+93 (18+6.6j) Xyzvalue of X, Y:1 2
Tuple built-in functions
The Python tuple contains the following built-in functions
Serial number
Method and Description
1 cmp (TUPLE1, Tuple2)
Compares two tuples of elements.
2 len (tuple)
Counts the number of tuple elements.
3 Max (tuple)
Returns the element's maximum value in a tuple.
4 min (tuple)
Returns the element minimum value in a tuple.
5 tuple (SEQ)
Converts a list to a tuple.
The above is the "Python tutorial" python tuple content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!