Introduction to Python learning. With lists, why are there tuples?

coldplay.xixi
Release: 2020-12-18 17:23:06
forward
6187 people have browsed it

python video tutorialThe column introduces why there are tuples

Introduction to Python learning. With lists, why are there tuples?

Recommended (free) : python video tutorial

There is a basic data structure in Python called tuple, but generally few people will use it, because during the development process, lists (list) can basically meet our needs.

Even so, you must not think that tuples are redundant. Otherwise, you won’t often be asked in interviews, what is the difference between tuples and lists? Why do you need tuples?

The following two points, the first point is well known to everyone, and the second point may only be known by experienced drivers. Only by learning the second point can we truly understand the value and significance of the existence of tuples. .

1. Immutable list

This is the most significant feature of tuples that distinguishes them from lists.

list: mutable sequence

tuple: immutable sequence

So what is an immutable sequence?

That is, after the tuple object is generated, functions such as inserting elements, deleting elements, adding elements, clearing elements, modifying elements, etc. are not available in the tuple, and you cannot modify them. .

Since tuples are immutable, their methods are also very limited. List them here.

Introduction to Python learning. With lists, why are there tuples?

2. Named tuple

This feature, I personally think, is the meaning of the existence of tuples.

Only talks about named tuples, which may not be easy to understand. If you call it a record with field names, you may understand.

Here is an example, but to implement field names, you need the support of a library (collections), and you need to import it.

Introduction to Python learning. With lists, why are there tuples?

It looks a bit like a dictionary, right? But it is not a dictionary (the method of obtaining values ​​​​is also different from that of a dictionary). The dictionary is variable. Once a tuple is created, it cannot be modified. This shows to some extent that tuples are suitable for storing data that does not need to be modified. For example, above, place name, country, longitude and latitude.

In addition to the above usage, here we also introduce some unique properties of tuples.

Introduction to Python learning. With lists, why are there tuples?

# As a language that is constantly developing and popularizing, Python is still being updated. When studying, it is recommended to find some study partners to study and discuss together for better results. If you want to learn Python, you are welcome to join the Python learning exchange group (627012464) to supervise and learn together. It contains development tools, a lot of useful information and technical information to share!

To summarize, tuples are a very powerful data type that can be used as records. This is the value and significance of its existence. As we all know, its second role is to act as an immutable list. (The above are all personal opinions. If you have different opinions, please leave a message for discussion)

The above is the detailed content of Introduction to Python learning. With lists, why are there tuples?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jianshu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!