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

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:

主站蜘蛛池模板: 崇阳县| 昭觉县| 江口县| 晋州市| 余庆县| 金湖县| 叶城县| 兴义市| 金寨县| 马龙县| 盖州市| 罗甸县| 阜平县| 红安县| 乐山市| 察雅县| 温州市| 台中县| 武乡县| 蚌埠市| 祁门县| 旌德县| 巴彦淖尔市| 勐海县| 盐津县| 大竹县| 灌阳县| 津市市| 许昌市| 铅山县| 大名县| 胶南市| 容城县| 徐闻县| 三台县| 巴林右旗| 丁青县| 安庆市| 舟曲县| 胶州市| 浪卡子县|