Introducing containers in Python

Download original file: 3_python_strings_as_containers.ipynb

View original file in nbviewer: 3_python_strings_as_containers.ipynb

Python containers

Python containers collect items together in one single collection that can be stored using a single variable name.

Python containers hold fundamental Python types. One exception are strings which are both a fundamental type and a Python collection.

Strings

Strings can be made by enclosing text with either single or double quotation characters (‘) or (“).

Stan_Ulam_said = "Whatever is worth saying can be stated in fifty words or less."
I_say = 'Most ideas can be expressed in fifty Python statements or less.'

Line breaks

Line breaks can be added to a Python string by using a line feed control character (\n).

John_Tukey_said = 'An approximate answer to the right problem is worth a good deal\n more than an exact answer to an approximate problem.'

Multiline strings

Line breaks can also be added using multiline strings. These are made by starting and ending with three quotation characters (''' text ''') or (""" text """).

John_Tukey_said = '''

An approximate answer to the right problem is worth a good deal 
more than an exact answer to an approximate problem.

'''

Triple quotes can be used to make strings that contain quote characters:

Even_though_the_student_knew_the_problem_was_ill_poised = ''',

the professor strongly advised:

"Do the calculation!".'''

Using Containers in Python

Containers allow us to perform useful operations on collections of types.

Using the word in to test is something is inside a container.

True if an item is contained in a container object, otherwise False.

'fifty' in Stan_Ulam_said

The not keyword can be used to test the opposite (The substring ‘50’ is not in this str object):

'50' not in Stan_Ulam_said

Elements of list-like containers (like strings) can be accessed by index

(starting at 0):

I_say[0]

Multiple elements can be accessed at once by slicing:

I_say[0:4]

Leaving out one of these indicies means to the beginning or to the end:

I_say[:4]


I_say[5:]

Negative index values mean number of index values from the end

An index of -1 means the last item in the list-like

I_say[-1]

Slicing also works with negative indicies:

The following means take the slice starting five index values from the end (-5) and slice to the end (:)

I_say[-5:]

You can also skip elements in regular steps when slicing

In the following cases we slice in steps of 2.

I_say[5:16:2]


I_say[::2]

The number of items in a container is its length:

len(I_say)

This is the same as calling the hidden function inside containers __len__, but this is rarely used directly.

I_say.__len__()

The smallest and largest items in a container are found using min and

max

However, this is not often useful for strings.

min(I_say), max(I_say)

Use the .index function to find the index of the first occurance of

something in a container:

I_say.index('fifty')


Stan_Ulam_said.index('fifty')

This proves that I said fifty first…

Use the .count function to find the number of times an object occurs in a

container:

John_Tukey_said.count('approximate')

Exercise: Try to use all of the properties and functions contained in string

types.

Hint: These are accessed using the dot notation. Try typing a variable name like I_say followed by a dot, and then hitting the Tab key. (i.e. I_say. followed by one or more Tab keystrokes). Try using the build-in Python help function. (i.e. help(I_say))