当需要创建“ N”个元素增量元组时,可以使用生成器表达式和“ tuple”方法。
以下是相同的演示-
N = 3 print("The value of 'N' has been initialized") print("The number of times it has to be repeated is : ") print(N) my_result = tuple((elem, ) * N for elem in range(1, 6)) print("The tuple sequence is : ") print(my_result)输出结果
The value of 'N' has been initialized The number of times it has to be repeated is : 3 The tuple sequence is : ((1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5))
“ N”的值已初始化,并显示在控制台上。
将具有特定元素的元组(在给定范围内乘以“ N”倍)分配给变量。
生成此序列,并在控制台上将其显示为输出。