Basic knowledge review--general sequence operations

Source: Internet
Author: User

The data structure is the way the computer stores and organizes it. There are three types of four built-in data structures in Python, namely, sequence (List, Tuple), Mapping (Dictionary), and collection (set).

All sequence types can perform certain operations, including index, Shard, add, multiply, iterate, and check whether an element belongs to the members of a sequence (membership), as well as the built-in elements that calculate the length of a sequence, find the largest and smallest elements.

1. Index

1 >>> greeting = ("Hello")2 >>> greeting[1] 3 ' e ' 4 >>> greeting[-2]5'l'

2. sharding

1>>> num = [1,5,4,7, 0] #从左往右分片起始索引为0, right-to-left Shard start index is-1 2>>> Num[1:3]3[5, 4]4>>> num[0:-2]5[1, 5, 4]6>>> num[2:-2]7[4]8 9 #The elements of the start point are included in the result, and the elements of the end point are not within the Shard;Ten # for a positive step, the element is extracted to the right from the beginning of the sequence, until the last element, and for the negative step, extracts the element left from the end of the sequence until the first element  One>>> Num[0:4:2]           A[1, 4] ->>> num[::-2] -[0, 4, 1] the>>> num[2::-2] -[4, 1] ->>> num[:2:-2] -[0]

3. Addition: sequence of the same type for connection operation

1>>> [1,2,3]+[4,5]2[1, 2, 3, 4, 5]3>>>'Hello'+' World'4 'HelloWorld'5>>> [1,2,3]+'Hello'6 7 Traceback (most recent):8File"<pyshell#201>", Line 1,inch<module>9[1,2,3]+'Hello'TenTypeerror:can Only concatenate list ( not "Str") to List

4. Multiplication

1>>>'python,'The2 'Python,python,python,python,python,'3>>> seq = [none]*10#initialize a list of length 104>>>Printseq5 [None, none, none, none, none, none, none, none, none, none]6>>>Print '!'+'Hello, World'+'-'*107!!!!! Hello, World----------

5. Memberships

1>>>'P'inch 'Python'2 True3>>>' P'inch 'Python'4 False5 6>>>'P'inchpython #注意 ' python ' and python differences 7 8 Traceback (most recent):9File"<pyshell#211>", Line 1,inch<module>Ten     'P'inchPython OneNameerror:name'Python'  is  notDefined

6. Iteration: Iterating through A For loop

1 #Parallel iterations2>>> names = ['Ann','Beth','Tita','Jane']3>>> ages = [' +',' +',' -',' A']4>>>Zip (names,ages)5[('Ann',' +'), ('Beth',' +'), ('Tita',' -'), ('Jane',' A')]6>>> forName,ageinchZip (names,ages):#zip can handle unequal sequences7     PrintName' is', Age,'years old'8 9Ann is21styears oldTenBeth is19years old OneTita is18years old AJane is22years old -  - #Index iterations the>>> strings ='python' ->>> forIndex,stringinchEnumerate (strings): -     if '3' inchStrings: -Strings[index] ='h' +  - #Flip and sort iterations: The reversed method returns an iterative object, and the sorted method returns a list +>>> Sorted ([2,0,7,5]) A[0, 2, 5, 7] at>>> Reversed ('Hello') -<reversed Object at 0x02ff8c70> ->>> List (Reversed ('Hello')) -['o','L','L','e','h']

7. Sequence length, maximum value, minimum value

1 >>> num = [120,14,39]2 >>> len (num)3 34 > >> max (num)5 6 >>> min (num)7 14

Basic knowledge review--general sequence operations

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.