Python's tuple, the difference between a list and a dictionary

Source: Internet
Author: User

The Python language contains 6 built-in sequences, of which there are two main types: lists and tuples.

Lists can be modified, and tuples are not allowed, and if you want to add or remove certain elements, you can only use a list, in order to restrict certain elements, you will use tuples. In general, lists can be substituted for tuples. In the list, there are indexes, shards, add, multiply and so on.

One, list (lists) with square brackets []
A list is a built-in data type of Python, and list is an ordered set of elements that can be added and removed at any time.
Gets the list of elements to be obtained with a corner mark, a corner mark can use a positive angle, or you can use a negative angle, throw indexerro when out of bounds
The data type of the elements in the list can also be different (just like in Java), or you can put another list within the list, which creates a multidimensional collection

The common methods are:

1.len () Gets the length of the list

2.x.append ("hello") adding elements to the list

3.x.pop () Popup End Element

1Zoo = ['Pig','Tiger','Dog']2 Print(Len (Zoo))3Zoo.append ("Nihao")4 Print(Zoo)5 Print(Zoo.pop ())6 Print(zoo[0])7 Print(Zoo[-1])
Second, a tuple (tuple) with parentheses ()

Ganso is also an ordered list, and the list is very similar, the difference is the tuple once defined can not be modified, in a sense this also improves the security of the code, the Query method and list, the use of a tuple can use a tuple.
Add "comma" when defining Ganso with only one element to avoid ambiguity with mathematical operations

Variable tuple
When a list is placed inside the ancestor, the value of the list in the ancestor can change, in fact the tuple does not change, the value of the internal list is changed.

1 country = ('bj', ['sh','gd'  ])2print(country[1])3print(country[1 ][0])4 country[1][0] ="nihao"5Print (Country[1][0])
Third, the dictionary

Dictionary (dictionary): using {} to define
dict={' name ': ' Pei ', ' age ': ' 26 '}

Python's tuple, the difference between a list and a dictionary

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.