Basic usage of the List

Source: Internet
Author: User

#-*-Coding: cp936 -*-
'''
1: Create an empty list
2. Add data elements directly in the list, which can be numbers, strings, and characters.
'''
Mylist = [] #1
Print mylist
Mylist = [1, 'A', 'string']
Print mylist
Mylist = mylist + [2, 'B', 'OK']
Print mylist
Mylist. append ('good ')
Print mylist
Mylist. append (10000)
Print mylist
Mylist. append ('C ')
Print mylist
Mylist. Extend (['x', 1, 'zzz'])
Print mylist
Mylist. insert (0, 'First ')
Print mylist

"""
Retrieve the value count in the list to calculate the number of times an element has been specified in the list. Index returns the subscript of the query element.
"""
Print mylist. Count ('first ')
Print mylist. Index ('first ')

"""
Delete the element del in the list to delete the elements in the list. The remove parameter of the List Index is the list element.
Pop deletes the value of an element from the end.
"""
Del mylist [1]
Print mylist
Mylist. Remove ('good ')
Print mylist
Mylist. Pop ()
Print mylist

"""
Delete the entire list. If no exception is thrown, a list with the same name is created.
"""
Del mylist
Mylist = []
Print mylist
Print

 

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.