Basic Python Tutorial (2nd edition) Chapter II after reading summary;

Source: Internet
Author: User

Python chapter II

A sequence is a data structure: a typical sequence consists of: list, string, tuple

A data structure is a collection of elements that are organized in some way (for example, numbering elements) together, and these data elements

can be numbers or characters, or even other data structures, in Python, the most recent data structure is a sequence, and each element in the sequence is

Assign an ordinal-----that is, the position of the element, the index, the first index is 0, the second is 1, and so on.

The main differences between lists and tuples:

Lists can be modified, tuples are not, but they are collectively referred to as sequences; the elements of the list are separated by commas, written in square brackets, and each element of the tuple is separated by commas and written in parentheses; tuples can be used as keys in a map, but lists are not; tuples exist as return values for many of the built-in functions and methods. That is, you have to deal with tuples, as long as you do not attempt to modify tuples, the handling of tuples in most cases is to treat them as a list, in general, the list is more able to meet all the requirements of the sequence;

A sequence can also contain other sequences;

In [ten]: Lambert = [' Age ', 20]in [all]: Lizheng = [Lambert, ' Hello ']in [[]: Print lizheng[[' age ', +], ' hello ']

Python also has a data structure called a container.

A container is basically any object that contains other objects, and sequences (such as lists and tuples) and mappings (such as dictionaries) are the two main types of containers. Each element in the sequence has its own number, and each element in the map has a generic sequence operation with a name (also known as a key): All sequence types can perform certain operations, such as indexing, slicing, adding, multiplying, and checking whether an element belongs to a member of a sequence, in addition to Python also calculates the length of the sequence to find the largest and smallest elements of the built-in function;


In Python, the membership check is in: This operator checks if the condition is true, and then returns the corresponding value.

True if the condition is true, the condition is false to return false.

For example:

in [+]: aa = "Lambert" in []: ' Lam ' in aaout[20]: True


This chapter is designed by several functions and methods, the following will be described around this picture;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/58/BD/wKioL1S7LQ_hBJqpAAGLp7_8GU4246.jpg "title=" 2) eta~ 4$2_5v}7}ez3p5t4w.png "alt=" wkiol1s7lq_hbjqpaaglp7_8gu4246.jpg "style=" float:left; "/>

1. Built-in functions Len,min and Max are very useful;

The Len function returns the number of elements contained in the sequence

Min function returns the smallest element in a sequence

The Max function returns the largest element in a sequence

In [1]: numbers = [1,168,888]in [2]: Len (Numbers) out[2]: 3In [3]: Max (numbers) out[3]: 888In [4]: Min (numbers) out[4]: 1

The list function, which can split strings into lists, is sometimes useful for all types of sequences;

In [5]: aa = ' Lambert ' in [6]: List (AA) out[6]: [' l ', ' a ', ' m ', ' B ', ' e ', ' r ', ' t ']


The 2.append method is used to append a new object to the end of the list, just to modify the original list at the appropriate location, which means that it does not simply return a modified new list----But instead modifies the original list directly, which is not what we want in general.

In [7]: LST = [1,2,4]in [8]: Lst.append (6) in [9]: Print Lst[1, 2, 4, 6]


3.count method counts the number of occurrences of an element in a list

In []: times = [1,2,3,4,5,6,2]in [all]: Times.count (2) out[11]: 2In [ten]: Times.count (4) out[12]: 1


The 4.extend method can append multiple values from another sequence at the end of the list, in other words, the existing list can be expanded with the new list;

in [+]: a = [1,2,3]in]: b = [4,5,6]in]: A.extend (b) in [+]: Print a[1, 2, 3, 4, 5, 6]

The above operation looks like a join operation, the most important difference is that the Extend method modifies the extended sequence

, and the original sequence operation (that is, the join operation) is not, it returns a completely new list;


The 5.index method is used to find the index position of the first occurrence of a value from a list;

in [+]: Lambert = [' Light ', ' orange ', ' via ', ' enough ']in []: Lambert.index (' orange ') out[23]: 1In []: lambert[1]out[24 ]: ' Orange '

The Insert method is used to insert an object into the list:

in [+]: numbers = [1,2,3,4,5,6]in]: Numbers.insert (3, ' three ') in []: Print Numbers[1, 2, 3, ' Three ', 4, 5, 6]

Of course, as with the Extend method, it is possible to implement shard assignments, for example:

in [+]: numbers = [' One ', ' one ', ' three ', ' four ', ' five ']in []: numbers[3:4] = [' Hello ']//1, with the next comparison replace four itself, five before; in [n]: numbersout[43]: [' One ', ' one ', ' ' Three ', ' Hello ', ' five ']//You may be thinking that I would like to replace four and five, why is it not in effect? in [[]: Numbers[3:5] = [' Hello ']//2, compared to above, because if the Shard is printed, after the index 5, after 3 (including 3) in [the]: [' One ', ' " ', ' three ', ' Hello ']


The 6.pop method removes an element from the list, which defaults to the last one, and returns the value of the modified element;

The Pop method is the only list method that can both modify the list and return the element value (except none);

Using the Pop method can achieve a common data structure----stack, the principle of the stack is like stacked on the plate, only on the top of the plate, the same, can only take the plate from the top, and finally put into the stack of the first overflow, this principle becomes LIFO, that is, LIFO;

As an example:

in [+]: x = [1,2,4]in]: X.pop () out[47]: 4In []: xout[48]: [1, 2]//4 was kicked out in []: X.append (x.po P ()) in []: xout[50]: [1, 2]//2 is still in, I kicked out and then use append to add in;


The 7.remover method is used to remove the first occurrence of a value in the list;

In [Wuyi]: x = [' Hello ', ' Lambert ']in [): X.remove (' Lambert ') in [+]: xout[53]: [' Hello ']

The reverse method stores the elements in the list in reverse, as you can see from the word literal once;

In [si]: x = [1,2,3]in]: X.reverse () in [the]: xout[56]: [3, 2, 1]


The 8.sort method is used to sort the list at the original location, which means that the original list is changed so that the elements can be arranged in a certain order, rather than simply returning a sorted copy of the list;

in [+]: AA = [2,45,1,34,2342,332]in [max]: Aa.sort () in [+]: Print aa[1, 2, 34, 45, 332, 2342]


Quick way to copy a list: The first method, is the slice assignment, the second through the sorted function;

in []: x = [1,454,2,34,3]in]: y = x[:] or y = sorted (x) give Yin the value after x slice [max]: Y.sort () in [Max]: xout[73]: [1, 454, 2, 3]in, yout[74]: [1, 2, 3, 34, 454]///See, is so capricious;


The function of the 9.tuple function is basically the same as the list function, which takes a sequence as an argument and converts it to a tuple;

In [Bayi]: Tuple ([out[81]): (1, 2, 3)

This article is from the "Leejung" blog, make sure to keep this source http://leejung.blog.51cto.com/8917264/1605258

Basic Python Tutorial (2nd edition) Chapter II after reading summary;

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.