Python data containers - lists, tuples, strings, sets, dictionaries

Author: Insist--

Personal homepage: insist--personal homepage

Column for this article: Python column

Column introduction: This column is a free column and will continue to update the basic knowledge of python . You are welcome to subscribe and pay attention.

Table of contents

1. Understand data containers

1. Why do we need data containers?

2. What is a data container?

2. Data container—list (list)

1. Definition of list

2. Characteristics of lists

3. Example questions

3. Data container—tuple

1. Definition of tuple

2. Characteristics of tuples

3. Example questions

3. Data container—string (str)

1. Definition of string

2. Characteristics of strings

4. Data container—set

1. Definition of set

2. Characteristics of collections

3. Example questions

5. Data container - dictionary (dict)

1. Dictionary definition

2. Features of dictionary

3. Example questions


Preface

Data containers play a vital role in Python. This article will explain Python's data containers, including list (list), tuple (tuple), string (str), set (set), and dictionary (dict).

1. Understand data containers

1. Why do we need data containers?

Suppose you are a teacher and you now need to use a program to record the information of three students in the class. What do you need to do?

Some people may say that you can directly define three string variables, as shown in the following code:

name1 = "学生1"
name2 = "学生2"
name3 = "学生3"

Although this method can meet our needs, it is very troublesome to write. There is only information about 3 students here, but what if there are 1,000? Do we need to define 1000 variables? This is obviously unrealistic. Is there any way to solve this problem? Of course, we have to talk about our data container. For example, we define a list to store, as shown in the following code:

my_list = ["学生1","学生2","学生3"]

It can be seen that we only use one variable to receive multiple pieces of data. From the above article, we can conclude that the Python data container has the function of organizing data , which is why we need data containers.

2. What is a data container?

Through the above article, everyone already knows something about data containers. Now let’s talk about what a data container is in detail?

A data container is a data type that can store multiple pieces of data. The data in the data container is called an element. The element can be any data type (string, integer, Boolean, etc.) . The data container is divided into 5 categories, namely : List (list), tuple (tuple), string (str), set (set), dictionary (dict) . Below we will explain these five types of data containers respectively.

2. Data container—list (list)

For related operations on lists, please refer to other articles in the column

As mentioned above, using a list to store the data of 1,000 students is much simpler than defining 1,000 variables. A list is a type of data container. Let’s explain it in detail below:

1. Definition of list

Lists can store multiple elements, and the type is not restricted . To define a list, we can use square brackets and separate elements with commas, as shown in the following code:

list_name = [element1, element2, ...]

list_name in the code is the name of the list, element1, element2 are the data stored in the list, called elements.

We can also define an empty list, such as:

# 定义空列表的 第一种 方法
empty_list = []
# 定义空列表的 第二种 方法
empty_list = list()

2. Characteristics of lists

  • Can store multiple data and support storage of multiple types
  • Data is stored in order and data duplication is allowed
  • Content can be modified (modification, deletion, etc.)

3. Example questions

After reading the above demonstration, can you define a list? Let's look at a very simple example:

Example : Define a list, store the three data ("Zhang San", 666, True) in the list, and print it out

answer:

# 定义一个列表,列表内存储着("张三",666,True)
list_name = ["张三",666,True]
# 使用 print 将列表打印出来
print(list_name)

Output result:

7453ef640fc34d8086baf94f228f212f.png

3. Data container—tuple

For related operations on lists, please refer to other articles in the column

1. Definition of tuple

From the characteristics of the list, we know that the list supports modification. So what should we do if the data we store does not want to be modified? It's very simple, just use tuples to store it.

In Python, a tuple is an immutable sequence type . To put it simply, tuples do not support modification. A tuple can be defined using parentheses and the elements separated by commas. The following code:

my_tuple = (666, 'ABC', 9.2)

Likewise, my_tuple is the name of the tuple and 666, 'ABC', 9.2 is the data stored in the tuple.

Note : If you do not write a variable name when defining a tuple, Python will create an anonymous tuple. Anonymous tuples are primarily intended for one-time use.

2. Characteristics of tuples

  • Can store multiple data and support storage of multiple types
  • Data is stored in order and data duplication is allowed
  • Supports looping, but does not support data modification

3. Example questions

Similarly, let’s practice defining tuples, as in the following example;

Example : Define a tuple, store (6, "insist", 1.1) in the tuple and print it out. You can also try to store the same elements and see what happens?

answer:

# 定义一个元组,元组内存储着(6,"insist",1.1)
tuple1 = (6,"insist",1.1)
# 使用 print 将元组的内容打印出来
print(tuple1)

Output result:

c7f8e48507cc411fbddb96fb00b4ad22.png

3. Data container—string (str)

String related operations will be published in this column later.

1. Definition of string

In Python, characters enclosed in single quotes or double quotes are called strings. Define a string with the following code:

# 双引号定义
str1 = "insist"
# 单引号定义
str2 = 'insist'
# 三引号定义
str3 = '''
insist
'''

2. Characteristics of strings

  • Different from the above, string only supports storage of string type
  • Like tuples, strings are also immutable
  • Orderly storage and allowing data duplication
  • Supports looping

4. Data container—set

The related operations of the collection will be published in this column in the future.

1. Definition of set

The three data containers (list, tuple, string) have been explained above. The data in these three data containers support duplication. If we need to deduplicate the data, then these three can be used. It’s not very convenient. In Python, sets do not allow data duplication (equivalent to the built-in deduplication function), so let’s learn about sets. We can create a collection by writing elements in {} (separate elements with commas), as shown in the following code:

set1 = {1,2,3,4,5}

2. Characteristics of collections

  • Can store multiple data and can store multiple types
  • The data is unordered and no duplication of data is allowed
  • Only supports for loop

3. Example questions

Let’s practice defining sets again, as in the following example:

Example : Define a set, store (1,1,1,2,2,2,3,3,3) in the set and print it out to see what happens?

answer:

# 定义一个集合,集合内存储着(1,1,1,2,2,2,3,3,3)
set1 = {1,1,1,2,2,2,3,3,3}
# 使用 print 将集合的内容打印出来
print(set1)

Output result:

1debf05a7cd346b99af7bd0dcbdc1a0b.png

As you can see, the collection has deduplicated the data.

5. Data container - dictionary (dict)

Dictionary related operations will be published in this column later.

1. Dictionary definition

00ed051ca201414c90e0698287e9ba45.webp

The dictionaries in our lives can query the meaning of words through words. The dictionaries in Python are very similar to the dictionaries in life. The dictionaries in life use words to query the meaning of words. The dictionaries in Python use Key to find the corresponding Value, let's first define a dictionary. The definition of dictionary is the same as that of a collection. {} is also used, but the dictionary stores one pair of key-value pairs (key: value, just like Zhang San in the following code: 88 ) the following code:

dict1 = {'张三':88,
         '李四':18
}

In the above code, John is the name, and 88 is its score. We can find the corresponding value by entering its name (key), as shown in the following code:

print(dict1["张三"])

We can find the corresponding value through key through the above method.

2. Features of dictionary

  • Can store multiple data and can store multiple types
  • Data is stored in key-value pairs
  • The key in the key-value pair cannot be repeated
  • Can be modified and only supports for loops

3. Example questions

Let’s practice some dictionary-related knowledge again, with the following examples:

Example: Define a dictionary that stores the names and scores of 3 students. After the definition is completed, we find the corresponding scores through the names and print them out.

answer:

dict1 = {'张三':88,
         '李四':18,
         '王五':2
}

print(dict1["王五"])

Output result:

You can see that the corresponding value has been found through the key.

Guess you like

Origin blog.csdn.net/m0_73995538/article/details/133046112