Python's strongest King (7) -- tuple and pythontuple

Source: Internet
Author: User

Python's strongest King (7) -- tuple and pythontuple

1. sequence ):

Note: In fact, we have used sequences in the previous string list. The reason why we put them here is mainly to help you understand and remember them.

Python Data Access Model: direct access, sequence, and ing

  • Non-container classes can be directly accessed, and all numeric types are classified as such.
  • Sequence type refers to the sequential access of elements in the container from 0 to one or more elements at a time. Returns the string, list, And tuples to the secondary class.
  • The difference between the ing type and the sequence type is that it uses different indexes and sequential numeric offsets. Its elements are stored unordered and accessed by unique keys. Dictionary is of this type.

2. Python tuples

The Python tuples are similar to the list, except that the elements of the tuples cannot be modified.

2.1 create tuples

The tuples use parentheses, And the list uses square brackets.

Creating tuples is simple. You only need to add elements in brackets and separate them with commas.

Example 1:

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: 2016/9/11 20:54 # @ Author: wwyxtup1 = ("python", "java ", "c", "c ++") tup2 = ("demassiya", "no", "") tup3 = ("", "", "Children's robbery", "baby fish", "Elementary School Monk") print "tup1 is as follows:" for var1 in tup1: print var1print "tup2 is as follows:" for var2 in tup2: print var2print "tup3:" for var3 in tup3: print var3

Example 1 running result:

The tup1 is as follows: pythonjavacc ++ tup2 is as follows: what about demaxa nosi? tup3 is as follows: Withdrawal Ji chushou suo children robbery baby fish Primary School monk

2.2 create null tuples

When only one element is contained in a tuple, you must add a comma after the element.

Tuples are similar to strings. subscript indexes start from 0 and can be truncated or combined.

Example 2

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: 2016/9/11 20:54 # @ Author: wwyxtup1 = () # create an empty tuples tup2 = ("",) # create a tuple print tup1print tup2 [0]

Example 2 running result:

() What about IOU?

Note: The element traversal in the tuples is traversed Based on the Traversal method of the sequence. If tup2 is printed directly in the preceding example, tup2 is returned in the memory.

2.3 access tuples

  • Tuples can be accessed through for iteration, as shown in Example 1.
  • The access to tuples can also be traversed through the sequence index, as shown in example 2.

2.4 modify tuples

Note: element values in tuples cannot be modified, but we can concatenate and combine them.

Example 3:

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: 2016/9/11 20:54 # @ Author: wwyxtup1 = ("python", "java ", "c", "c ++") tup2 = ("demassiya", "no", "") tup3 = ("", "", "Children's robbery", "baby fish", "Elementary School Monk") tup4 = tup1 + tup2 + tup3print "tup4 is as follows:" for var1 in tup4: print var1

Example 3 running result

The tup4 is as follows: pythonjavacc ++ what about demaxa Nova? withdraw money from Ji chushou suo children's robbery baby fish Elementary School monk

2.5 Delete tuples

Note: The element values in the tuples cannot be deleted, but we can use the del statement to delete the entire tuples.

Example 4

Tup2 = ("demassia", "NOVA", "") tup3 = ("withdrawal Ji", "nursery Suo", "Children robbery", "baby fish ", "Elementary School Monk") print "tup2 is as follows:" for var1 in tup2: print var1del tup2print "the deleted tup2 is as follows: "print tup2 [2] del tup3 [2] # Delete the element whose index of tup3 is 2

Example 4 running result

Tup2 is as follows: Traceback (most recent call last): demassiya File "E:/python/hello/untitled3/tuple. py ", line 14, in <module> Nova print tup2 [2] What about IOU? NameError: name 'tup2' is not defined the deleted tup2 is as follows:

3. tuples Operator

Like a string, you can use the plus sign (+) and minus sign (*) to perform operations between tuples. This means they can combine and copy, and a new tuples will be generated after the operation.

Python expressions Result Description
Len (1, 2, 3 )) 3 Calculate 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

4. tuples index, truncation

Because tuples are also a sequence, we can access the elements at the specified position in the tuples, or intercept some elements in the index, as shown below:

Tuples:

L = ('spam', 'Spam', 'SPAM!')
Python expressions Result Description
L [2] 'Spam! ' Read the third element
L [-2] 'Spam' Reverse reading; reads the second to last element
L [1:] ('Spam', 'spam! ') Truncation Element

5. No Delimiter is closed

Any unsigned objects are separated by commas (,). The default value is tuples,

Example 5

Print "withdrawal Ji", "nursery Suo", "Children robbery", "baby fish", "Elementary School Monk" print "Timo captain", 1, 2, 3, 4

Example 5 running result

Withdrawal Ji chushou suo children's robbery baby fish Elementary School monk Timo Captain 1 2 3 4

Note: Remember this article and try not to use it on your own.

6. built-in functions of tuples

Common built-in functions of Python tuples:

Serial number Method and description
1 Cmp (tuple1, tuple2)
Compares two tuples.
2 Len (tuple)
Calculates the number of tuples.
3 Max (tuple)
Returns the maximum value of elements in the tuples.
4 Min (tuple)
Returns the minimum value of the element in the tuples.
5 Tuple (seq)
Converts a list to a tuples.

 

Example 6

#! /Usr/bin/env python #-*-coding: UTF-8-*-# @ Time: 2016/9/11 20:54 # @ Author: wwyx # tup1 = ("python", "java ", "c", "c ++") tup2 = ("demassiya", "no", "") tup3 = ("", "", "Children's robbery", "baby fish", "Elementary School Monk") var1 = cmp (tup2, tup3) print "comparison result of tup2 and tup3:", var1print "maximum value of tup2 :", max (tup2) print "minimum value of tup3:", min (tup3)

Example 6 running result:

Comparison results of tup2 and tup3:-1tup2's maximum value: the minimum value of Novus tup3: Children's robbery

Now, let's talk about the next dictionary. Come on !!

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.