Introduction to Python programming-from getting started to practicing-note 2_ list

Source: Internet
Author: User
Tags sorts

List Introduction Basics
    • Use square brackets [] to represent the list, separated by commas
    • Print (list name) prints the internal contents of the list, including the square brackets and the quotation marks of the strings
    • Access the elements in the list, tell the location/index of the element to list name [index]
    • Number of indexes starting from 0
    • Print (list name [index]) prints a single element, excluding square brackets and string quotes
    • Access the last element of a list
      • List name [-1] to access the last element of the list
      • List name [-2] the second-to-last element of the access list ... And so on
    • list name [] Create an empty list
    • When using escape characters such as '/n ', be sure to use them in quotation marks. Suppose the message is a variable
      • Print ('/nmessage ') will prompt for errors
      • Print ('/n ' +message) is correct, wraps first, then prints message variable contents

Modify, add, and delete elements in a list
  • modifying list elements
    • How to replace: List name [index]= ' new element '
    • Change is permanent, unrecoverable.
  • Add a new element to the list
    • Method Append () example: Names.append (' Zoe ') adds the element ' Zoe ' to the end of the list names
    • Method Insert () Example: Names.insert (0, ' Zoe ') inserts the element ' Zoe ' before the element with the index 0 in the list names, and then moves the subsequent element to the right one position
  • Remove an element from the list
    • Statement del
      • Example: Del names[0] Delete the element on index 0 in the list names
      • Note that Del is a statement, not a method
      • Del Delete element, you need to know the index of the element to delete first
    • Method Pop ()
      • Deletes the last element of the list and returns the delete value
      • Example: Poped_n=names.pop () deletes the last element of the list names and saves the number in Poped_n
    • Method Remove ()
      • Delete an element by value
      • You don't need to know where the element you want to delete is located
      • Example: Names.remove (' Zoe ') delete list names in Zoe

Organization List
  • Method Sort
    • Sort by element in alphabetical order : Names.sort () Sorts the elements in the list names alphabetically by the first letter
    • The sort is permanent .
    • Reverse sort, pass-through parameter Reverse=true example: Names.sort (reverse=true) sorts the elements in the list names by the first letter, in reverse order
  • function sorted
    • Example: print (sorted (names)) prints the elements in the list names in alphabetical order, sorted by the first letter. But do not change the original sort of names
    • Sort of is temporary
    • Reverse sort, passing parameters reverse=true Example: Print (sort (names,reverse=true)) prints the elements in the list names in the first letter, sorted alphabetically, in reverse order
  • Method Reverse ()
    • Example of an arrangement of inverted list elements: Names.reverse () Reverses the order of the elements in the list names, not the alphabetical order
    • permanent , want to restore the original order, and then reverse once again
  • function len ()
    • Calculates the list length, which is the number of elements in the list
    • The number of calculated list elements is starting from 1, and several are
    • Example: Len (names) calculates the number of elements in the list names, and returns 5 if there are 5 elements.
  • using (-1) The method of accessing the last element will cause an error only if the list is empty, otherwise this method is in any case a proper way.
  • An error message is returned when an element that does not exist in the access list, or an index that is accessed exceeds the list

Introduction to Python programming-from getting started to practicing-note 2_ list

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.