Python Sequence list and tuple common methods and precautions, pythontuple

Source: Internet
Author: User

Python Sequence list and tuple common methods and precautions, pythontuple

Sequence

Sequence is a set of ordered objects. A sequence can contain one or more elements, or contain no elements.

The basic data types we mentioned earlier can all be used as sequence objects. The object can also be another sequence. There are two types of sequences: list (table) and tuple (tuples ).
The main difference between list and tuple is that, once created, each element of tuple cannot be changed, and each element of list can be changed.

List

Obtain the number of list elements:

Copy codeThe Code is as follows:
>>> Lst = ['Update slowness ', 'python', 5.44, False]
>>> Len (lst)
4

The index starts from 0 when the access is referenced. Do not cross-border the index:

Copy codeThe Code is as follows:
>>> Lst [0]
'Update slowness'
>>> Lst [1]
'Python'
>>> Lst [2]
5.44
>>> Lst [3]
False
>>> Lst [4]
Traceback (most recent call last ):
File "<pyshell #30>", line 1, in <module>
Lst [4]
IndexError: list index out of range

Use-1 as the index to directly obtain the last element:
Copy codeThe Code is as follows:
>>> Lst [-1]
False
>>> Lst [-2]
5.44
>>> Lst [-3]
'Python'
>>> Lst [-4]
'Update slowness'
>>> Lst [-5]
Traceback (most recent call last ):
File "<pyshell #35>", line 1, in <module>
Lst [-5]
IndexError: list index out of range

Because list is a variable ordered table, you can append an element to the end of the list:
Copy codeThe Code is as follows:
>>> Lst. append ('add me one ')
>>> Lst
['Update slowness ', 'python', 5.44, False, 'add me one']

Append multiple elements at a time:
Copy codeThe Code is as follows:
>>> Lst. extend (['A', 'B', 'C'])
>>> Lst
['Update slowness ', 'python', 5.44, False, 'add me A', 'A', 'B', 'C']

To delete the element at the end of the list, use the pop () method:
Copy codeThe Code is as follows:
>>> Lst. pop ()
'C'
>>> Lst
['Update slowness ', 'python', 5.44, False, 'add me A', 'A', 'B']

Delete the element at the specified position and use the pop (I) method, where I is the index position:
Copy codeThe Code is as follows:
>>> Lst. pop (0)
'Update slowness'
>>> Lst
['Python', 5.44, False, 'add me one ', 'a']

Replace the list element, which can be directly assigned to the corresponding index location:
Copy codeThe Code is as follows:
>>> Lst [-1] = '000000'
>>> Lst
['Python', 5.44, False, 'add me one ', '123']

The list element can also be another list. The inserted list is regarded as only one element:
Copy codeThe Code is as follows:
>>> Lst. append (lst1)
>>> Lst
['Python', 5.44, False, 'add me one ', '123', ['123', 'qwer']
>>> Len (lst)
6

Tuple

Once initialized, Tuple cannot be modified. A string is a special element, so you can perform operations on tuples.

Copy codeThe Code is as follows:
>>> Str = 'It's time to go to bed. Good night! '
>>> Print (str [: 7])
It's time to go to bed. Good night.

The immutable tuple makes the code safer because it is immutable. If possible, use tuple whenever possible.
Copy codeThe Code is as follows:
>>> Tuple = ('1', '2', '3 ')
>>> Tuple [0] = 6
Traceback (most recent call last ):
File "<pyshell #62>", line 1, in <module>
Tuple [0] = 6
TypeError: 'tuple' object does not support item assignment

Define an empty tuple:
Copy codeThe Code is as follows:
>>> Tuple1 = ()
>>> Tuple1
()

Note: To define a tuple with only one element:
Copy codeThe Code is as follows:
>>> Tuple2 = (666 ,)
>>> Tuple2
(666,) # correct
>>> Tuple3 = (666)
>>> Tuple3
666 # incorrect. Only 666 is defined.

Note: Tuple's so-called "unchanged" means that every element of tuple is always pointed.

Copy codeThe Code is as follows:
>>> L = ['CCTV-5', 'Hi']
>>> Tuple4 = ('uvo', 'hink', l)
>>> Tuple4
('Ufos ', 'hink', ['CCTV-5', 'Hi'])
>>> L [1] = 'change First'
>>> Tuple4
('Ufos ', 'hack', ['CCTV-5', 'change First'])

So try to avoid this application.

Summary of the day

Measure the test taker's knowledge about the common methods and precautions of List and Tuple.

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.