Header Ads

List and Tuples

List and tuples:

Tuples:- 

It is an ordered sequence.

Tuples are expressed as follows,


Tuples_1 = (1,2,3,4,5,6,7,8,9,10)

Tuples contain numbers same as list and represented in  ' () ' bracket.
each number of tuples is separated by symbol ' , '  . Tuple contain String,Float or integer. for example,


Tuples_2 = ('hello' , 920.5 , 941) 

And type of tuple is ,
type(Tuple_2) = tuple

Each tuple of element can be accessed by index.each index start from 0 and represented as,


Tuple_2[0] : 'hello'

We can concatenate tuple as follows,

Tuple_3 = (' hi ' , 5)Tuple_4 = Tuple_3 + (6 , 8)
Tuple _4 = (' hi ', 5, 6, 8)


Slicing of Tuple :

Suppose Tuple_4 given as,

Tuple _4 = ('hi', 5, 6, 8)

then we can write slicing of tuple using command,

Tuple_4[0:2]  # one more than required index


Ans: ('hi',5)


We can also find length of tuple by finding len() command

len('shop', 8, 6, 99)  => 4

Sorting the tuple:


Tuple_5 = (40,50,90,6,7,63,7)
Tuple_6 = sorted(Tuple_5)    # Sort tuple


Tuples can also access nested index,

suppose you have nested index of tuple


ln = (551,99,(55,66),77,('demo',99))
then each index represented in different tuple . if you want to access the tuple inside tuple then use this command

ln[2][1]  => 55   # answer is 55
or 
ln[2]  => (55,66)






















No comments

Powered by Blogger.