Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the tuples and lists of Python

Shulou Source: shulou.com Published: 2022-06-01 06:08:14 10月12日 Update

This article mainly shows you "what is the tuple and list of Python". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what is the tuple and list of Python".

1 tuple

The data types of elements in the same tuple can be different

When there is only one element in a tuple, you need to add a comma after the element, otherwise parentheses will be used as operators

Tuple creation: assignment command = or built-in function tuple ()

Create an empty tuple-- tup1 = tuple () or tup1 = ()

Element values in tuples cannot be modified, but tuples can be concatenated, similar to strings, and tuples support two-way indexing.

Interception of tuples: variable [head subscript: tail subscript: step size] follow the principle of closing left and opening right

Tuple element access: variable [subscript] gets a single element in a tuple through the subscript index

View the operation methods of tuple objects

Print (dir (tuple)) # returns the number of occurrences of element x in the tuple tuple.count (x): # returns the number of times that element x first appears within the specified range of tuples tuple.index (x [, start, [stop]])

2 list

List creation: assignment command or built-in function list ()

Lst1 = []; lst1 = list ()

Like strings, lists also support two-way indexing

Interception of list: variable [header subscript, trailing subscript, step size], following the principle of closing left and opening right

Access to list elements: variable [subscript]. Get a single element in the list through the subscript index

To delete a list or delete an element in the list, you can use the delete command del

How to view list objects

Print (dir (list))

The list contains 11 built-in methods

# add a new element at the end of the list x (add the original list) list.append (x): # clear the list list.clear (): # return the shallow copy of the list list.copy (): # return the number of times the element x appears in the list list.count (x): # append multiple values in another sequence list.extend (seq): # return element x in the list at one time Specifies the number of times the range occurs for the first time list.index (x [ Start [, stop]]): # element x insert the specified location in the list list.insert (index,x): # remove an element from the list (default last element) Return the element value list.pop ([index=-1]): # remove the element that appears for the first time in the list xlist.remove (x): # the list is arranged in reverse order (all list elements are reversed) list.reverse (): # the list is sorted in place (list elements belong to the same data type, default ascending order) list.sort ():

The above is all the contents of the article "what are the tuples and lists of Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Element subscript variable number index content command article similarity function single in place two-way character string object data method end type Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Tech Info Redmi Shulou Information vpn