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

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:

主站蜘蛛池模板: 牙克石市| 乌拉特前旗| 九台市| 梓潼县| 镇沅| 定南县| 遵化市| 陆河县| 泊头市| 西昌市| 肇东市| 黔江区| 平罗县| 广丰县| 定日县| 息烽县| 道真| 灵宝市| 雷波县| 遂宁市| 乐业县| 马尔康县| 永登县| 洛川县| 晋州市| 汽车| 抚顺县| 兴城市| 天津市| 阜平县| 荆州市| 龙岩市| 阜新市| 华阴市| 辰溪县| 湖州市| 达日县| 内江市| 鄂伦春自治旗| 玉环县| 大厂|