Basic Python tutorial for. Net programmers-list and meta-group [First Day], basic python tutorial

Source: Internet
Author: User

Basic Python tutorial for. Net programmers-list and meta-group [First Day], basic python tutorial

I. general sequence operations:

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

1.1 index:

The subscript of all elements in the sequence increases from 0. If the index length is N, the range is-N ~ Between N-1, beyond this range will promptIndexError: index out of range

>>> greeting ='Hello world'>>> print greetingHello world>>> print greeting[0],greeting[-1]H d

1.2 parts:

A. Simple sharding:
Slice is based on the index and can be understood as retrieving data from a certain region. It is similar to substring in C # And the skip () and take () methods provided by C # Linq.

Note: [A: B] A and B correspond to the index subscript respectively. The value range is [A, B) (anyone who has studied mathematics knows that B is not included ). note that B must be greater than A; otherwise, the output is null. Why is there data in web [9:-1, in fact, the subscript data of-1 is equivalent to the subscript of m 12, 12> 9, so there is data. then, for web [9:-5], it is a tragedy.

>>> Web = 'www .baidu.com '>>> print web [4: 9] #4 for bbaidu >>>> print web [9:-5] #
>>> Print web [9:-1] # m. co corresponding to the second '.'-1 corresponding to 9

B. partitions in the form of an equal difference series:

By default, it is obtained based on the subscript interval of 1. In fact, the third parameter can be used for further division. [A: B: Number of intervals]. If there are no two 10 numbers, take one.

  

>>> 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 addition:

You can add the same Type. If the Type is different, Type Error is returned.

>>> [1,2,3] + [4,5][1, 2, 3, 4, 5]>>> [1,2,3] +['hello'][1, 2, 3, 'hello']>>> [1,2,3]+'hello'Traceback (most recent call last):  File "<pyshell#41>", line 1, in <module>    [1,2,3]+'hello'TypeError: can only concatenate list (not "str") to list>>> 

1.4 multiplication:
I think Python's multiplication is very interesting. It can be understood as one of multiple copies of existing data.

    

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

1.5 useful methods.

You can use in to Check whether an element exists. max, min, len, and count are used to obtain the maximum value, minimum value, length, and quantity.

    

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

Ii. List.
  

The list can be understood as a linked list in C. or List <Object> in C #. The List can implement the general serial number operation shown above. You can add, delete, modify, query, and sort the List.

2.1 Basic list

>>> Print list ('20140901') # You can convert a string to a list ['1', '2 ', '3'] >>> list1 = [123, 3] # declare the list through [] >>> list2 = list (123) # is an integer and no index exists, therefore, an error is reported during conversion. Traceback (most recent call last): File "<pyshell #53>", line 1, in <module> list2 = list (123) TypeError: 'int' object is not iterable

2.2 basic list operations
A add, delete, and modify

>>> Numbers = [1, 3, 4, 5] >>> numbers [0] = 5; # modify >>> print numbers [5, 2, 3, 4, 5] >>> numbers. remove (5) # delete a value of 5 >>> print numbers [2, 3, 4, 5] >>> del numbers [0] # Delete the first element >>> print numbers [3, 4, 5] >>> numbers. append (7) # Add an element at the end> print numbers [3, 4, 5, 7]

B. Assignment of parts [magic part operations]

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]

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

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

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

Note: All operations can be considered as a replacement.

2.3 list method.

>>> Number = [1, 2, 3, 4, 5] >>> Number. extend ([6, 7, 8]) # Add a list, which must be a list> Number [1, 2, 3, 4, 5, 6, 7, 8] >>> Names = ['Frank ', 'lock', 'Vincent', 'tingna'] >>> Names. index ('lock') # use Index to obtain the subscript 1 >>> Names. insert (2, 'hard') >>> Names ['frank', 'lock', 'hard', 'Vincent ', 'tingna'] >>> Names. pop () # Delete 'tingna' at most> Names ['frank', 'lock', 'hard', 'Vincent ']> Names. pop (0) # Kick out the first element 'frank'> Names ['lock', 'hard', 'Vincent ']> Names. reverse () # reverse Order >>> Names ['Vincent ', 'hard', 'lock'] >>> Names. sort () # arrive from an early age >>> Names ['hard', 'lock', 'Vincent '] >>> Names. sort (key = len) # sort by length >>> Names ['lock', 'hard', 'Vincent '] >>> Names. sort (reverse = True) # from large to small> Names ['Vincent ', 'lock', 'hard']

3. Immutable sequence of tuples

The difference between the declaration by using the () number and the list is that it is immutable once declared.
    

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

Iv. Summary
    

This chapter summarizes the list and metadata in the basic Python tutorial. net. for example, the wonderful slicing operation is actually simplified. the addrang and revomeall methods in. net. indeed, as mentioned in the book, Python can use 100 lines of code to implement what needs to be done in C Language 1000 lines of code. all right, wash, and sleep. I hope the company will win the Iphone gold prize at the annual meeting tomorrow, hahaha.
[Complete the basic Python tutorial after one month's work and study the blog series. Come on!]

 

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.