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

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:

主站蜘蛛池模板: 永胜县| 城固县| 壤塘县| 理塘县| 双江| 盘山县| 宁海县| 新乡市| 长治市| 五原县| 西乌珠穆沁旗| 漯河市| 榆社县| 台南市| 大埔县| 鄂尔多斯市| 巴彦县| 读书| 庐江县| 云龙县| 华宁县| 河间市| 祁阳县| 盐山县| 微山县| 旅游| 枣强县| 石林| 佳木斯市| 巫溪县| 西贡区| 河西区| 弥勒县| 正镶白旗| 云霄县| 司法| 阳城县| 镇安县| 株洲市| 尉氏县| 西安市|