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

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:

主站蜘蛛池模板: 东至县| 临朐县| 高雄县| 永城市| 新乡市| 阜南县| 葫芦岛市| 论坛| 麻城市| 泽库县| 陆川县| 武陟县| 阳城县| 名山县| 沂源县| 潼关县| 滁州市| 枝江市| 班玛县| 读书| 福州市| 赤壁市| 开平市| 南江县| 淄博市| 思南县| 昌都县| 邯郸县| 新民市| 乌拉特中旗| 侯马市| 隆化县| 和顺县| 酒泉市| 革吉县| 英德市| 钦州市| 伊春市| 太谷县| 民县| 什邡市|