List of Python basics

Source: Internet
Author: User

List lists

Create list () lists using [] (brackets)

List lists, which can be stored in different types and elements, and can be modified after a defined list.

a=[1,2,3,4,5,6]

B=list ((3,2,3,2))

Alist=list (3,2,3,2,'I am this list')) Blist=['a','b','C','D','e','I'm a list, too .']Print(alist)Print(blist)#Output Result:[3, 2, 3, 2,'I am this list']['a','b','C','D','e','I'm a list, too .']

A, list index---slice [:], [::]

Print (Alist[1:3]) # left-to-right without stop bit 3, supports negative indexes (as in strings) # Output Result: [2, 3]
Print (Blist[1:8:2]) # from right 1 to left 8, the third parameter is Step 2 (one for each) # Output Result: ['b'd' I'm also a list ' ]
 print  (blist[:: -1]) #   reverse direction (reverse list contents)  #   output result:  [ "  I'm also a list  "  ,  e  "  ,  d  "  ,  c  "  ,  b  "  ,  a  "  ]
Print (Blist[4:2:-1]) # -1 Reverse direction take, from right to left, start to index 4 (right to left), end to index 2 (right to left), not end 2 # Output Result: ['e'd']

B, List add element

1, append () (Append to end)

Alist.append (' I'm append add in ')print(alist)#  Output:' I am this list '' I am append add in ']

2, insert () specify position append

Alist.insert (2,' add ' in 2 position ')print(alist)# output Result:  ' add ' in 2 position  ' I am this list '  I'm append added to the ']

3. Modify the list element

alist[2]='2 position modified 'print' (alist)# output:' 2 Location Modified ' ' I am this list ' ' I'm append add it in. ']

4. Popup element--return value

4.1, pop () does not pass the parameter default pop-up last a no-vegetarian

# A pop in the list has a return value, and you can define a variable to receive Print (APOP) #apop接收到弹出来的值, this is the output popup element. Print (alist) # This is the output of the alist # Output Result: I am append added ['2 position modified ' I am this list ']

4.2, Pop () incoming parameters can be specified to eject the non-vegetarian

Bpop=alist.pop (2)# pass in a parameter (list index), you can specify the location of the popup, through the variable Bpop receive the popup value print(bpop)#  Output the received value print(alist)# alist output # output:2 position modified [  ' I am this list ']

C, List nesting

Alist=list ((3,2,3,2, ' I am this list '), blist=[' A ', ' B ', ' C ', ' d ', ' e ', ' I'm a list ')

Alist.append (blist)#Add the blist to the Alist by append () so that a list is nested in a listPrint(alist)#Output Result:[3, 2, 3, 2,'I am this list', ['a','b','C','D','e','I'm a list, too .']]

Nested list values

Print # takes the value of index 3 in the list nested list, and the nested list index position is 5 # output Result:D

D, List length calculation len () alist=[3, 2, 3, 2, ' I am this list ', [' A ', ' B ', ' C ', ' d ', ' e ', ' I am also a list ']

Print (Len (alist)) # Output List Length # Output results:6

Some methods of Python Click here to view the ""

List of Python basics

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.