Objects, Lists, Strings


Most programming languages in common use now are object-oriented.

Numbers (integers, floats, etc) are pretty basic things.

Strings are a bit more complicated that numbers since a string might be comprised of 0, 1000, or any number of characters.

Similarly a list is a more complicated thing than a number.

It is helpful to think of more complicated things, like lists, strings, files, and many more things, as objects.

With object-oriented programming we think of the things that our variables represent as objects.

We can pass objects to functions and have the function use the object, such as search a list object.

With object-orientation there is another way.

With object-orientation an operation is done "on" an object to produce a result or side effect.

When an operation is done on an object is may cause a side effect or produce a result.

Functions that are done "on" objects are often called methods.

There are many string methods and some useful list methods.

List Method Examples

numbers = [2, 3, 6, 2, 5, 2, 9]
print('Initial list:', numbers)

# Result example
count_of_twos = numbers.count(2)
print('There are', count_of_twos, 'twos.')

# Side effect examples
# Put a new number at the end
print('After append 999:', numbers)

# Put item 888 in the list at index 1
numbers.insert(1, 888)
print('After insert 888, 4:', numbers)

# Remove last item
print('After pop:', numbers)

# Remove i-th item
print('After pop 3:', numbers)

# Remove a value
print('After remove 2:', numbers)

# Sorts the list "in-place"
print('After sort:', numbers)

# Reverses the items of s "in place"
print('After reverse:', numbers)

String Method Examples

The string type has many methods for processing strings.

See the string set of notes and examples.