A tuple is a collecton of objects, very similar to a list in several ways: tuples are ordered, they can be indexed, iterated and sliced and they can hold other collections. They can accept variables but they “make deep copies”, i.e. Python tuples copy the VALUE of the variable, not the pointer to the value, so if that variable’s value changes, the value in the tuple does NOT.
But there are a couple of important differences also:
-
- Tuples are immutable – once created they can’t be changed, but they can contain lists which CAN be changed.
- The order of their items is fixed.
- If you add a single item, it must be followed by a comma, ex: testtup+=(“gopher”,)
The following code is meant to be run with Idle so that you can follow the code and see the resulting print-out at the same time. There should be an example of just about everything you can do with a tuple somewhere in the following. Some of this applies only to 3.4 and above.
print('''\nThe intentional design of these examples is for the user to run\nand print out the result using Idle so that the code can be \ncompared step-by-step with the result.\n''') myvar1="gorilla" alist=["cat","elephant","fish","ox","zebra"] testtup=("gnu","anteater","bear",myvar1,"cat","catapiller") print("1 ",testtup) #demo: deepcopy does not change value in tuple myvar="zebra" print("2 ",testtup) #adding a single item testtup+=("goat",) # note required comma print("3 ",testtup) #sorting, in place, forward or backward print("4 ",sorted(testtup)) print("5 ",sorted(testtup, reverse=True)) #boolean constant, not string #this is not the same as the list.sort() command and it #does not change the natural order or the tuple #Create a tuple from a list testtup2=tuple(alist) print("tuple from a list: ",testtup2) #join and sort two tuples testtup = sorted(testtup + testtup2) print('Two tuples joined, sorted, reassigned: ') print(testtup) print("Total number of elements in a tuple; testtup has ",len(testtup)) print("number of times 'cat' appears: ",testtup.count("cat")) print("clear a tuple with empty reassignment; T=():") testtup2=() print("testtup2 is now ",testtup2) del testtup2 print('delete it with del T .... done') #now trying to use testtup2 should be an error print("testing for error when we try to use testtup2") try: print(testtup2) except: print("'is not defined' error occurred") else: print("no error occured") #find a member in answer= lambda animal: "yes" if animal in testtup else "no" print("Is 'goat' in testtup? " + answer("goat")) #min and max print("min and max can be used a couple of different ways") print('applied to the testtup tuple...') print("min testtup: ", min(testtup)) print("max testtup: ", max(testtup)) testtup3=(54,3,72,21,16,99,5,2) print('applied to a tuple of numbers: '+ str(testtup3)) print("min testtup: ", min(testtup3)) print("max testtup: ", max(testtup3)) #min and max can take a key=function parameter print(max(testtup, key=len)) print(min(testtup3, key=lambda x: 1000 if x%2==0 else x)) # above, find the smallest odd number in the list #find the index of a known item print("find the index for known item 'cat' in testtup:") print(testtup) myitem = 'cat' i=testtup.index(myitem) print('index for '+myitem+' is '+str(i))# starts with 0! j=testtup.index(myitem,i+1) print('next occurance is '+ str(j)) print('find 5th item with a slice') myitem = testtup[4:5] #start with the item you want -1, end next print(myitem) # because the first item is zero 0 #easy way to get a reversed tuple print("Easy reversed tuple") print(testtup[::-1])