I want to learn more about Python and list (1 ).

Source: Internet
Author: User

I want to learn more about Python and list (1 ).

In the previous study, we have learned two types of python data: int and str. I would like to emphasize my understanding of the data types. The world is composed of data, and the data may be numbers (Note: Do not confuse them. There are differences between numbers and data ), it may also be text, sound, or video. In python (similar to other advanced languages), numbers like 2 and 3 are divided into one type, and texts like "hello" are divided into one type. The former is int type, the latter is 'str' type (the translation name is not mentioned here. Please check that the official website is familiar with the English name. It will be of great benefit to programming in the future. What are the advantages? Who uses it !).

We also learned about variables. If a variable is connected to an int-type data using a line (in the industry: Value assignment), we call this variable an int-type variable; sometimes the value has not been assigned yet. We want this variable to receive int-type data. We also need to declare it as an int-type variable. However, in python, there are the same benefits: variables do not need to be declared in advance and can be named as needed.

The list type is also a data type of python. Translation: list. The following black text is worth your attention:

LIST has very powerful functions in python.

Definition

In python, square brackets are used to represent a list, []

In square brackets, it can be int, str, or even True/False. Take a look at the example below and pay special attention to reading comments.

>>> A = [] # defines a variable a, which is of the list type and is empty. >>> Type (a) <type 'LIST'> # Use the built-in function type () to view the type of variable a, which is list >>> bool (a) # Use the built-in function bool () check the Boolean value of list type variable a. Because it is null, It is FalseFalse >>> print a # print list type variable a []

It cannot be empty all the time. Come and try it.

>>> 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']

Use the above method to define a list type variable and data.

The title of this lecture is "a large list", which indicates a major feature of the list: It can be infinitely large, that is, the number of elements contained in the list is infinite, of course, this is ideal for hardware devices.

List Index

Still remember that in play string (3), a character string was numbered and a part or part of the characters were obtained based on the number, it is an index ).

>>> url = "qiwsir.github.io">>> url[2]'w'>>> url[:4]'qiws'>>> url[3:9]'sir.gi'

Similar operations are also available in the list. It is only an element, not a character index. Let's see the example.

>>> A ['2', 3, 'qiwsir. github. io ']> a [0] # the index number is also '2' starting from 0 >>> a [1] 3 >>> [2] [2] >>> a [: 2] # similar to str, the slice range is: contains the start position, before the end position ['2 ', 3] # does not include the end position> a [1:] [3, 'qiwsir. github. io ']> a [-1] # The negative number starts from the right side 'qiwsir. github. io '> a [-2] 3> a [:] ['2', 3, 'qiwsir. github. io ']

List operations

Every industry has its own line of speech. Just like the ancient robbers, they call retreat "even if it is a meaning, but the robbers are willing to use their own industry terms, it is commonly known as "Black talk ". This is true for all walks of life. I understand two purposes of doing so: one is a certain degree of confidentiality; the other is that people outside the industry show the threshold of this industry, making others feel that this industry is very advanced and practitioners have a certain level.

In any case, in python and many advanced languages, they give functions in terms of mathematics, and have different names in different situations, such as methods and classes. Of course, this kind of name is actually used to distinguish different functions of a function.

Some built-in functions, such as s. strip (), are used in str operations. This is a built-in function for removing spaces between left and right. It is also the str method. According to the consistent symmetric rule, there are some operation methods for the list.

Append Element

>>> A = ["good", "python", "I"] >>> a ['good', 'python', 'I'] >>>. append ("like") # Add the str type "like"> a ['good', 'python', 'I' to the list ', 'like']>. append (100) # Add int type 100 to list> a ['good', 'python', 'I', 'like', 100]

This document describes the list. append () method.

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

Can list. append (x) be understood from the above description and the title "append element" in this section? Append the new element x to the end of the list.

If you have read the sentence in the above official document, you should note that there is a second sentence: equivalent to a [len (a):] = [x], list. append (x) is equivalent to a [len (a):] = [x]. This also tells us another method for appending 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, which refers to the number of elements in the list. >>> A ['good', 'python', 'I', 'like', 100, 3] >>> len (a) 6 >>> a [6:] = ['xxoo '] >>> a ['good', 'python',' I ', 'like', 100, 3, 'xxoo']

By the way, len () is used to obtain the length of data types such as list and str. This is also mentioned in the string explanation.

>>> Name = 'yeashape '>>> len (name) # str length, which is the number of characters 8 >>> a = [1, 2, 'A ', 'B'] # The length of list, which is the number of elements >>> len (a) 4 >>> B = ['yeashape '] >>> len (B) 1

In the next lecture, we will continue the list, which is more tolerant.




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.