With old Ziko Python's big list (1)

Source: Internet
Author: User
In the previous study, we already know the two types of Python data: int and str. Again, the understanding of data types, the world is made up of data, data may be numbers (note, don't confuse, numbers and data are different), may be text, or sound, video and so on. In Python (other high-level languages are similar) divide a number such as 2,3 into a type, a type such as "Hello", the former is an int type, the latter is a str type (here does not say the name of the translation, please crossing familiar with the name of English, for future programming a great advantage, What good is it? Who knows with whom! )。

The variables are also learned before, if a variable is connected with a line of data of type int (the jargon is: assignment), then this variable is called a variable of type int, and sometimes it is not assigned, it is ready for this variable to receive the int type of data, we also need to declare it as a variable of type int. However, there is one advantage in Python, where variables do not have to be declared in advance and are named with them.

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

The list has very powerful features in Python.

Defined

In Python, square brackets represent a list,[]

Inside the square brackets, it can be an int, or it can be a str-type data, or even a Boolean value such as True/false. Take a look at the following example, paying special attention to reading comments.

>>> a=[]    #定义了一个变量a, it is a list type and is empty. >>> type (a)
 
  
   
    #用内置函数type () View the type of variable A, list>>> bool (a)   #用内置函数bool () Look at the Boolean value of the variable A of list type, because it is empty, so for falsefalse>>> print a   #打印list类型的变量a []
 
  

Do not always play empty, to some real bar.

>>> a=[' 2 ', 3, ' Qiwsir.github.io ']>>> a[' 2 ', 3, ' Qiwsir.github.io ']>>> type (a)
 
  
   
  >>> BOOL (a) true>>> print a[' 2 ', 3, ' Qiwsir.github.io '
 
  

Define a list-type variable and data using the above method.

The title of this lecture is "A large list", indicating a list of a major feature: can be infinitely large, that is, the list can accommodate the number of elements unlimited, of course, this is in the ideal hardware equipment.

List Index

Remember in the play string (3), the string was once numbered, and then according to the number to get a certain or some part of the character, the process 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 a unit of elements, not indexed in characters. Look at the example and you understand.

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

Operations on the list

Any industry has its own jargon, like ancient robbers, who call withdrawal "nonsense", even though it is a meaning, but robbers are willing to use their own industry terminology, commonly known as "slang." This is true in all walks of life. The purpose of doing this I understand that there are two, one is some kind of secrecy; the other is the outside people show the threshold of the industry, so that others feel that the industry is very advanced, practitioners have a certain level.

Anyway, in Python and a lot of high-level languages, the original mathematical perspective is the function of things, and in different situations have different names, such as methods, classes and so on. Of course, this kind of salutation is actually to distinguish function of different function.

In front of the operation of STR, there are some built-in functions, such as S.strip (), which is a built-in function that removes left and right spaces, and is the method of Str. According to consistent's symmetry law, there are some ways to manipulate list.

Append element

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

Official documentation describes the List.append () method in this way

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) from the above description and the title "Append element" in this section? Appends the new element x to the tail of the list.

Yours faithfully crossing, if you look at the official document in the above statement, you should note that there is also the following half sentence: equivalent to A[len (a):] = [x], meaning that list.append (x) is equivalent to: A[len (a):]=[x]. This is also equivalent to telling us another way to append elements, and the two methods are equivalent.

>>> a[' good ', ' python ', ' I ', ' like ', 100]>>> A[len (a):]=[3]   #len (a), that is, 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 ', 3, ' Xxoo ']

By the way, Len (), this is used to get the data length of list,str and other types. This is also mentioned when the string is explained.

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

The next talk continues the list, has the capacity is big.

  • Related Article

    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.