Week 3 Assignment Oversimplified

Week 4: AI6 Ilorin

The Scenic Route
ai6-ilorin
7 min readJan 30, 2020

--

The fourth week of ML class was centered on rounding up the fundamentals of Python. Understandably, the instructors wanted to move on from the basics, to begin more data-related problems with us. Adnan Haddy before introducing Numpy and Pandas, (python libraries for data science) began with the solution for Assignment 3 , a beautifully organized puzzle even I dare dreamed of deciphering, in time. Like some, I had been half-baked when it came to the topic of “classes”. Sure I’d written a piece on classes and functions , but I knew deep down there had been a lot of overlooking, hence my decision to oversimplify the Wedding assignment.

Before the Wedding question, I feel the need to reiterate on classes and functions.

Recap

A class is a more flexible ‘data type’ that allows you to define the attributes and behaviors of whatever you want to build with your program.

From the above definition of a class, an attribute is a variable, while a behavior is a function. Functions under a class are also referred to as Methods.

Example

After surfing the Internet for days, searching for the one website that would explain Classes appropriately for me, I settled on introtopython.org. I followed their example step by step; a virtual rocket from a video game. We are going to take a closer look at the Rocket class, with x and y coordinates as its attributes.

In the above image, the attributes are present in the parentheses of a function. The ‘self’ attribute that always comes first and the keyword arguments(x=0, y=0, x_increment=0, y_increment=0), are all attributes of the class called Rocket! Attributes do not have to be keyword arguments(having assignment operators (=) and default values after their names) they could be positional as well. Check this out.

The behavior on the other hand refers to the method (function) aside the special function(__init__). Behaviors are actions defined in the class. In the above image, def move_up is a method or behavior that quite literally defines the upward movement of the Rocket simulation. It modifies the y-coordinate from zero to its increment(+=).

What in the world is def__init__?

def __init__ is a special function that is automatically called when an object has been created from your class. It initializes (how it got its name, I presume) the given attributes of the class and sets them to their proper values. The self attribute always comes first and lets you refer to certain attributes from any other part of the class (self.name, self.age,self.donation etc)

To use a class in Python, you need to make an object under the class. This is where “object-oriented” comes from. An object, according to that blessed website is an instance of a class that has a certain set of values for all the variables in a class.

To create an object in Python, all you need to do is create a variable, then assign that variable to the name of the class with a set of parentheses.

my_rocket is the variable that has been assigned to the name of the class with parentheses. We have our object!

Playing around with classes

In the above image, instead of directly creating the object and just printing for an output. A list (a fleet of rockets) was created and each of them are identified by their indices. An additional knowledge of string formatting, for loops, and even list comprehension is needed to understand the different ways this fleet of rockets can be programmed. The important thing is to know the fastest and cleanest method.

Now after sharing what I know of Methods and objects under classes, the assignment should be a walk in the park…

The Red Wedding

We were to build an application that collects information from people who are to attend a wedding. Attributes with their corresponding data types were given; name, age, sex, donation, message, cwith(number of people that are coming with them)

First thing to do is to create the class and initialize its positional attributes.

Next up is to define functions for each of these parameters or attributes in the above special function. The goal is to enable users enter their details by your design.

The above is a function that allows the user enter their name using the input keyword. F-String formatting is employed for the printing method.

For every attribute in the __init__ function, a function is to be defined , to create an interface for a user to input values.

Next up is the Age Attribute:

Here, a condition arises, hence the while loop and ‘if’ statement. If the value entered by the user is not an integer data type(int), Python will print #Error. That’s the whole point of this code.

Consider the Sex Argument:

Here, there are two conditions to be met; the data type should be a string and the value MUST be ‘M’ or ‘F’, else Python prints an error message.

The remaining attributes such as donation, cwith and message are defined accordingly , depending on their data type. The message attribute will be just as the name attribute because they are both strings.

The last method and behaviour defined is to get the details or info of any one guest.

Before the method can work, the class needs an object. The object’s method is accessed by adding a dot notation(.) between the objectname and the methodname.

The ‘guest’ will be our object just like ‘my_rocket’ above. Yet, instead of leaving its parentheses empty, we give it arguments. In the above example of my_rocket, to the best of my knowledge, the class already has default values from its keyword arguments, that’s why it could have been left empty. Here however, Python requires you to fill in the positional ‘parameters’ or their arguments. Without filling in appropriately , nothing would work.

After filling in the parentheses, press Enter to access the object’s method.

Now, this is just one guest whose info has been organically inputed by you the programmer. The goal is to allow users input their info themselves.

To achieve this, a new object is integrated into the class (a class can have as many objects as required). I call it guest_id. And because the goal is to create a form of sorts for the user, an empty list is created and filled with a while loop. And I imagine that is just one of many ways to do it.

One by one, each attribute is integrated into the form with their peculiar conditions that must be met. It is also possible to group more than one argument under the umbrella of one ‘try:’ to save time.

In the end, another object(more) is appended to our guest_id object. It is useful for collecting fresh set of values from a new user. Got a lot of conditions too.

Shift + Enter:

Finally, a for loop iterated the 3 guests’ information properly. I could only capture 2, but they are really 3, according to the len keyword.

For those like me who needed to go over this, I hope it makes a little more sense than it did before.

--

--