Python Tuples

1. basic

Tuples is a sequence of immutable object. It's a sequence, just like List. However, it cannot be changed.

a = (1,2,3)

If a tuples only contain a object, we still need to include a comma.

b = (1,)

 

2. accessing the value

b = (1,2,3,4,5)
b[0] #b[0] == 1
b[1:3] #b[1:3] == (2,3)

 

3. Update the tuples

We can not change any value in a tuple.

b = (1,2)
b[0] = 3 #this is wrong
#What we can do it's just
a = (3,4)
c = a+b
#result (3,4,1,2)

  

4. delete a tuples

Since wen can not change a value in a tuple. What we can do is to delete a whole tuples.

b = (1,2)
del b

  

5. Basic operation

b = (1,2)
len(b) #return the length of b
(1)*4 #(1,1,1,1)
1 in b #return Ture
for i in b:
	print(i)

  

6. Built-in function

cmp(tuples1, tuples2) #compare two tuples
len(tuples)
max(tuples1)
min(tuples1)
tuple(seq) #turn a seq into a tuple

 

posted @ 2017-01-17 10:21  KennyRom  阅读(232)  评论(0编辑  收藏  举报