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

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:

主站蜘蛛池模板: 新野县| 石林| 黄浦区| 丰镇市| 安丘市| 荣昌县| 乾安县| 来安县| 东光县| 延寿县| 巴东县| 宁明县| 会东县| 凌云县| 玛曲县| 海宁市| 扶沟县| 长阳| 杭州市| 万荣县| 屯门区| 余庆县| 阜南县| 临沂市| 明溪县| 桐乡市| 长子县| 同德县| 九龙坡区| 涡阳县| 天镇县| 宁明县| 交城县| 忻城县| 元江| 大渡口区| 太原市| 岳池县| 桃园县| 巍山| 台前县|