List and tuple

Source: Internet
Author: User
List

A list is an ordered collection in which the element data types can be different. You can add and remove elements at any time.
1. Define

>>> list = []
>>> list
[]
>>>

2. Add/Remove Append element to end of list

>>> list.append (1)
>>> list
[1]
>>> list.append (' a ')
>>> list
[1, ' a ']
>>>
Inserts an element into a specified location
>>> List.insert (1, one)
>>> list
[1, one, ' a ']
>>>
Delete the element at the end of the list
>>> list.pop ()
' a '
>>> list
[1, one]
>>> l
Deletes the element at the specified location
>>> list.pop (0)
1
>>> list
[one]
>>>
To replace an element with another element
>>> list
[one]
>>> list[0]=111
>>> list
[+]
>>>
List can contain another list
>>> list
[a]
>>> list2=[' a ']
>>> list2
[' a ']
>>> List2.append (list)
>>> list2
[' A ', []]
>>>

3. Commonly used functions to calculate the length of the list

>>> len (list)
1
>>> len (list2)
2
>>>
tuple

Tuple: Tuple, also a sequence table. Tuple can not be modified once initialized. If an element in tuple is a list, the list can be changed.
1. Define

Define an empty tuple
>>> t= ()
>>> T
()
>>>

define a tuple > with only one element
>> t= (1) #错误, this time () represents the parentheses in the mathematical formula
>>> T
1
>>> t= (1,)
>>> t
(1, )
>>>

2. Commonly used functions to calculate the length of tuple

>>> len (t)
1
>>>

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.