Basic Python Learning (iii) four

Source: Internet
Author: User

Learn about List:

"" "More on Lists?
Using Lists as Stacks.
"""

Fruits = [' orange ', ' apple ', ' pear ', ' banana ', ' kiwi ', ' apple ', ' banana ']

#Return the number of times X appears in the list
Print (Fruits.count (' apple '))
Print (Fruits.count (' Tangerine '))

#Return zero-based index in the list of the first item whose value is X.
#list. Index (x[, start[, end]])
Print (Fruits.index (' Kiwi ', 4))

#Reverse The elements of the list in place.
Print ("Before reversing---------------------------------")
Print (fruits)
Fruits.reverse ()
Print ("After reversing---------------------------------")
Print (fruits)

#Add an item to the end of the list
Print ("Before appendding---------------------------------")
Print (fruits)
Fruits.append (' Grape ')
Print ("After appendding---------------------------------")
Print (fruits)

#sort (Key=none, Reverse=false)
Fruits.sort ()
Print ("After sorting---------------------------------")
Print (fruits)

#Remove the item at the given position in the list, and return it. If No index is specified, A.pop () removes and returns
# The last item in the list.
Fruits.pop ()
Print ("After poping---------------------------------")
Print (fruits)

If ' Apple ' in fruits:
Print ("in range")

Printing results:

D:\Python3.6.1\python.exe f:/python_workspace/tutorial/lists.py
2
0
4
Before reversing---------------------------------
[' Orange ', ' apple ', ' pear ', ' banana ', ' kiwi ', ' apple ', ' banana ']
After reversing---------------------------------
[' Banana ', ' apple ', ' kiwi ', ' banana ', ' pear ', ' apple ', ' orange ']
Before Appendding---------------------------------
[' Banana ', ' apple ', ' kiwi ', ' banana ', ' pear ', ' apple ', ' orange ']
After Appendding---------------------------------
[' Banana ', ' apple ', ' kiwi ', ' banana ', ' pear ', ' apple ', ' orange ', ' grape ']
After sorting---------------------------------
[' Apple ', ' apple ', ' banana ', ' banana ', ' grape ', ' kiwi ', ' orange ', ' pear ']
After Poping---------------------------------
[' Apple ', ' apple ', ' banana ', ' banana ', ' grape ', ' kiwi ', ' orange ']
In range

Basic Python Learning (iii) four

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.