Python List Data Type Classification

Source: Internet
Author: User
Tags define local python list

The Python list is a commonly used computer language. It is widely used. The following article introduces it in detail, I hope you will have a better understanding of your Python list by reading this article. Here we will introduce the relevant content and hope to help you.

List is the data structure that processes a group of ordered projects. You can store a series project in a list. Suppose you have a shopping list that records the items you want to buy, so you can easily understand the list. However, in your shopping table, each item may have a single row, while in Python, you use commas to separate each item.

The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete a project, we say that the list is a variable data type, that is, this type can be changed.

  • Python programming language user experience
  • Differences between the Python programming language and other languages
  • Effect of using the Python programming language in Java
  • Python command line code Enhanced memory
  • Convert Python print encoding to default encoding

Quick Start to objects and Classes

Although I have been postponing the discussion of objects and classes, a little explanation of them can help you better understand the list. We will explore this topic in detail in the corresponding chapter.

List is an example of using objects and classes. When you use variable I and assign values to it, for example, if you assign an integer 5, you can think that you have created a class type) int object instance) I. In fact, you can take a look at help (int) to better understand this.

Classes also have methods, that is, they define local functions for classes only. You can use these functions only when you have an object of this class. For example, Python provides the append method for the list class. This method allows you to add a project at the end of the list. For example

 
 
  1. mylist.append('an item') 

Add the string to the mylist. Note: Use the dot to use the object method.

A class also has a domain, which is a variable defined only for the class. Only when you have an object of this class can you use these variables/names. Class, for example

 
 
  1. mylist.field。 

Quick Start to objects and Classes

 
 
  1. #!/usr/bin/python  
  2. # Filename: using_list.py  
  3.  
  4. # This is my shopping list  
  5. shoplist = ['apple', 'mango', 'carrot', 'banana']  
  6.  
  7. print 'I have', len(shoplist),'items to purchase.'  
  8.  
  9. print 'These items are:', # Notice the comma at end of the line  
  10. for item in shoplist:  
  11. print item,  
  12.  
  13. print '/nI also have to buy rice.'  
  14. shoplist.append('rice')  
  15. print 'My shopping list is now', shoplist  
  16.  
  17. print 'I will sort my list now'  
  18. shoplist.sort()  
  19. print 'Sorted shopping list is', shoplist  
  20.  
  21. print 'The first item I will buy is', shoplist[0]  
  22. olditem = shoplist[0]  
  23. del shoplist[0]  
  24. print 'I bought the', olditem  
  25. print 'My shopping list is now', shoplist  
  26.  

Output

 
 
  1. $ python using_list.py  
  2. I have 4 items to purchase.  
  3. These items are: apple mango carrot banana  
  4. I also have to buy rice.  
  5. My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']  
  6. I will sort my list now  
  7. Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']  
  8. The first item I will buy is apple  
  9. I bought the apple  
  10. My shopping list is now ['banana', 'carrot', 'mango', 'rice']  
  11.  

How it works

The shoplist variable is a shopping list of someone. In shoplist, we only store the name string of the purchased item, but remember that you can add any types of objects including numbers or other lists to the list. We also use the for... in loop to recursively iterate between projects in the list. From now on, you must have realized that the list is also a sequence. The sequence features will be discussed in later sections. Note that a comma is used at the end of the print statement to remove the line breaks automatically printed by each print statement. This is a bit ugly, but it is simple and effective.

Next, we use the append method to add a project to the list, as we have discussed earlier. Then, we can print the list to check whether the project has been added to the list. To print a list, simply pass the list to the print statement, and we can get a clean output.

Next, we will use the sort method of the List to sort the list. It should be understood that this method affects the List itself, rather than returning a modified list-this is different from the method used to work with strings. This is what we call the list variable and the string variable.

Finally, we want to delete an item from the list when purchasing it in the market. We use the del statement to complete this task. Here, we point out which project in the list we want to delete, and the del statement is for us to delete it from the list. We specify that we want to delete the first element in the list, so we use del shoplist [0] to remember that Python starts counting from 0 ).

If you want to know all the methods defined by the list object, you can use

 
 
  1. help(list) 

Obtain the complete Python list. I hope the above content will be helpful to you.

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.