官术网_书友最值得收藏!

Tuples in Python

A Python tuple is very similar to a Python list. The difference is that it's a read-only structure, so once it is declared, no modification can be made to the elements of the tuple. Python tuples can be used as follows:

In the preceding code, we can see that we can access tuples in the same way as we can access lists, but when we try to change any element of the tuple, it throws us an exception as a tuple is a read-only structure. If we perform the operations that we performed on lists, we will see that they work in exactly the same way as tuples:

If a tuple has only one element in it, it has to be declared with a trailing comma. If we do not add that comma while declaring it, it will be interpreted as a numeric or string data type, depending on the elements of the tuple. The following example explains this better:

A tuple can be converted into a list and can then be operated on as follows:

主站蜘蛛池模板: 高清| 长汀县| 万盛区| 辽宁省| 耒阳市| 曲靖市| 武强县| 铅山县| 许昌市| 确山县| 平度市| 江陵县| 荆门市| 南丰县| 基隆市| 临颍县| 高平市| 陇川县| 彩票| 呼和浩特市| 安平县| 镇康县| 云阳县| 利辛县| 喀喇沁旗| 龙州县| 西乌珠穆沁旗| 托克托县| 上林县| 布尔津县| 科技| 台南县| 长垣县| 宜兰市| 山东| 饶阳县| 固始县| 娱乐| 长春市| 溆浦县| 榕江县|