# 创建一个空元组
nums = ()
# 从1到10的整数中筛选出前5个偶数,并将它们添加到元组num中
for i in range(1,11):
if i % 2 == 0 and len(nums) < 5:
nums = nums + (i,)
# 定义另一个元组,并将其与nums合并
other_nums = (1, 3, 5, 7, 9)
nums = nums + other_nums
# 计算nums的长度
length = len(nums)
print('元组nums包含的前5个偶数加上另一个元组other_nums的结果是:', nums)
print('合并后的元组nums的长度为:', length)
输出:
元组nums包含的前5个偶数加上另一个元组other_nums的结果是: (2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
合并后的元组nums的长度为: 10