What are some of the common operations that these data are prone to?
In addition to the addition, deletion, and modification of data (except for the tuple immutable length and element immutability), we also need the following operations:
- Compare and contrast operation
- Calculate the number of elements
- Print out the container
- Get container type
utilization == The operation symbols are compared to see if they are equal len(container object) str(container object) type(container object)#type supports type determination for various objects.
Let's look at the code for a couple of containers
Strictly speaking, we don't use the tuple tuple type as a data container.
We use it more often to describe the structure of fixed lengths.
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/8 12:40 AM # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : __init__. # @Project : hello tuple1 = ("name", "leixuewei") tuple2 = ("name", "leixuewei") print("len : ", len(tuple1)) print("== : ", tuple1 == tuple2) print("dict1 : ", str(tuple1)) print("type : ", type(tuple1))
The running effect is as follows:
Here is the same operation for list:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/8 12:40 AM # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : # @Project : hello list1 = ["name", "leixuewei"] list2 = ["name", "leixuewei"] print("len : ", len(list1)) print("== : ", list1 == list2) print("list1 : ", str(list1)) print("type : ", type(list1))
The running effect is as follows:
The following are operations on the dict dictionary type:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/8 12:40 AM # @Author : LeiXueWei # @CSDN/Juejin/Wechat: Lei Xuewei # @XueWeiTag: CodingDemo # @File : __init__. # @Project : hello dict1 = {"name": "leixuewei"} dict2 = {"name": "leixuewei"} print("len : ", len(dict1)) print("== : ", dict1 == dict2) print("dict1 : ", str(dict1)) print("type : ", type(dict1))
The running effect is as follows:
summarize
These operations above are python built-in functions, for several kinds of data containers, the operation is very symmetrical, and do not need to be specifically memorized. More than a few times to knock the code will be memorized.
That's all for this post, I hope it was helpful and I hope you'll check back for more from me!