On Classes and Functions

Week 3: AI6 Ilorin

The Scenic Route
ai6-ilorin
4 min readJan 18, 2020

--

Minutes before arriving Malhub, I took some time to reflect on the body of work that had been done, all that had been achieved by me, relative to what I didn’t know before. It seemed trifling from a broader perspective, compared to what it’d have been if I’d started this journey 5 years ago. Even at that, I was grateful. Grateful I did not stop checking the right WhatsApp ‘statuses’, grateful I didn’t refuse to learn.

I walked into the beautiful compound with a confidence and hunger for more, gingerly parading my new laptop and bag.

The previous week was the basics ; lists, tuples, dictionaries, concatenations and else statements. It was, in a way, easier to follow. In my mind, I was ready for what the weekend had to offer.

Fast forward a few pleasantries later, the class began. And we were being introduced to new keywords — faithfully following the leader in hopes that the class would only get easier as we went….

Week 3 Recap

The class started with Iterations. An iteration is the number of times a loop can be executed. A Loop is a code that allows for an iteration. Python has 2 primitive loop commands : the While loop and the For loop

We began with the while loop, a control structure capable of running for eternity (infinity) and crashing your program or killing your kernel just because it can.

A While loop executes statements as long as a condition is true. The Break/Else/Continue statements are also essential in this discuss.

The Break statement stops the loop even while the condition is true. A Continue statement skips an iteration and moves on the next one in the loop. The Else statement can execute one more thing even when the loop has stopped or when it’s no longer true.

It’s safe to say one has to try it all on the Notebook to fully comprehend what it’s all about.

Functions

Next up was Functions. A Function is a block of code that runs only when it is called. This is what it looks like:

def My_function():

def’- represents the keyword used to define the function. It is present at all times. That’s how we know My_function is an actual function.

My_Function - this is simply the name of the function.

()- these (parentheses) house the parameters/arguments of said function.

A parameter is a variable in a function’s parentheses. An argument is the value inside the parentheses.

Remember how variables are containers that house, or get assigned some data values. Arguments are the values of Parameters.

Consider this,

def my_function(fname):
print(fname + “ Effron”)
my_function(“Zac”)

The output, if you run this code, becomes Zac Effron . This means fname is Zac. ‘fname’ represents the parameter name, Zac (the argument) on the other hand represents the true meaning of fname ; the value of fname.

Sometimes there might be more than one parameter (separated by commas) or there might be an unknown number of arguments. The computer understands they(the arguments ) are definitely more than one when you indicate with an * before the parameter name.

def friendlist(name, *age) ; this signifies that there will be tuples of ages.

Return Values let Functions return a value.

According to our instructor, Functions have the limitation of not being able to have objects under them, unlike classes.

And this brings us to…

Classes

Python is an object-oriented programming language. What this means is that almost all the code are implemented using a special construct called classes. A class is like a blueprint for creating objects.

Classes are used to relate things together.

A class is useless without functionalities; and functionalities are defined by setting attributes (these are containers).

Functions found under a class are known as Methods. So also, functions under an object are known as Methods.

The __init__ Function

This function, peculiar to classes, is used to assign values when an object is being created.

Class Person:

def __init__(self, name, age)

self.name = name

self.age = age

p1 = Person(“Lola”, 50)

print(p1.name)

print(p1.age)

(Run this code)

<where p1 is the object >

Note: A self parameter doesn’t have to be named “self”. It can be literally anything.

From then on was Constructors and Exceptions and then we reviewed, together with our second facilitator, the feedback he had garnered over the weeks. It was an impressive presentation of the backend that gave an average of everyone’s responses to record the efficacy of the AI Saturdays.

We came to the end of the class, and took pictures for the TL, some weary than others.

In truth, nothing is ever really tough. Just add water. And a dash of sheer grit and eagerness. Also be ready to laugh at your errors.

--

--