Odin May | Google Colab Notebook
This is a short description for the post which introduces it
Alright, so with this post I think I just want to discuss one singular idea. Everything in python has a type and is an 'object'. ( don't overthink it ). Objects can have their own functions tied to them called methods. You know what, lets type this out and follow along. It will help you get the feel for writing code.
temp = "05/02/95 average temperature: 25"
# See how declaring the variable is very similar to how you read it
# Variable called temp, check!
# Equal to some text, check!
So this variable that we have called temp can be printed out, we know we can do that, but it's kind of boring.
print(temp)
And unsurprisingly it prints the text out to screen.
Our temp object is far from boring though, much like python itself including built-in functions, every object can also have built-in functions that operate on themselves. Sound confusing and exciting? Good! Let me just show you a few of them first then we will talk more about them.
print(temp.upper()) # Uppercases the text
print() # I'm including this blank print just to get a new line on output, very useful.
print(temp.title()) # Uppercases the first character of each word
print()
print(temp.split()) # This splits our text at whitespaces and returns a list
So from the above example, upper, title, and split are all methods. These are functions as well technically, they are generally called methods when they are owned by the object. They still have to be called with the ( ) because they are functions. We can also pass arguments to them as well. lets look at some other ones that use arguments.
print(temp.count("e")) # Count how many times a string is found in the object
print()
print(temp.split('/')) # We can split the string on the character we pass in
print()
print(temp.replace("/", "-")) # Replace takes 2 arguments. The first is the string to replace, the second is it's replacement
As you can see methods like replace can be very useful for data manipulation. Common tasks like renaming and modifying text are made easy with replace. Methods are very useful and also very convenient becuase they are tied to the object. If you run the type function on a variable and it says its a string then you can access string methods. There are also methods on other objects types like lists and dicts as well. I would bookmark this page, it shows all available string methods and has other resources like list methods in the left column.
Another trick you can do with the replace method is deleting things from the string by passing a blank string as the second argument. This is an example of that working.
print(temp.replace(" average", ""))
Chaining Methods Together
There is a lot you can do with methods. Let's look at it a little more closely though. All of the methods mentioned here except split return a string back. Since we have a string we can use string methods. This brings us to a technique called chaining. When you call the method it returns an object which can have methods called on it. Now that we know the concept, lets look at it. It may look more complicated than it is so just remember the concept.
print(temp.upper().count("e"))
# temp.upper() gives us "05/02/95 AVERAGE TEMPERATURE: 25" then we do .count("e")
# We get back 0 because all of the characters are uppercase there are no lower case "e"
Chaining methods is just performing the method on the result of the previous method. This can save you time and memory when coding because you don’t have to do any intermediary steps. The slow way of doing the same thing would be like below. You can see we are cutting out the intermediary variable by chaining methods right on to the expression.
upper_letter = temp.upper()
print(upper_letter.count("e"))
In Python most objects have methods you can use to operate on them, these were all string methods but lists also have methods, and they are equally as helpful. Lets look at a list method, now we could make a fake list to work on but if you remember when we called the split string method it returned a list. We will use that list for testing.
test_list = temp.split() # We will save the list as a variable for easier readability
print(test_list) # And now test_list is a list object, go ahead and type check it
print(type(test_list)) # Get used to doing this, you will do it a lot trust me.
test_list.append("New list item") # We can add an item to the end of the list
print(test_list) # This is the list, you can see its updated.
list_copy = test_list.copy() # We can add a copy of the list. Lets store the copy in list_copy so we can use it later
print(list_copy)
list_copy.clear() # We can clear all elements of the list
print(list_copy) # And now we have an empty list
print(test_list) # This is the original list, unmodified since we cleared only the copy
Methods are something that are fairly easy to understand and if you keep them in mind as a key part of coding you will find yourself using the to solve a lot of problems. I highly encourage you to look at all of the methods available to you just to know whats out there. From the W3Schools link you can access list methods and a bunch of other programming reference.
Now that you are more comfortable with methods lets look a more confusing looking example of chaining. If you evaluate it in order of chained methods it will make sense. Try reading it left to right and each function call () you see remember that it is returning you an object.
print(temp.upper().replace("TEMPERATURE", "DAILY HIGH").split())
# Okay lets break this down one at a time
# First we are asking python to print something for us
# Then we are calling the upper() method on our temp variable
# The result of that is a string, which we call replace() on immediately, swapping some text
# The result of the text swap has split() called on it immediately giving us a list back.
Wow that's getting a bit unreadable but it works because of chaining methods returning objects that we can chain on to. This is more to demonstrate that longer code can be scary but when you break it down, it is often simple. Even complex projects come down to simply dealing with objects of different type and being able to modify them. This is a key thing to understand and it is simpler than you would think.
Simply put methods are functions that objects include and can be called from them.
Congrats and keep studying, these tools are very powerful and very accessible! 😀