Python data type (3) ancestor: tuple

Source: Internet
Author: User
This article introduces tuple In the Python data type. In short, Tuple is the same as the list, and the elements are immutable, but different from the list, different types of elements can be contained in a ancestor. I. Basic Data Types

Integer: int
String: str (Note: \ t is equal to a tab key)
Boolean value: bool
List: list
List []
Ancestor: tuple
Ancestor use ()

Dictionary: dict
Note: All data types exist in the desired class columns. The list can be modified and the ancestor cannot be modified in the same way as the list function.

Ii. list all data types:

Basic operations:

Index, slice, length, inclusion, and loop

Class tuple (object): "tuple ()-> empty tuple (iterable)-> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. "def count (self, value): # real signature unknown; restored from _ doc _" "T. count (value)-> integer -- return number of occurrences of value "" (T. count (value)-> integer, number of occurrences of returned values) return 0 def index (self, value, start = None, stop = N One): # real signature unknown; restored from _ doc _ "" T. index (value, [start, [stop])-> integer -- return first index of value. raises ValueError if the value is not present. "" (T. Index (value, [start, [do not])-> integer, returns the first index value. It puts forward the value of ValueError if it does not exist .) Return 0 def _ add _ (self, * args, ** kwargs): # real signature unknown "" Return self + value. "pass def _ contains _ (self, * args, ** kwargs): # real signature unknown" Return key in self. "pass def _ eq _ (self, * args, ** kwargs): # real signature unknown" Return self = value. "pass def _ getattribute _ (self, * args, ** kwargs): # real signature unknown" Return getattr (self, name ). "pass def _ getitem _ (self, * args, ** kwargs): # real signature unknown" "Return self [key]. "pass def _ getnewargs _ (self, * args, ** kwargs): # real signature unknown pass def _ ge _ (self, * args, ** kwargs): # real signature unknown "" Return self> = value. "pass def _ gt _ (self, * args, ** kwargs): # real signature unknown" Return self> value. "pass def _ hash _ (self, * args, ** kwargs): # real signature unknown" Return hash (self ). "pass def _ init _ (self, seq = (): # known special case of tuple. _ init _ "tuple ()-> empty tuple (iterable)-> tuple initialized from iterable's items If the argument is a tuple, the return value is the same object. # (copied from class doc) "pass def _ iter _ (self, * args, ** kwargs ): # real signature unknown "" Implement iter (self ). "pass def _ len _ (self, * args, ** kwargs): # real signature unknown" Return len (self ). "pass def _ le _ (self, * args, ** kwargs): # real signature unknown" Return self <= value. "pass def _ lt _ (self, * args, ** kwargs): # real signature unknown" Return self
 
  

Iii. Examples of all ancestor Data Types

# Count is used to calculate the number of elements that appear name_tuple = ("zhangyanlin", "suoning", "nick") print (name_tuple.count ('zhangyanlin ')) # index obtain the specified position of the specified Element name_tuple = ("zhangyanlin", "suoning", "nick") print (name_tuple.index ('zhangyanlin '))

Iv. Index

name_tuple = ("zhangyanlin","suoning","nick")print(name_tuple[1])

5. Slicing

# Retrieve the name_tuple = ("zhangyanlin", "suoning", "nick") print (name_tuple [0: len (name_tuple)-1]) element whose first digit minus 1.

6. Total length len

# Retrieve name_tuple = ("zhangyanlin", "suoning", "nick") print (name_tuple [len (name_tuple)-1])

VII. for Loop

name_tuple = ("zhangyanlin","suoning","nick")for i in name_tuple:  print(i)

So what are the advantages of using tuple?

Tuple is faster than list operation. If you define a constant set of values, and the only thing you want to do with it is to continuously traverse it, use tuple instead of list.
If you perform write protection on data that does not need to be modified, the code can be safer. Using tuple instead of list is like having an implicit assert statement, which indicates that the data is a constant. If you must change these values, you need to convert tuple to list (a special function is required ).

Remember when I said that dictionary keys can be strings, integers, and "other types? Tuples is one of these types. Tuples can be used as the key in dictionary, but the list cannot. In fact, things are more complicated than that. The Dictionary key must be unchangeable. Tuple itself cannot be changed, but if you have a list of tuple, it is considered variable and it is not safe to use it as a dictionary key. Only string, integer, or other tuple that is safe for dictionary can be used as the dictionary key.

Tuples can be used in string formatting. We will see it soon.

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.