Python learning notes (9) Python tuples and dictionaries (1), python learning notes

Source: Internet
Author: User

Python learning notes (9) Python tuples and dictionaries (1), python learning notes

Python tuples

Tuple is a Python object type and a sequence.

The tuples in Python are similar to the list. The elements of the tuples cannot be modified.

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

Create a tuples, that is, add elements in parentheses and separate them with commas (,).

1 >>> a = 123,"aaa",["python","pass"]2 >>> a3 (123, 'aaa', ['python', 'pass'])4 >>> type(a)5 <type 'tuple'>6 >>> print "I love %s,and I am a %s"%("Python","programmer")7 I love Python,and I am a programmer

Tuples are a sequence of basic operations such as len (), +, *, in, max (), min (), and cmp ()

Conversion between tuples and Sequences

Tuples cannot be modified.

1 >>> a = (, 3) 2 >>> id (a) # compare the two tuples a and B, is two different objects 3 44307080L 4 >>> B = (1, 3, 2) 5 >>> id (B) 6 48682136l 7 >>> a 8 (1, 2, 3) 9 >>> len (a) # Calculate the length of 10 311 >>> b12 (1, 3, 2) 13 >>> a + B # connect the two tuples together 14 (1, 2, 3, 1, 3, 2) 15 >>> a * 3 # repeat a triple three times for 16 (1, 2, 3, 1, 2, 3, 1, 2, 3) 17 >>> 3 in a # judge whether the element 3 is 18 True19 in the tuples >>> 4 in a # judge whether the element 4 is 20 False21 in the tuples>> max () # calculate the maximum value of 22 323> min (a) # Calculate the minimum value of 24 125> cmp (a, B) # compare the size of tuples a and B 26-127 >>> alst = list (a) # convert tuples to list 28 >>> alst29 [1, 2, 3] 30 >>> c = tuple (alst) # convert the list to tuples 31 >>> c32 (1, 2, 3) 33 >>> a34 (1, 2, 3) 35 >>>. append (4) # append an element to the tuples. The tuples cannot append 36 Traceback (most recent call last): 37 File "<stdin>", line 1, in <module> 38 AttributeError: 'tuple' object has no attribute 'append' # The tuples do not have an attribute append39 >>> dir (tuple) # dir view the tuples, only count index40 ['_ add _', '_ class _', '_ ins INS _', '_ delattr __', '_ doc _', '_ eq _', '_ format _', '_ ge _', '_ getattribute __', '_ getitem _', '_ getnewargs _', '_ getslice _', '_ gt _', '_ hash __', '_ init _', '_ iter _', '_ le _', '_ len _', '_ lt __', '_ mul _', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex __', '_ repr _', '_ rmul _', '_ setattr _', '_ sizeof _', '_ str __', '_ subclasshook _', 'Count', 'index'] 41 >>> dir (list) 42 ['_ add __', '_ class _', '_ ins INS _', '_ delattr _', '_ delitem _', '_ delslice __', '_ doc _', '_ eq _', '_ format _', '_ ge _', '_ getattribute __', '_ getitem _', '_ getslice _', '_ gt _', '_ hash _', '_ iadd __', '_ imul _', '_ init _', '_ iter _', '_ le _', '_ len __', '_ lt _', '_ mul _', '_ ne _', '_ new _', '_ reduce __', '_ performance_ex _', '_ repr _', '_ reversed _', '_ rmul _', '_ setattr __', '_ setitem _', '_ setslice _', '_ sizeof _', '_ str _', '_ subclasshook __', 'append', 'Count', 'extend', 'index', 'insert', 'pop', 'delete', 'reverse', 'sort ']

The index and slice of tuples, similar to the list and string.

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

1> a 2 (1, 2, 3) 3 >>> a [0] # retrieve element 4 1 5 >>>> a [1] 6 2 7 >>> a [2] 8 3 9 >>> a [1:] # extract elements 10 (2, 3) 11 >>> a [0: 2] 12 (1, 2) 13 >>> [:: -1] # reverse the tuple a by 14 (3, 2, 1) 15 >>> alst [1] = 100 # Add the element alst [1] 16 >>> alst17 [1,100, 3] 18 >>> a [1] = 100 # element 19 Traceback (most recent call last): 20 File "<stdin> ", line 1, in <module> 21 TypeError: 'tuple' object does not support item assignment tuples do not support modification 22 >>> temp = list () # convert a to a list and store it in the temp Temporary Variable 23> temp [1] = 100 # Add 100 to the location where the temp index in the list is 1 24> = tuple (temp) # convert temp to tuples 25 >>> a # implement conversion between tuples and lists 26 (1,100, 3) 27 >>> [1] # A separate [1] is a list 28 [1] 29 >>> type ([1]) 30 <type 'LIST'> 31 >>> type (1) # Separate (1) is an integer of 32 <type 'int'> 33 >>> type (1,) # Separate (1,) is a tuples, when the tuples contain only one element, you need to add a comma 34 <type 'tuple'> 35 >>>

Count () and index () of tuples ()

1 >>> a 2 (1,100, 3) 3 >>> B = a * 3 4 >>> B 5 (1,100, 3, 1,100, 3, 1,100, 3) 6 >>> B. count (1) # count the number of times 1 appears 7 3 8> B. index (3) # Calculate 3 the first occurrence location 9 2

Significance of tuples

Faster tuples than list operations

Write protection for data because the tuples cannot be modified

It can be used in string formatting.

Can be used as the dictionary key

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.