Python 元组(Tuple) 练习题

Posted by thouLee on June 1, 2021

Python 元组(Tuple) 练习题

1

将一个给定的整型元组:tuple(range(1, 7))倒序输出

1
2
3
4
nums = tuple(range(1, 7))

print(nums)
# (1, 2, 3, 4, 5, 6)
1
2
3
4
5
list_nums: list = sorted(nums, reverse=True)
tuple_nums = tuple(list_nums)

print(list_nums)
print(tuple_nums)
1
2
[6, 5, 4, 3, 2, 1]
(6, 5, 4, 3, 2, 1)

2

请问以下变量哪些是 tuple 类型:
A. a = ()
B. b = (1)
C. c = [2]
D. d = (3,)
E. e = (4, 5, 6)

1
A, D, E