. NET Programmer's Python basic tutorial learn----lists and tuples [first day]

Source: Internet
Author: User

A. General sequence operation:

In fact, for lists, tuples belong to serialized data, which can be accessed through the following table. Let's take a look at the basic operation of the sequence below.

1.1 Index:

The subscript of all elements in the sequence is incremented from 0. If the length of the index is N, then the range is between-n~n-1, and exceeding this range will prompt indexerror:index out of range

>>> greeting ='HelloWorld'print  greetinghelloWorldPrint greeting[0],greeting[-1]h D

1.2 Shards:

A. Simple shards:
Sharding is based on an index and can be understood as taking a certain area of data, similar to substring in C #, and the Skip () and take () methods provided by C#LINQ.

NOTE:[A:B] A, A and b respectively corresponding to the index subscript, the value area is [A, b] (learned that the math is not included in the C). Note that the opinion is that B must be greater than a, otherwise the output is empty, then the problem is why Web[9:-1] have data, please think carefully, actually-1 of the subscript data equivalent to M subscript 12,12>9 so there is data. Then for WEB[9:-5] it's tragic.

>>> Web ='www.baidu.com'print web[4:9] #4对于的bbaiduPrint web[9:-5
print web[9:-1] #9对应的第二个 '. ' -1 corresponding m.co

B. Shards in arithmetic progression form:

By default, when the index interval is 1, it can be further segmented by a third parameter. [A:B: Interval number], 10 digits not two take 1 times.

  

>>> number =[1,2,3,4,5,6,7,8,8,10]print number[0:10:2[1, 3, 5, 7, 8]
>>> Print Number[::-2]
[10, 8, 6, 4, 2]
>>> Print Number[::2]
[1, 3, 5, 7, 8]

1.3 Sequence Additions:

The same type of can want to add, different types of errors will be error type error

>>> [4,5] + [][1, 2, 3, 4, 5]>>> [+[]'Hello'][1, 2, 3,'Hello']>>> [1,2,3]+'Hello'Traceback (most recent): File"<pyshell#41>", Line 1,inch<module>    [1,2,3]+'Hello'Typeerror:can only concatenate list ( not "Str") to list>>>

1.4 Multiplication:
I think the multiplication of Python is interesting and can be understood as a multiple copy of the existing data.

    

>>> [1]*10[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]'*10'
    '

1.5 useful methods.

Through in can check whether the element exists, Max,min,len,count get the maximum value, the minimum value, the length, including the quantity

    

>>> number =[1,2,2,3,3,4,5,7]print'max number:' + str (max (number)) Max Number:The quantity ofprint'2 is:' + str (number.count (2)) 2 is:  The length of the print ' number list is:' + str ' (len number) The length of the number list is:8

Two. List.
  

The list can be understood as a linked table in C. or C # List<object>, the list can be implemented above the common serial number operation, can be added to the list, delete, change, check, sorting and other methods

2.1 Basics of Lists

>>>PrintList'123')#" " can convert a string to a list['1','2','3']>>> list1=[1,2,3]#through the [] Declaration list>>> list2 = list (123)#123 is plastic, there is no index, so when the conversion error. Traceback (most recent): File"<pyshell#53>", Line 1,inch<module>List2= List (123) TypeError:'int'Object is  notIterable

2.2 Basic operations of the list
A increase, delete, change

>>> numbers =[1,2,3,4,5]>>> numbers[0]=5;#Modify>>>Printnumbers[5, 2, 3, 4, 5]>>> Numbers.remove (5)#Remove the value of 5>>>Printnumbers[2, 3, 4, 5]>>>delNumbers[0]#Delete First element>>>Printnumbers[3, 4, 5]>>> Numbers.append (7)#add an element to the tail>>>Printnumbers[3, 4, 5, 7]

B Shard Assignment "magical shard operation"

Overall replacement
    

>>> greeting= [1,2,3,4,5]
>>> greeting[1:] = [0,0]
>>> Greeting
[1, 0, 0]

Replacement interval

>>> greeting= [1,2,3,4,5]>>> greeting[1:2]=[0,0,0]>>> greeting[ 1, 0, 0, 0, 3, 4, 5]

Batch add, replace with [0,0,0] []

>>> greeting=[1,2,3,4,5]>>> greeting[1:1]=[0,0,0]>>> greeting[ 1, 0, 0, 0, 2, 3, 4, 5]

Bulk Delete, replace [2,3,4] with []

>>> greeting=[1,2,3,4,5]>>> greeting[1:4]=[]>>> greeting[1, 5]

Note: For various operations can be understood as a replacement.

2.3 List of methods.

>>> number =[1,2,3,4,5]>>> number.extend ([6,7,8])#Add a list that must be a list>>>number[1, 2, 3, 4, 5, 6, 7, 8]>>> Names =['Frank','Lock','Vincent','Tingna']>>> Names.index ('Lock')#remove the label by index1>>> Names.insert (2,'Hardy')>>>names['Frank','Lock','Hardy','Vincent','Tingna']>>> Names.pop ()#most one element is deleted'Tingna'>>>names['Frank','Lock','Hardy','Vincent']>>> Names.pop (0)#kick out the first element'Frank'>>>names['Lock','Hardy','Vincent']>>> Names.reverse ()#reverse Order>>>names['Vincent','Hardy','Lock']>>> Names.sort ()#从小到达>>>names['Hardy','Lock','Vincent']>>> Names.sort (Key=len)#Sort by length>>>names['Lock','Hardy','Vincent']>>> Names.sort (reverse=true)#从大到小>>>names['Vincent','Lock','Hardy']

Three. Tuples immutable sequences

With the () sign, the difference from list is that it is immutable once declared.
    

 >>> tuple1 = (1,2,3)  >>> 1, 2, 3)  >>> tuple (list ( '  ,  '  e   ' ,  '  l   ' ,  '  l   ' ,  '  o   '   '  >>> 

Four. Summary
    

     This chapter summarizes the list and tuples in the basic Python tutorial, but the overall difference between. NET is not too big. For example, a wonderful slicing operation is actually simplifying the addrang,revomeall of. Net. Indeed, as the book says, Python can use 100 lines of code to do what the C-language 1000 line of code needs to do.             All right, wash and sleep. For tomorrow the company annual meeting can draw the iphone local Tyrants Gold Award, hahaha.
[Take advantage of one months of working out of the Python basic tutorial, complete the Basic Tutorial Learning Blog series. Come on!]

. NET Programmer's Python basic tutorial learn----lists and tuples [first day]

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.