White Science Python (11): basic data structure (tuples)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

White Science Python (10): basic data structure (list) (lower)

Tuple (tuple)

In Python, tuples and lists are very similar to a data structure, if there is a good look for a list of the previous article, then just grab some central point, this article may seem a little redundant.

Briefly explain the difference between tuples and lists of:

  • Elements of a tuple can not be changed
  • Tuples use parentheses, square brackets list

Creating a tuple

Is a comma-separated tuple, however for aesthetic and readability, the general increase in parentheses.

tuple1 = "Python", "Java", 2011, 2015
print(tuple1)

tuple2 = ("Python", "Java", 2011, 2015)
print(tuple2)
print(type(tuple2))

Execution results are as follows:

('Python', 'Java', 2011, 2015)
('Python', 'Java', 2011, 2015)
<class 'tuple'>

In the creation of tuples may contain a list, as follows:

tuple3 = ("Python", "Java", [1 ,2, 'python', 'java'], 2011, 2015)
print(tuple3)

Results of the:

('Python', 'Java', [1, 2, 'python', 'java'], 2011, 2015)

Basic operation tuple

Tuples and lists the basic operation is very similar, comprising:

  • index
  • slice
  • connection
  • copy
  • The internal elements of the cycle
  • Is there an element tuple lookup
  • Delete tuple
  • Returns the maximum value and the minimum value tuple
  • Get Meta Group Length

So many operations, I do not separate shows, directly attached to a piece of code, the specific meaning has been added in a note:

tuple4 = (0 ,1, 2, 3, 4, 5, 6, 7, 8, 9)
print(tuple4)
# 索引
print(tuple4[2])
# 索引
print(tuple4[-2])
# 切片
print(tuple4[0:8:2])
# 切片
print(tuple4[8:1:-1])

tuple5 = (2333, '98k')
# 连接
print(tuple4 + tuple5)
# 循环
for index in tuple4:
    print(index)
# 查找元素是否存在
print(1 in tuple4)
print(11 in tuple4)
# 删除元组
# del tuple5
# print(tuple5)

# 取最大
print(max(tuple4))
# 取最小
print(min(tuple4))
# 元组长度
print(len(tuple4))
# 修改元组
# tuple4[0] = 11

Results of the:

(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
2
8
(0, 2, 4, 6)
(8, 7, 6, 5, 4, 3, 2)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2333, '98k')
0
1
2
3
4
5
6
7
8
9
True
False
9
0
10

The last modification tuple will complain, so I'll commented out, interested students can look at the specific error remove the comment content.

Mutual conversion between the tuple list

# 相互转化
print(type(tuple4))
print(list(tuple4))
print(type(list(tuple4)))
list1 = [0 ,1, 2, 3, 4, 5, 6, 7, 8, 9]
print(type(list1))
print(tuple(list1))
print(type(tuple(list1)))

The results are as follows:

<class 'tuple'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<class 'list'>
<class 'list'>
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
<class 'tuple'>
  • In the list of coat layer  tuple() can be converted to a tuple
  • In tuple coat layer  list() can be converted to a list

Tuple unpacking

# 元组解包
tuple6 = (1, 2, 3)
print(tuple6)
a, b, c = tuple6
print(a, b, c)

The results are as follows:

(1, 2, 3)
1 2 3

I hope that students in the learning process can practice hands-on look at the example code, after all, they do not knock the code is never learn to code :)

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

 

转载声明:本博客由极客挖掘机创作,采用  CC BY 3.0 CN 许可协议。可自由转载、引用,但需署名作者且注明文章出处。

Guess you like

Origin www.cnblogs.com/aliswell2king/p/11753929.html