Python 3

home

Introduction to Python

davidbpython.com




Containers: Tuples and Sets

tuples and sets: like lists but different

It's helpful to contrast these containers and lists.



It's easy to remember how to use one of these containers by considering how they differ in behavior.





the tuple container object

A tuple is an immutable ordered sequence of values.


var2 = (1, 2, 3, 'a', 'b')   # initialize a tuple of values





subscripting a tuple

Subscripting allows us to read individual items from a tuple.


mytuple = (1, 2, 3, 'a', 'b')       # initialize a tuple of values

xx = mytuple[3]                     # 'a'

Note that indexing starts at 0, so index 1 is the 2nd item, index 2 is the 3rd item, etc.





slicing a tuple

Slicing a tuple returns a new tuple.


var2 = (1, 2, 3, 'a', 'b')             # initialize a tuple of values

subtuple1 = var2[0:3]                  # (1, 2, 3)

subtuple2 = var2[2:4]                  # (3, 'a')

subtuple3 = var2[3:]                   # ('a', 'b')

Remember the rules of slicing, same as lists and strings:





concatenating two tuples with +

Concatenation works in the same way as lists and strings.


var = ('a', 'b', 'c')
var2 = ('d', 'e', 'f')

var3 = var + var2                  # ('a', 'b', 'c', 'd', 'e', 'f')




"set" container object

A set is an unordered, unique collection of values.


Initialize a Set

myset = set()                  # initialize an empty set (note empty curly
                               # are reserved for dicts)

myset = {'a', 9999, 4.3, 'a'}  # initialize a set with elements

print(myset)                   # {9999, 4.3, 'a'}





adding an item to a set

myset = set()                  # initialize an empty set

myset.add(4.3)                 # note well method call not assigned
myset.add('a')

print(myset)                   # {'a', 4.3}    (order is not
                               #                necessarily maintained)




getting information about a set or tuple

# Get Length of a set or tuple (compare to len() of a list or string)
myset = {1, 2, 3, 'a', 'b'}

yy = len(myset)              # 5 (# of elements in myset)


# Test for membership in a set or tuple
mytuple = (1, 2, 3, 'a', 'b')

if 'b' in mytuple:                        # this is True for mytuple
    print("'b' can be found in mytuple")

print('b' in mytuple)                      # "True":  the 'in' operator
                                          # actually returns True or False





looping through a set or tuple

The 'for' loop allows us to traverse a set or tuple and work with each item.


mytuple = (1, 2, 3, 'a', 'b')            # could also be a set here

for var in mytuple:
    print(var)                            # prints 1, then 2, then 3,
                                         # then a, then b





summary functions: len(), sum(), max(), min()

Summary functions offer a speedy answer to basic analysis questions: how many? How much? Highest value? Lowest value?


Whether a set or tuple, these operations work in the same way.


mytuple = (1, 3, 5, 7, 9)       # initialize a tuple
myset =   {1, 3, 5, 7, 9}       # initialize a set

print(len(mytuple))              # 5  (count of items)
print(sum(myset))                # 25 (sum of values)
print(min(myset))                # 1 (smallest value)
print(max(mytuple))              # 9 (largest value)




sorting a set or tuple

Regardless of type, sorted() returns a list of sorted values.


mytuple = (4, 9, 1.2, -5, 200, 20)       # could also be a set here

smyl = sorted(mytuple)                   # [-5, 1.2, 4, 9, 20, 200]





[pr]