Advanced Data Types

There are a number of advanced data types native to Python. These include

Tuples

Tuples are an ordered, immutable, list of objects. You cannot change the order of a tuple; neither can you add, or remove elements. Tuples, therefore, are particularly useful when you want to pass around sets of things whose ordering doesn’t change, like sets of x-y-z, coordinates. Because they are static and immutable, tuples don’t carry the same memory overhead as lists.

tuples are defined using parentheses, as below.

tup1 = (0,1)
tup1
(0, 1)
tup2 = (3,-2)
tup2
(3, -2)
tup3 = tuple(range(5))
tup3
(0, 1, 2, 3, 4)

tuple of tuples

tup4 = (tup2,tup1)
tup4
((3, -2), (0, 1))

you can use the sorted method to create a sorted list of tuples

sorted(tup4)
[(0, 1), (3, -2)]

you can also pick how you sort based on a lambda method

sorted(tup4,key=lambda x:x[1])
[(3, -2), (0, 1)]

accessing tuples

tup4[0]
(3, -2)
tup4[0][1]
-2

Lists

Lists are mutable, ordered, sets of objects. You can add and remove elements from lists, which makes them handy for holding objects whose elements change, even if the list stays the same.

Lists are defined using square brackets, as below:

list1 = [-1,1,2,3,4]
list1
[-1, 1, 2, 3, 4]

List classes contain many more methods than tuples for organizing and sorting the elements. Some can be seen below.

list1.append(5)
list1
[-1, 1, 2, 3, 4, 5]
list1.extend([6,7,8,9,0b101101,11.0])
list1
[-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 45, 11.0]
result = list1.pop(0)
list1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 45, 11.0]
list1.insert(0,14)
list1
[14, 1, 2, 3, 4, 5, 6, 7, 8, 9, 45, 11.0]
ii = list1.count(1)
ii
1
ii=list1.index(7)
ii
7
list1.remove(7)
list1
[14, 1, 2, 3, 4, 5, 6, 8, 9, 45, 11.0]
sorted(list1)
[1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]
list1
[14, 1, 2, 3, 4, 5, 6, 8, 9, 45, 11.0]
list1.sort()
list1
[1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]
#list1.clear()

Indexing and Slicing

Like tuples, you can select one or more elements from a list; the result of indexing a single element is the object itself. The result of indexing multiple elements from a list is another list

list1
[1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]

You can select one element by indicating its number in the list in order to select it. Of note is that Python uses zero-based indexing, which means that the first element of a list is accessed with

list1[0]
1

To select a range of elements (called slicing), you can provide two nubmers, separated by a colon. Because of Python’s zero-based indexing, using [2:4] selects, for example, a range of elements starting with the third element, up to (but not including) the fifth element.

list1[2:4]
[3, 4]

Omitting the first element indicates you start at the beginning of the list

list1[:4]
[1, 2, 3, 4]

Omitting the second element indicates you go to the end of the list.

list1[4:]
[5, 6, 8, 9, 11.0, 14, 45]

Adding an optional third elements indicates the “step-size”. [::2], for example, means starting at the end of a list, going to the end, but selecting one of every two elements.

list1[::2]
[1, 3, 5, 8, 11.0, 45]

Negative numbers indicate an element’s position from the end of the list. To slice all elements in reverse order, use

list1[-1::-1]
[45, 14, 11.0, 9, 8, 6, 5, 4, 3, 2, 1]
#list1.clear()

understanding types

isinstance(list1,list)
True
list1
[1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]

Dicts

A dict or dictionary, is an unorderd set of key:value pairs. The key is unique, and can be used to obtain the value within a dictionary. Dictionaries are useful when values are stored in an unordered fashion, but are accessible through a unique identifier.

dicts are defined using curly brackets. You can index into dictionaries using square brackets, just like lists or tuples.

dict1 = {}
dict1['key1']=123.45
dict1['coordinate 1'] = tup1
dict1['coordinate 2'] = tup2
dict1['all_coordinates'] = list1
dict1
{'key1': 123.45,
 'coordinate 1': (0, 1),
 'coordinate 2': (3, -2),
 'all_coordinates': [1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]}
dict1['coordinate 1']
(0, 1)

Methods

You can obtain all key:value pairs using the items() method

dict1.items()
dict_items([('key1', 123.45), ('coordinate 1', (0, 1)), ('coordinate 2', (3, -2)), ('all_coordinates', [1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45])])

you can also obtain all the keys within a dictionary,

dict1.keys()
dict_keys(['key1', 'coordinate 1', 'coordinate 2', 'all_coordinates'])

or all the values

dict1.values()
dict_values([123.45, (0, 1), (3, -2), [1, 2, 3, 4, 5, 6, 8, 9, 11.0, 14, 45]])

Sets

A set is an unordered collection of unique values. You can use sets to perform set math on collections of things in Python. Sets also use curly brackets in their definition.

a = {3,2,1}
print(a)
print(type(a))
{1, 2, 3}
<class 'set'>
b={2,3,4,3,2,2,3,2,4,2,3,4}
b
{2, 3, 4}

You can operate on sets using their methods (union, intersection, difference), or with shortcut operators, like & (and/intersection) and |(or/union).

a&b
{2, 3}
a|b
{1, 2, 3, 4}

Strings & Formatting

Strings are ordered collections of characters, with many many methods. See the official python documentation for more details. One of my favorite methods, though, is the format() method. This permits you to mix variables containing complex string or numerical data into a string at specficied locations. See the example below

float1 = 12.345
'the value of the first element is {0:07.1f}, as an int: {1:07d}'.format(float1,int(float1))
'the value of the first element is 00012.3, as an int: 0000012'