Home  >  Article  >  Backend Development  >  What is the use of python tuple?

What is the use of python tuple?

little bottle
little bottleOriginal
2019-05-22 09:49:159643browse

The role of tuple in Python: when passed as a parameter to a function call, or when parameters are obtained from a function call, its content is protected from modification by the external interface. Typically used to enable a statement or user-defined function to safely take a set of values, that is, the value of the tuple being used will not change.

What is the use of python tuple?

Python is a computer programming language and a scripting language. It is very easy to use compared to other languages. This makes more and more people willing to learn Python. However, some syntax in Python requires more practice before we can truly understand its meaning. Below, I will take you to understand what tuples in Python do?

The ordered list in Python is called tuple.

Tuples are defined by comma-separated items in parentheses.

The main function of the tuple is to protect its content from being modified by the external interface when it is passed as a parameter to a function call or when parameters are obtained from a function call.

is usually used when a statement or user-defined function can safely take a set of values, that is, the value of the tuple being used will not change.

tuple is very similar to list, but once the tuple is initialized, it cannot be modified . For example, the names of classmates are also listed:

>>> classmates = ('Michael', 'Bob', 'Tracy')

Now, the tuple "classmates" cannot be changed. , it also does not have methods such as append() and insert(). Other methods of obtaining elements are the same as lists. You can use classes[0] and classes[-1] normally, but they cannot be assigned to other elements.

What is the significance of immutable tuple?

Because tuples are immutable, the code is safer. If possible, use tuple instead of list.

Use of python tuple

1. Tuples are faster than list operations. When traversing operations, be sure to use tuples, which will make the speed much faster

What is the use of python tuple?

2. "Write-protect" data that does not need to be modified can make the code more secure

What is the use of python tuple?

3. Can be used as a key in a dictionary, but not a list

What is the use of python tuple?

4. Can appear in string formatting

What is the use of python tuple?

The above is the detailed content of What is the use of python tuple?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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