Python Automation 3.0-------Learning path--------tuples!

Source: Internet
Author: User

PythonMeta-group

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, 2000); =  (1 2, 3 , 4,  5 =  "a" , "B" ,  "C"   "D"          

Create an empty tuple

=();

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

=(a);  

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, 2000);Tup2= (1, 2, 3,  4, 5,6, 7print  "tup1[0": ",< Span class= "PLN" > Tup1[0] print  "Tup2[1:5": ",[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/python#-*-Coding:utf-8-*-tup1 = ( 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,< Span class= "PLN" > 2000print Tup;del Tup;print  "after deleting tup:"  Tup           

When the above instance tuple is deleted, the output variable will have exception information, the output is as follows:

(' Physics ', ' Chemistry ', 1997, 2000)Afterdeleting Tup:traceback  ( Most recent call last):  file   "test.py" , line 9, in <module>  print Tup;nameerror: name  ' tup '  Span class= "KWD" >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 Description
Len ((1, 2, 3)) 3 Count the number of elements
(1, 2, 3) + (4, 5, 6) (1, 2, 3, 4, 5, 6) Connection
(' 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 Iteration
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:

=(' spam ',' spam ',' spam! ' )
Python Expressions Results Description
L[2] ' spam! ' Reading a third element
L[-2] ' Spam ' Read the second last element of the inverse
L[1:] (' Spam ', ' spam! ') Intercepting 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 result of the above example operation:

-4.24e+93(+6.6j) xyzValue,:12    
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.

Python Automation 3.0-------Learning path--------tuples!

Related Article

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.