Python - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Python Tuples

Python Tuples

shape Description

A tuple is a gathering of changeless Python objects. Tuples are courses of action, much the same as list concept. The differentiations among tuples and list are, the tuples can't be changed not at all like lists and tuples use walled in areas, while lists use square segments. The accompanying is an illustration. [c]>>> data=(10,20,'ram',56.8) >>> data2="a",10,20.9 >>> data (10, 20, 'ram', 56.8) >>> data2 ('a', 10, 20.9) >>> [/c] The following is an example for an empty tuple. [c]tuple1=() [/c] While declaring a single esteem, comma should be placed at end of the value. The following is an example. [c]Tuple1=(100,) [/c]

Accessing Tuple Values

shape Description

Here use the square brackets to place an elements and this on eof the difference between the tuple and list. The following is an example. [c]data1=(1,2,3,4) data2=('x','y','z') print data1[0] print data1[0:2] print data2[-3:-1] print data1[0:] print data2[:2] [/c] Now compile the code result will be as follows. [c]>>> 1 (1, 2) ('x', 'y') (1, 2, 3, 4) ('x', 'y') >>> [/c]

Updating The values Of Tuples

shape Description

Tuples are permanent which implies user can't overhaul or change the estimations of tuple components.. The following is an example. [c] tup1 = (222, 35.56); tup2 = ('abcd', 'abc'); # Following action is not valid for tuples # tup1[0] = 100; tup3 = tup1 + tup2; print tup3[/c] Now compile the code result will be as follows. [c](222, 35.56, 'abd', 'abc')[/c]

Deleting The Values Of Tuples

shape Description

Erasing singular component from a tuple is not bolstered. However the entire of the tuple can be erased utilizing the del articulation. The following is an example. [c]data=(100,200,'sachin',50.6,'y') print data del data #will delete the tuple data print data[/c] Now compile the code result will be as follows. [c]>>> (100,200,'sachin',50.6,'y') Traceback (most recent call last): File "C:/Python27/b.py", line 4, in print data NameError: name 'data' is not defined >>>[/c]

Summary

shape Key Points

  • The tuple(seq) is the method to convert list into tuple.
  • The len(tuple) is the method to find out length of a tuple.
  • The tuples are not changable that means user can not perform updating operations.