Python path-basics-6 list and tuple, pythontuple

Source: Internet
Author: User

Python path-basics-6 list and tuple, pythontuple

List:

A built-in data type in Python is list: list. List is an ordered set that allows you to add and delete elements at any time.

For example, you can use a list to list the names of all the students in the class:

Classmates = ['Michael ', 'Bob', 'tracy '] print (classmates) # output # ------------------------------------------- ['Michael', 'bob', 'tracy ']

 

VariableclassmatesIs a list. Uselen()The function can obtain the number of list elements:

Print (len (classmates) # output: # --------------------------------------------------- 3

 

Use indexes to access the elements at every position in the list.0Start:

Print (classmates [0]) # output --> 'Michael 'print (classmates [1]) # output --> 'bob' print (classmates [2]) # output --> 'tracy'
Print (classmates [3]) # output: # define Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range

When the index is out of the range, Python will report an IndexError. Therefore, make sure that the index does not cross the border. Remember that the index of the last element islen(classmates) - 1.

 

To obtain the last element, you can use-1Index to directly obtain the last element:

Print (classmates [-1]) # output-> 'tracy'

 

And so on, you can get 2nd to the last and 3rd to the last:

Print (classmates [-2]) # output --> 'bob' print (classmates [-3]) # output --> 'Michael 'print (classmates [-4]) # output: # ------------------------------------------------------ raceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range

Of course, the last 4th will be out of the border.

 

In the world of python, everything is an object. Each object has some methods. You can see what methods are available using the dir (Object Name:

Index (): returns the index of the first value if the value exists multiple times.

Insert (): insert a value at a specified position in the list.

Pop (): Delete the last element of the list and print it.

Remove (): similar to pop, but this is to delete the specified value. If this value does not exist, a ValueError error message is returned.

Reverse (): returns the list in reverse order.

Sort (): sort the list

 

 

Tuple tuples:

Another ordered list is tuple. Tuple and list are very similar, but tuple cannot be modified once initialized.

Tuple is not variable, so the code is safer. If possible, use tuple instead of list.

The method for creating a tuples is the same as that for creating a list. The only difference is that when a tuple has only one value, you need to create it as follows:

If no comma is added, the name value is a string:

 

Tuples:

There are only two methods for tuples: count and index.

The usage of these two methods is the same as that of list

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.