Introduction to Python lists, the most commonly used Python data types

 

The text and pictures of the text come from the Internet and are only for learning and communication. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for processing.

Author: miscellaneous data on

PS: If you need Python learning materials, you can click the link below to obtain http://t.cn/A6Zvjdun

1 Introduction

  Sequences are the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.

Python has 6 built-in types for sequences, but the most common are lists and tuples. The operations that can be performed on a sequence include indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of sequences and determining the largest and smallest elements.

   A list is the most commonly used Python data type, and it can appear as a comma-separated value in square brackets. The data items of the list need not have the same class.

2. Style

3. Use

 Extract the elements from the list through a_list.

4. Modify, add, delete elements

 

    The syntax for modifying list elements is similar to the syntax for accessing list elements. To modify a list element, you can specify the list name and the index of the element to be modified, and then specify the new value of the element

Add list elements using append keyword and + way

Delete To distinguish whether to delete one or more elements

Typical in deletion:

     Pops the element anywhere in the list

     Sometimes, you want to remove an element from the list and then use its value. For example, in a web application, you may want to remove users from the active member list and add them to the inactive member list.

     Use pop () to delete the element. By default, the last digit in the list is deleted. When the parameter (x) exists, the value at position x-1 in the list is deleted.

 Remove elements based on value.remove ()

List sorting 

      Use sort () to sort permanently

           You can also arrange the list elements in the reverse order of the alphabet. To do this, simply pass the parameter reverse = True [cars.sort (reverse = True)] to the sort () method

Use the function sorted () to temporarily sort the list

 

Print the list backwards

To reverse the order of list elements, use the method reverse ().

Determine the length of the list

Common errors: list index out of range

The latest Python tutorial in 2020:

If you want to learn Python or are learning Python, there are a lot of Python tutorials, but is it the latest?

Maybe you have learned something that people might have learned two years ago, and here I share a wave of the latest Python tutorials for 2020.

 

 

 

 

The above series of editors are ready for everyone to pack, I hope to help you who are learning!

How to get it, you can get it for free by editing the "Information" of the private letter!

Guess you like

Origin www.cnblogs.com/python0921/p/12691481.html