Python learning notes (8) Python list (3), python learning notes

Source: Internet
Author: User
Tags python list

Python learning notes (8) Python list (3), python learning notes

Sequence

Sequence: in mathematics, a sequence is an object (or event) in a column. In this way, each element is either before or after another element. The order between elements is very important. Wikipedia

Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its location, or index. The first index is 0, the second index is 1, and so on.

Python has six built-in sequence types, but the most common is list and metadata.

Operations that can be performed by sequences include indexing, slicing, adding, multiplication, and checking members.

Python has built-in methods for determining the sequence length and the largest and smallest elements.

List and string comparison

1. The list can be modified, and the string cannot be modified.

2. Strings and lists are all sequences.

3. The list can be modified in situ.

4. The list has a multi-dimensional list, but the string does not

5. The string cannot be changed, and the list can be modified in situ.

1 >>> a = [1, 2, 3] 2 >>> id (a) 3 46281096L 4 >>>. append (4) # append an element. If no return value is returned, this is an external representation of in-situ modification. 5 >>> a 6 [1, 2, 3, 4] 7 >>> id (a) # after appending, the position of the list in the memory has not changed 8 46281096L 9> a [1] = 9 # The list can also be modified as follows: Change the element whose index position is 1 to 9, list of modifiable features 10 >>> a11 [1, 9, 3, 4] 12 >>> B = "python" 13 >>> B [1] = "w" # returns an error in a string, 14 Traceback (most recent call last): 15 File "<stdin>", line 1, in <module> 16 TypeError: 'str' object does not support item assignment23 >>> B [0] + "w" + B [2:] # If a service requires string modification, you can do this, however, this modification actually produces another string, the original string does not change 24 'pwthon '25 >>> B # The actual B string does not change 26 'python' 27 >>> a28 [1, 9, 3, 4] # One-dimensional list 29 >>> m = [[1, 2, 3], [, 6, 9] # multidimensional list 30 >>> m [1] 31 [4, 5, 6] 32 >>> m [1] [0] 33 434 >>>

Conversion between list () and str () lists and strings

1 >>> B = "python" 2 >>> B 3 'python' 4 >>> list (B) # convert a string to list 5 ['P ', 'y', 't', 'h', 'O', 'n'] 6 >>> a = "www.baidu.com" 7 >>>. split (". ") # Use the separator for the string. split into list 8 ['www ', 'baidu', 'com'] 9 >>> c = list (B) 10 >>> c11 ['P ', 'y', 't', 'h', 'O', 'n'] 12 >>> "". join (c) # Use the join function to connect List c with null and convert it to a string of 13 'python' 14 >>> "-". join (c) # Use the join function to connect List c with the minus sign 15' p-y-t-h-o-N'

 

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.