Getting started with Python(10)

Getting started with Python (10/18)
Data structure: tuple

Hello everyone, let's continue to talk about the four major data structures of Python. Today's topic is: Tuples.

1、 Tuple

Tuples are very similar to lists, but lists allow and provide methods to change its value, while tuples are immutable, that is, you are not allowed to change its value, which should also have no method Part of the reason. Therefore, some people refer to it as the "List of Curses".

So, please remember:

(1) The objects contained in the tuple are unchangeable. Note that the tuple is not unchangeable.

(2) The length of the tuple changes due to update or deletion.

(3) The main function of tuples is to protect their contents from being modified by external interfaces when they are passed to function calls as parameters or when parameters are obtained from function calls.

(4) Tuples are not used much in the foreground, but they are used quite frequently in the background.

2、 Create a tuple

Putting the objects separated by commas in a pair of horns creates a tuple.

PS: Although tuples usually exist in parentheses, it is not enough to have only parentheses in form. Whether there is a comma or not can determine whether it is a tuple.

**Explanation: **We specially used all the objects in the previous list example to create a tuple. They look alike except for the parentheses. Do they look similar? Please note how to define an empty tuple and a tuple with only one single item. Look at the last defined tuple6 is really a tuple?

3、 Access the value in the tuple

**Description: **

1、 Display: the code in interactive mode can omit print().

2、 Index: Use the subscript to access the value of the object in the tuple.

3、 Negative index: Use negative subscripts to implement reverse indexing.

4、 Pass -1 index: Get the value of the last item.

5、 Slicing: Use colon section breaks for slicing operations.

6、 Special slice: get a complete reverse tuple.

4、 Update tuple

We can modify or update tuples using reassignment and append() methods.

**Description: **

1、 Use slices to achieve interpolation;

2、 Use the append() method to append new items to the end of the list.

3、 Tuples cannot pass the index and directly reassign the item with the specified subscript, nor can the del statement be used to delete the item with the specified subscript by the index.

5、 Operators for tuples

The operators of tuple pairs + and * are similar to lists. The + sign is used for merging, and the * sign is used for repetition.

6、 Conversion between tuples and lists

The conversion operation between tuples and lists allows it to easily convert lists into tuples, and then use the "laziness" of tuples to pass it as a parameter to a function call, or to protect it when parameters are obtained from a function call. The content is not modified by the external interface.

7、 zip() and zip(*)

**Description: **

(1) zip() will pack more than two sequences into a zip object (a new sequence composed of tuples).

(2) This new sequence can be traversed through iteration, or directly converted into a list or tuple.

(3) But no matter which way, it can only be read once, and then it will return a null value.

(4) The zip() operation is actually applicable to all sequences that support iterative operations, even strings.

**Description: **

(1), zip(*) will unpack a two-dimensional sequence into more than two sequences and generate a zip object.

(2) Unpack this object, you will find that it is actually multiple sequences before packing, so zip() and zip(*) can be mutually inverse operations.

(3) zip(*) also only supports one-time reading. Reading again returns a null value.

(4) The operations of zip() and zip(*) formally realize the exchange of rows and columns of multiple sequences. Remember this feature, we can use it in some specific situations.

7、 The function uses * to realize the transfer of arbitrary parameters

The function can use the wildcard character * to realize any number of parameters.

**Explanation: **Here, why the value of x is displayed as a tuple, because the function parameters themselves are objects in parentheses and separated by commas. In Python, of course it is a tuple, and it also reflects Python syntax is unified in content and form.

summary

In this lesson, we learned the data structure of Python: tuples. Learned in detail about its creation and usage, as well as the difference between tuples and lists, and their mutual conversion.

Preview

In the next lesson, we will continue to introduce the third Python data structure: dictionary, which is also one of the four commonly used data structures in Python. We will find that although it is also a sequence type data structure, it has a completely different internal organization form, so that Python can provide technical support for different application scenarios or requirements.

If you have any questions or suggestions, please leave a message.

Recommended Posts

Getting started with Python(18)
Getting started with Python(9)
Getting started with Python(8)
Getting started with Python(4)
Getting started with python-1
Getting started with Python(14)
Getting started with Python(7)
Getting started with Python(17)
Getting started with Python(15)
Getting started with Python(10)
Getting started with Python(11)
Getting started with Python(6)
Getting started with Python(3)
Getting started with Python(12)
Getting started with Python(5)
Getting started with Python (18+)
Getting started with Python(13)
Getting started with Python(16)
Getting started with Numpy in Python
Getting started with Ubuntu
Getting Started with Python-4: Classes and Objects
Getting started with python-2: functions and dictionaries
04. Conditional Statements for Getting Started with Python
Getting started python learning steps
How to get started quickly with Python
Play WeChat with Python
Web Scraping with Python
Centos6.7 comes with python upgrade to
Played with stocks and learned Python
Gray-level co-occurrence matrix (with python code)
Speed up Python code with Cython
How to make a globe with Python
Automatically generate data analysis report with Python
Create dynamic color QR code with Python
Python | Quickly test your Python code with Hypothesis
How to process excel table with python