The path to python growth-the first day, the path to python Growth

Source: Internet
Author: User

The path to python growth-the first day, the path to python Growth
Sequence includes (list, tuples, strings, buffer objects, xrange objects)
1. List
List is the data structure that processes a group of ordered projects. You can store a series project in a list. Project in the list. The items in the list should be included in square brackets so that python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete projects, we say that the list is a variable data type, that is, this type can be changed and the list can be nested.2. tuples
The ancestor is very similar to the list, but the tuples are immutable. That is, you cannot modify the tuples. The tuples are defined by commas (,) in parentheses. Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change. Tuples can be nested.3. Dictionary
The dictionary is similar to the address book in which you use the contact name to find the address and contact details. That is, we associate the key (name) and value (details) together. Note that keys must be unique, as if two people happen to have the same name, you cannot find the correct information.
Key-value pairs are marked in the dictionary in this way: d = {key1: value1, key2: value2 }. Note that their key/value pairs are separated by colons, and each pair is separated by commas, all of which are included in curly brackets. Remember that the key/value pairs in the dictionary have no order. If you want a specific sequence, you should sort them before use.4. IndexThe index value can be positive or negative. The positive index starts from 0 and ranges from left to right. The negative index starts from-1 and from right to left. When a negative index is used, python starts counting from the last element. The position number of the last element is-1; >>> str = "test" >>> str [1] 'E' >>> str [-1] 'T' string can be directly indexed >>> "zhangyan" [3] 'n'5 partsData format for accessing a certain range: Slice uses two indexes separated by colons. Note:. The first index is the index value of the first element, (the second index-1) is the index value of the last element >>> data [1, 2, 3, 4, 5, 6, 7, 9] >>> data [] takes 1st to 5th elements, that is, data [1] to data [5] [2, 3, 4, 5, 6] >>> data [-5:-2] corresponds to data [-5] to data [-3] [4, 5, 6] B. If you extract all on the right, you can neither write the right index nor write the right index to the next index of the last element during partitioning. >>> data [3:] [4, 5, 6, 7, 9] >>> data [] the last element is data [9], so the index is 9 [4, 5, 6, 7, 9] c. If you extract all the data on the left, you can write 0 or no data on the left-side index during sharding. >>> data [0: 4] [1, 2, 3, 4] >>> data [: 4] [1, 2, 3, 4] d. Set step size: If the step size is not specified, 1 can be taken or the step size can be specified. The step size cannot be0The step size can also be a negative number, and the extracted part can be output in reverse order. >>> Data [1: 2] [2, 4, 6, 9]Note:If the step size is positive, the first index is smaller than the second index.If the step size is negative, the first index is larger than the second index.
>>> Data [-1:-5:-2] [9, 6] >>> data [5: 1:-2] [6, 4]6. sequences can be added together.>>> A = [, 3] >>> B = [, 6] >>> a + B [1, 2, 3, 4, 5, 6]7. MembershipUse keywordsInCheck whether the value is in the sequence. If "True" is returned in the sequence, otherwise "False" is returned ". Values can be elements or subsequences.
>>> Str = "zhangyan" >>> 'any' in strTrue >>> mystr = "yan" >>> mystr in strTrue8Length, maximum value, minimum value >>> data = [,] >>> len (data) 10 >>> max (data) 131234 >>> min (data) 19 list can convert a string to a list>>> List ("zhangyan") ['Z', 'h', 'A', 'n', 'G', 'y', 'A ', 'N']10 list operations>>> Data [0, 4, 0, 5, 1] >>> del data [4] >>> data [0, 4, 0, 5] >>> str = list ("zhang yan") >>> str ['Z', 'h', 'A', 'n', 'G ', '', 'y', 'A', 'n'] >>> str [6:] = list (" jie ") >>> str ['Z ', 'H', 'A', 'n', 'G', '', 'J', 'I ', 'E'] >>> str [0: 0] = list ("my name is") >>> str ['M', 'y', '', 'n ', 'A', 'M', 'E', '', 'I', 's', 'z', 'h', 'A', 'n ', 'G', '', 'J', 'I', 'E']11 List MethodAppend: Append a new element to the end of the list. After the List calls this method, it is directly modified by this method;
>>> Test = list ("1") >>> test ['1'] >>> test. append ("my name is") >>> test ['1', 'My name is '] appends a string element >>> test. append (list ("zy") >>> test ['1', 'My name is ', ['Z ', 'y'] append a list element >>> test [2] [0] 'Z' Count: Used for statisticsAn elementNumber of times displayed in the list; >>> test ['1', 'My name is ', ['Z', 'y'], 'A', 'A ', 'A'] >>> test. count ('A') 3 >>> test. count ('y') y is not an element 0Extend: Append Multiple Elements in another list at the end of the list. This method can also directly modify the original list, which is also different from the list connection operation (+, equivalent to + =; >>> test. extend (list ("abc") >>> test ['1', 'My name is ', ['Z', 'y'], 'A ', 'B', 'C'] >>> test ['1', 'My name is ', ['Z ', 'y'] >>> test = test + list ("abc") >>> test ['1', 'My name is ', ['Z ', 'y'], 'A', 'B', 'C'] >>> test + = list ("def") >>> test ['1 ', 'My name is ', ['Z', 'y'], 'A',' B ', 'C', 'D', 'E ', 'F'] Three Red annotation methods are equivalent.
Index: Locate the index that appears for the first time for a match. >>> test ['1', 'My name is ', ['Z', 'y'], 'A ', 'B', 'C', 'D', 'E', 'F']> test. index ('A') 3Insert: Insert a new object into the list; >>> test. insert (2, list ("web") >>> test ['1', 'My name is ', ['w', 'E',' B '], ['Z', 'y']Pop: Remove a value from the list (the last one by default) and return the value of this element; >>> test ['1', 'My name is ', ['w ', 'E', 'B'], ['Z', 'y']> test. pop () ['Z', 'y'] >>> test ['1', 'My name is ', ['w', 'E ', 'B'] >>> test. pop (1) 'My name is '>>> test ['1', ['w', 'E',' B ']Remove: Removes the first match in a Value List. >>> test ['1', ['w', 'E', 'B'], 'A ', 'B', 'C'] >>> test. remove ('A') >>> test ['1', ['w', 'E', 'B'], 'B', 'C']Reverse: Stores the values in the list in reverse direction; >>> test = [1, 2, 3, 4, 5] >>> test. reverse () >>> test [5, 4, 3, 2, 1]Sort: Sort the list. >>> test [3, 6, 1, 2, 3, 9, 7] >>> test. sort () >>> test [1, 2, 3, 3, 6, 7, 9] >>> lst ['asdf ', 'qwersf', 'aaf ', 'S ', 'afasfd', 'qre']> lst. sort () >>> lst ['aaf', 'afasf', 'asdf ', 'qre', 'qwersf', 's'] >>> lst. sort (key = len) >>> lst ['s ', 'aaf', 'qre', 'asdf', 'afasf', 'qwerf']SortedBuilt-in functions: sorts the sequences in reverse order and returns a list.
The difference between sort () and sorted () is that sort rearranges the list in the original position, while sorted () generates a new list.

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.