Python Learning Notes (v)--list and tuple

Source: Internet
Author: User

A, list 1, definition:

List is an ordered set of elements that can be added and removed at any time

2. Declaration method:
subjects=['Math','中文版'Chinese  ']

3. Some API (1) Gets the number of list elements
Len (subjects)

(2) using an index to access the elements of each position in the list, remember that the index is from 0Started with
Subjects[0]

PS: If you want to take the last element, in addition to the index location, you can also be -1 indexed, directly get the last element, -2 is the penultimate, and so on.

(3) Append the element to the list to the end:
Subjects.append ('Music')

(4) inserting the element into the specified position
' Music ')

(5) Delete the element at the end of the list
Subjects.pop ()

(6) To delete the element at the specified position
Subjects.pop (i)

(7) to replace an element with another element, you can assign a value directly to the corresponding index position
' Music '

(8) The data type of the elements in the list can be different

(9) The IST element can also be another list
p = ['asp''php'= ['python'  'java'scheme']s[2][1]  # equivalent to a 2-D array

Second, tuple1, definition

Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but a tuple cannot be modified once initialized

2. Statement
subjects= ('Math','中文版'Chinese  ')

3, the advantages of the tuple

The tuple is immutable, so the code is more secure

4. Tuple traps

(1) When you define a tuple, the elements of the tuple must be determined at the time of definition.

(2) When you define a tuple of only one element, you must write to the following format, otherwise the operation is performed () by default.

Tuplee = (1,)

(3) A tuple refers to the data that is pointing to the same, that is, when a tuple contains a list

Python Learning Notes (v)--list and tuple

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.