With old Ziko Python's capacity is large list (1) _python

Source: Internet
Author: User

In the previous study, we already knew two types of Python data: int and str. Again, the understanding of the data type, the world is composed of data, the data may be a number (note, do not confuse, numbers and data are different), it may be text, or voice, video and so on. In Python (similar to other high-level languages), numbers like 2,3 are divided into one type, the word "hello" is divided into a type, the former is type int, the latter is the STR type (here is not to say the name of the translation, please reader familiar with the name of English, for future programming great benefits, What good is it? Who Uses who knows! )。

We also learned about variables, if a variable is wired to an int type of data (in the Jargon: assignment), then this variable is called a variable of type int, and sometimes it is not assigned, it is prepared to allow this variable to receive data of type int, we also need to declare it as a variable of type int. However, there is a benefit in Python that a variable does not have to be declared in advance and is named after it.

The list type in this story is also a data type of Python. Translated as: list. Below the black Word, please reader note:

List has a very powerful function in Python.

Defined

In Python, a list,[is represented by square brackets]

Inside the square brackets, it can be int, or str-type data, or even true/false this boolean value. Take a look at the examples below and pay special attention to reading comments.

>>> a=[]    #定义了一个变量a, it is the list type and is empty.
>>> Type (a)
<type ' list ' >  #用内置函数type () View the type of variable A, list
>>> bool (a)   # Use the built-in function bool () to see the Boolean value of variable A of the list type, because it is empty, so false
>>> print a   #打印list类型的变量a
[]

Do not always play empty, to some of the real bar.

>>> a=[' 2 ', 3, ' Qiwsir.github.io ']
>>> a
[' 2 ', 3, ' Qiwsir.github.io ']
>>> type (a)
<type ' list ' >
>>> bool (a)
True
>>> print a
[' 2 ', 3, ' Qiwsir.github.io ']

Using the above method, define a list type of variables and data.

The title of this lecture is "have capacity is big list", it points out a big feature of list: Can infinity, that is, the number of elements in the list can hold unlimited, of course, this is in the case of hardware ideal.

List Index

Still remember in the "Play to the string (3)", the string has been numbered, and then based on the number to get some or part of the characters, such a process, that is, index.

>>> url = "Qiwsir.github.io"
>>> url[2]
' W '
>>> url[:4]
' QIWS '
>>> Url[3:9]
' sir.gi '

In the list, there are similar operations. It's just an element, not an index in character units. Take a look at the example to understand.

>>> a
[' 2 ', 3, ' Qiwsir.github.io ']
>>> a[0]  #索引序号也是从0开始
' 2 '
>>> a[1]
3
>>> [2]
[2]
>>> A[:2]  #跟str中的类似, slice range is: Include start position, before end position
[' 2 ', 3]  #不包含结束位置
>>> a[1:]
[3, ' Qiwsir.github.io ']
>>> a[-1]  #负数编号从右边开始
' Qiwsir.github.io '
>>> a[-2]
3
>>> a[:]
[' 2 ', 3, ' Qiwsir.github.io ']

Action on List

Any industry has its own jargon, like the ancient bandits, the retreat is called "pull", even if it is a meaning, but the robbers are willing to use their own industry terminology, commonly known as "slang." This is true in all walks of life. The purpose of this is that I understand that there are two, one is some kind of secrecy; another is the line of people show the threshold of the industry, let others feel that the industry is very advanced, practitioners have a certain level.

Anyway, in Python and a lot of high-level languages, it's all about the mathematical perspective of the function, and in different situations have different names, such as methods, classes and so on. Of course, this kind of salutation is also to distinguish functions of different functions.

Earlier in the operation of STR, there are some built-in functions, such as S.strip (), which is the built-in function to remove the left and right spaces, but also the Str method. According to the consistent symmetry rule, there are also some methods of operation for list.

Append element

>>> a = ["good", "Python", "I"]   
>>> a
[' good ', ' python ', ' I ']
>>> a.append ("Like #向list中添加str类型 "Like    "
>>> a
[' good ', ' python ', ' I ', ' like ']
>>> a.append (100)      #向list中添加int类型100
>>> a
[' good ', ' python ', ' I ', ' like ', 100]

Official documentation This describes the List.append () method

List.append (x) Add an item to the "End of" the
list; equivalent to A[len (a):] = [x].

Is it possible to understand the meaning of list.append (x) in terms of the above description and the title "Append elements" in this section? Appends the new element x to the tail of the list.

You reader, if you look at the sentence in the official document above, you should note that there is also the latter part: equivalent to A[len (a):] = [x], meaning that list.append (x) is equivalent to: A[len (a):]=[x]. This is equivalent to telling us another method of appending elements, and the two methods are equivalent.

>>> a
[' good ', ' python ', ' I ', ' like ', M]
>>> A[len (a):]=[3]   #len (a), that is, to get the length of the list, This length refers to the number of elements in the list.
>>> a
[' good ', ' python ', ' I ', ' like ', 3]
>>> Len (a)
6
>>> a[6:]=[' Xxoo ']
>>> a
[' good ', ' python ', ' I ', ' like ', 100 , 3, ' Xxoo ']

By the Way Len (), this is used to get the type of data length such as LIST,STR. It was also mentioned when the string was explained.

>>> name = ' Yeashape '
>>> len (name)    #str的长度, is the number of characters
8
>>> a=[1,2, ' a ', ' B '] #list的长度, number of elements
>>> Len (a)
4
>>> b=[' yeashape ']  
>>> len (b)
1

The next one, continue list, there is capacity is big.

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.