In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Python's tuple Tuple". In daily operation, I believe many people have doubts about how to use Python's tuple Tuple. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python's tuple Tuple". Next, please follow the editor to study!
I. definition
A tuple can be understood as a read-only list, identified by ().
Define an empty tuple variable = ()
But if there is only one element in the tuple, is it a tuple or some other data type? Let's check it out.
Tuple_1 = 1) print (type (tuple_1)) tuple_2 = ("hello") print (type (tuple_2))
The above data types are not tuples. To express that a tuple with only one element is defined as a variable = (element 1,), let's verify it:
Tuple_1 = (1,) print (type (tuple_1)) tuple_2 = ("hello") print (type (tuple_2))
II. Merge, repeat and delete
List merging uses the "+" operation. Note that element values in tuples are not allowed to be modified.
Reuse "*" in the list
Element values in tuples are not allowed to be deleted, but we can use the de statement to delete the entire tuple.
Multiple values receive values in the tuple: variable 1, variable 2, … = (element 1, element 2, … )
The specific uses are as follows:
Tuple1 = ("Zhang San", "Li Si", "Wang Wu") tuple2 = ("Ma Liu", "Chen Qi") tuples = tuple1 + tuple2print (tuples) tuple3 = tuple2*3print (tuple3) teacher, student = ("Ma Liu", "Zhang San") print ("teacher is" + teacher+ ", student is" + student ")
III. Tuple conversion
The tuple () function and list () function are all English name functions of data types, and these English name functions are actually used for casting. There is also the collection set ().
The tuple () function takes a sequence as an argument and converts it to a tuple, and if the parameter is a tuple, the data is returned as a metadata type. List () and set () are similar.
Strs = "hello" print (tuple (strs)) print (list (strs)) print (set (strs))
4. Methods in tuples
Index (), Cong returns the index of the first specified element encountered from left to right, and if not, an error is reported.
Count () returns the number of specified elements in the tuple.
The use is as follows:
Tup = ("Python", "C++", "C language", "Java", "Python", "principles of compilation", "principles of computer") print (tup.index ("Python")) # output the index print (tup.index ("Python", 1pm6) of the first python from left to right) # output the number of indexes print (tup.count ("Python")) # of the first Python between intervals 1-6
At this point, the study on "how to use Python's tuple Tuple" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.