python-demo/basic/元组.py

23 lines
611 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@File : 元组.py
@Author : lichao
@Date : 2024/11/27 17:21
@Software : PyCharm
"""
tuple_data = (1, 2, 3, 4, 5)
print(tuple_data)
# 元组不可变,不能修改,不能删除,不能添加
# tuple_data[0] = 2 # TypeError: 'tuple' object does not support item assignment
# 错误的推导式,会生成一个生成器
tuple_g_data = (i for i in range(1, 10))
print(tuple_g_data) # <generator object <genexpr> at 0x7f8f8f7b4d60>
# 可以使用tuple来生成元组
tuple_g1_data = tuple(i for i in range(1, 10))
print(tuple_g1_data)