Python-week2, week 2 (based on Python3.0 or above ),

Source: Internet
Author: User

Python-week2, week 2 (based on Python3.0 or above ),
1, list

We can use variables to store data, but when there are a lot of data, there will be many limitations when using variables, so the list is used at this time. The list is separated by commas (,) for each element in brackets. For example, [1, 2, 3] is a list. You can assign this list to a variable name. Next let's take a look at how to query, add, modify, and delete a list.

Query the list:

If you want to print a parameter in the list, you can use the subscript (location) parameter of the List to call it out.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 print (name [1])

In this way, we printed the word "money". Note that the position in the program starts from 0.

If you want to print multiple elements, you can use the slice method:

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 3 print (name [0: 3])

Note that the slice does not contain the position at the end. If the position above is not 3, "Li" is not printed ". Slice you still have many cutting methods, such as cutting from the center, cutting from the back, continuously cutting or jumping. Here we will not give an example one by one, because I am a little lazy 0.0 ///. Note that we write the position parameter from left to right, that is, the second number is greater than the first number. [-1:-3] is incorrect, it should be [-3:-1].

What else do we know about an element and want to check its position in the list? We can use the index method.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 3 print (name. index ("Li "))

How many times has it appeared in the list? Use the count method (replace the index with count ).

List addition:

If we want to add an element to the list, we use the append method. Append is append, that is, append at the end.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 3 name. append ("yellow") 4 print (name)

If the value is added at a specified position, the insert method is used. For example, add in position 3.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 3 name. insert (3, "yellow") 4 print (name)

Or we can convert it into another list and use extend to merge it. This is called an extension.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 name2 = ["yellow"] 3 name. extend (name2) 4 print (name)
List modification:

Next is the modification of the list, which is easy to understand.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou ", "Wu"] 2 name [3] = "yellow" 3 print (name)

 

Delete A list:

You can use the del, remove, and pop methods to delete a file.

1 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 2 del name [3] 3 print (name) 4 5 name = ["Zhao", "Qian", "Sun", "Li", "Zhou", "Wu"] 6 name. remove ("Li") 7 print (name) 8 9 name = ["Zhao", "Qian", "Sun", "Li", "Zhou ", "Wu"] 10 name. pop () # If no parameter exists, the last 11 print (name) is deleted by default)
Other methods of the list:

There are still some syntaxes In the list. Let's introduce their functions one by one. Reverse (reverse), sort (sort ),.... Knock on the blackboard .. Focus on copy ).

1 name = ["a Zhao", "f Qian", "e sun", "d li", "c weeks", "B Wu"] 2 name. sort () 3 print (name) 4 name. reverse () 5 print (name)

------------------------------------------------------------------------------------------ 11-05----23: 26: 46 --- here, copy is followed ---------------------------------------------------------------------

 

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.