Python Learning sharing-list meta-groups

Source: Internet
Author: User

1 lists (list)

The following code assigns a variable to a list

a=['laay','aay','ay','  y']

There are indexes in the list that can be indexed to access the values in the list, or you can complete the slices by indexing

Print (a[0]) Print (a[1]) Print (a[-1]) Print (A[:3]) Print (A[:-1])

The printing results were

laayaayy['laay'aay'ay'  ['laay'aay ' ay ']

Add elements, delete elements, change elements, find elements

A.append ('abc')# add ' abc ' to the last del# of the list Delete element of current index 1 in list a[1]='dd'# Change the list of elements indexed to 1 to ' DD 'print (A[0])# Prints an element with a list index of 0

List of loops

 for inch A:     Print (i)

Other functions of the list

CMP (List1, List2)# compares the elements of the two list to the number of Len (list)# elements and, if the list is empty, can be judged as Faslemax (list)#  Returns the list element maximum min (list)# returns the list element minimum value list (seq)# convert tuples to lists

Other ways to List

List.count (obj)# count the number of occurrences of an element in a list list.extend (seq)#  Append multiple values from another sequence at the end of the list (with a new list)List.index (obj)# Find the index position of the first occurrence of a value from the list List.insert (index, obj)# Inserts an object into the list List.pop (obj=list[-1])# removes an element from the list (the last element by default) and returns the value of the element List.remove (obj)# Removes the first occurrence of a value in the list list.reverse ()# Reverses the element in the list list.sort ([ Func])# sort the original list

Lists can be nested in lists, and lists in lists use logic consistent with general lists

a= [['laay','laa'],'aa','  ay']print(a[0][0])# remove ' laay 'print (a[0][1])# take out ' Laa '

2 tuples (tuple)

Tuples are almost identical to list usages, except that the elements in a tuple cannot be modified, but the tuple itself can be deleted!

A= ('laay','aay','ay','  y')

Python Learning sharing-list meta-groups

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.