001 Python List Index

Source: Internet
Author: User
Tags python list

# Python 3 Basic syntax
List Index
0-1 represents the last
0list can be placed in a variety of types

# -*-coding:utf-8-*-names = ['Mark'coloe', 1,2,3,4,5,3.14, True]print (type (names))print(names)  Print(names[1])print(names[0])print(names[-1])

list nesting uses

1 #-*-coding:utf-8-*-2names = ['Mark','Coloe', ['I',' Love','poedu','!',],1,2,3,4,5,3.14,true, ['I',' Love','Mark','!',]]3 Print(type (names))4 Print(names)5 Print(Names[2])#nested list6 Print(Names[-1])#nested list7 Print(Names[-1][1], names[2][2])#gets the value of the nested list and outputs

List append[]
Join at the end

1 #-*-coding:utf-8-*-2names = ['Mark','Coloe', ['I',' Love','poedu','!',]]3 Print(names)4Names.append ('Google')5 Print(names)6Names.append ('Baidu')7 Print(names)8Names.append ('poedu')9 Print(names)

List insert[]
Specify location Insertion

Parameter 1: Position

Parameter 2: Inserted value

1 #-*-coding:utf-8-*-2names = ['Mark','Coloe', [' Love','poedu','!',]]3 Print(names)4Names.insert (1,'Carrot')5 Print(names)6Names.insert (4,'So Much!')7 Print(names)

list Clear ()
Empty index

1 #-*-coding:utf-8-*-2names = ['Mark','Coloe', [' Love','poedu','!',]]3Names.insert (1,'Carrot')4Names.insert (4,'So Much!')5 names.clear ()6 Print(names)

list copy ()
Copy Index

1 #-*-coding:utf-8-*-2names = ['Mark','Coloe', [' Love','poedu','!',]]3other =names.copy ()4 Print(names)5 Print(Other)

list pop ()
Delete End of index
Parameter 1: Delete data at the specified location
It feels like a stack.

1 #-*-coding:utf-8-*-2names = ['A','B','C','D','E','F','G','H','I','J','K',]3 Print(names)4 Names.pop ()5 Print(names)6 names.pop (0)7 Print(names)8Names.pop (3)9 Print(names)

Python tuples
A python tuple is similar to a list, except that the elements of a tuple cannot be modified.
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.

1#-*-coding:utf-8-*-2Names = ('A','B','C','D','E','F','G','H','I','J','K')3 print (type (names))4 print (names)5 #元祖访问6Print (names[0])7Print (names[1:5])8Print (names[6:])9Print (names[6:-1])

001 Python List Index

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.