Tuple

There are three basic sequence types: lists, tuples, and range objects. We've learned about list and range function in previous lessons, let's talk about tuple.

A tuple is a sequence of elements that cannot be modified. It can contain any Python or more values. the items of a tuple are enclosed in parenthesis and separated by commas.

Some tuples: myTuple = ( 1,2,3 ) t2 = ('H', 'e', 'L') t3 = ('College drive', ['A10','B20','C10'], 98008, 'WA', )

tuple packing

t = 3, 6, 9       # Supprise? No error message
print(t)          # (3, 6, 9)
tp = 5, 8, 'Hi'  
print(tp)         # (5, 8, 'Hi')

The above statements like tp = 5, 8, 'Hi' is an example of tuple packing: the values 5,8,'Hi' are packed together in a tuple. You see, the output tuples are always enclosed in parentheses.

Sequence unpacking The reverse operation, sequence unpacking is also possible:

t = 3,6,9
x, y, z = t
print(x,y,z)    # 3,6,9

Remember in lesson (Variables and Values) we mentioned about variables' multiple assignments m, n, l = 2,4,6
Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.

x,y,z = 3,6,9 is the same as x,y,z = (3,6,9), (x,y,z) = (3,6,9) and (x,y,z) = 3,6,9

The construction of tuples containing 0 or 1 items:

empty_tuple = ( )
singleton = 'hello',    # <-- note trailing comma
print(singleton)        # ( 'hello', )   a tuple
print( len(empty_tuple) )       # returns the length of tuple, empty tuple length is 0

tur = (2,4,6,)     # some programmers prefer to include the trailing comma for all tuples

Compare tuple and list, Different: tuple is immutable while list is mutable; Same:

  • Just like a list, the elements of a tuple have a defined order. Elements of a tuple is t[0], t[1]...and so on.

  • Negative indices count from the end of the tuple.

  • [start:stop:step], slicing works too. Slicing doesn't change the original tuple, it just create a new tuple.

  • tuple.index(item) returns the index number of an item in a tuple

  • max(tuple), min(tuple) returns the max or min value in a tuple,

>>> t = (1,2,3,4,5,6,7,8)
>>> t[2]
3
>>> t[-2]
7
>>> t[3:6]
(4,5,6)
>>> t[-2:-6:-1]
(7,6,5,4) 
>>> t.index( 5 )     # 4, the index of 5 in t
>>> max(t)      # 8, max value in t

Use tuple in CodeCraft

Use a tuple to represent a point in CodeCraft 3D world, (x,y,z)

It's convinent to use tuple of three numbers to indicate a point in 3D coordinates,

p1 = (3, 5, -10)
p2 = (1, 10, -15)
p3 = (-4, 3, -12)

# build blocks using these tuples
block( p1[0], p1[1], p1[2], 'brick')
block( p2[0], p2[1], p2[2], 'obsidian')
block( p3[0], p3[1], p3[2], 'cobblestone')

Loop through a tuple

Similar to list, we can use for loop to iterate through a tuple:

t = (1,4,7,3,9,2)
for item in t:
    print(item)     # 1,4,7,3,9

# see in the CodeCraft world, build some columns

for item in t:
    column( t.index(item) , -20, item, 'brick')  

# highlight the max value in tuple
max = max(t)    
column( t.index(max), -20, max, 'obsidian')

List of tuples

In CodeCraft World, since tuples(x,y,z) can represent locations, a list of tuples would represent various locations. lst = [ t1, t2, t3 ] t1 = (x1, y1, z1) t2 = (x2, y2, z2) t3 = (x3, y3, z3)

lst[listIndex][tupleIndex]

The list represents three points t1, t2, t3, and point t1 has coordinates: (x1,y1,z1)...

lst[1][2] indicates: t2, z2 ( z value of the second point)

>>> P= [(1,1,-5), (4,5,-7), (8,3,-10)]
>>> P[1][2]     # -7 (z of the second point)

>>> P[0][1]     # 1 (y of the first point)

===

(extra? in Ch.5, complex structures)

(use list of tuples, ) List of tuples representing some selected location coordinates (x,y,z)

4-01-2 Some blocks in the air

# 4-01-2
# us a tuple list to store the locations (x,y,z)
lst = [(2,8,-10),(5,15,-15),(12,3,-20),(16,10, -10)]

# put a bar_z at the first tuple indicated location (2,8,-10)
bar_z(lst[0][0], lst[0][1], lst[0][2], 5, 'dirt')

for i in lst:
    block(i[0],i[1],i[2],10,'quartz')

We can use list of tuples to store some location coordinates (x,y,z), then build some structures at these selected locations, the above app just put some blocks.

results matching ""

    No results matching ""