Demystifying the Mystical: My Foray into the World of AI

AI Saturdays Ilorin Week 3

Mubbysani
ai6-ilorin
6 min readJan 17, 2020

--

import this is the code one needs to enter to print the Zen of Python — a document that summarized the language’s core philosophy — and first among the aphorisms is the expression “Beautiful is better than ugly”. It turns out that Python does not only have instruction on beauty but also exemplifies magnificent beauty. This beauty cannot be unraveled unless taught by the best hands. In that quest, the AI Saturdays class resumes and we surge ahead, deep into the world conceived and developed by Guido van Rossum. Of course, we are being taught by the best hands!

The second week of AI Saturdays was characterized by the students meeting Anaconda, Python and traveling out of Mother Earth to Jupyter. Yes, Jupyter is essentially a notebook and embedded in its core is Python3. In the second week, some basic topics such as numbers, strings, printing, if, elif among others were taught, but the third week seemed new dawn, and with new dawn come new things, new lessons, and new phenomena in Python.

The instructor stepped ahead, reviewed the lessons from previous classes and mentioned the new lesson. “ Hello class, the first thing we are to learn today is Function”. Function? Could it be the same function as that of Mathematics: y = f(x)? Maybe not, maybe yes.

Functions

Python Function is multi-facets, ranging from user-defined functions to recursion functions and in between are built-in functions and Lambda functions. There are a number of built-in functions by Python, such as len(), sorted(), sum(), min(). These built-in functions apply on string, list, tuple, and others. We are much familiar with built-in functions(from week 2) and are complete strangers to user-defined functions. The strangeness will soon be usurped by the calm strength of explanation from the instructor. He went further. Defining a function in Python requires that the def keyword comes first before its name, then the name will be accompanied by parentheses before a colon(:). Take a look

Defining a function

Not only that, functions — as in user-defined functions — can also take in parameters that are placed in between the parentheses to provide the required functionality. Here is what I mean

Defining parameters

‘‘And there are a whole lot of other things that can be done with functions’’ said the Instructor as he went ahead to show us those things. Here I present them

more with functions

In the image above, the magic there is not as complicated as that done by Merlin, the keyword def was used to define the function with the name friendlist. The function was then given two parameters namely: name and age, then print was used to print a message using the two parameters. The asterisk that preceded age only instructed the machine to allow more than one argument from the age parameter. Simple, isn’t it?

And so the race continues, having explained the meaning of function and how it can be defined, the next on his list was class. Yes, class, as in…well, not that kind of class you are thinking, I was also ignorant of class until he explained.

Class

Class is defined by using the keyword class and it provides a means of bundling data and functionality together. In fact, Python programmers use classes to keep related things together. Python class is also known for its provision of standard features for object-oriented programming(more on this later). Meet class below

Class

A class in itself cannot be useful until it has functionalities which are put in place by setting attributes. In the image above, a class was defined and given the name Myclass. Myclass wouldn’t have had any use if there are no attributes in it. The variables name and age are the attributes that allowed the class to really have any functionality. The attributes inside a class can be assessed using the dot . operator.

Class attribute and modification

Class does exhibit modification even after it has been defined. The class Rubbishclass in the above image was assigned another name groupclass and the new name was used to assess the attribute that has the name functioninside(functioninside is indeed a user-defined function, that tells that a class can also have a function as one of its many attributes).

The journey doesn’t end with class as the never relenting instructor tried to introduce to us some errors that might come up during the course of writing a program and how the errors can be managed. Python is built in a way that the parser shows the offending line and displays information at the earliest point in the line where the error was detected, this is known as traceback.

Error

In the example above, a SyntaxError was displayed among the plethora of other errors that can be encountered. Even if a program is syntactically correct i.e doesn’t exhibit SyntaxError, another form of error can show up when an attempt is made to execute it, a programmer wouldn’t want his program to crash due to this and other types of error, hence the use of handling exceptions otherwise known as try and except.

try and except

Errors or exceptions are capable of donning different attires i.e they come in different types. The type that the program run into is printed whenever the program is executed. But the try and except clauses make it possible to write programs that take care of specified errors.

exceptions

If the statement under try is executed and there is no error encountered, the except part is neglected and the execution of the try statement is finished. However, in the example above, the program requires that the user input a number(the try part). It was required that the input be an integer, after the execution of the try statement, the user went ahead to input a string and instead of the program displaying an error message, it just shifted to the except statement and have it executed. A try statement may have more than one except statement to specify handlers and messages for different exceptions.

try statement with more than one except statement

What is OOP?

‘ Python is a multi-paradigm programming language. Object-oriented programming(OOP) and structured programming are fully supported…’

The above statement can be found on the Wikipedia page where features and philosophy of Python are discussed extensively, one cannot help but ask ‘What is object-oriented programming, otherwise known as OOP?’ I do not know until Adnan shed light on the rather abstruse concept, afterward, it was understood and the abstruseness diminished.

Object-oriented programming (OOP) is nothing but a piece of cake(not actually a piece of cake). It is a programming paradigm that provides a means of structuring programs so that properties are bundled into individual objects. It uses the idea of objects to represents data and methods, and also provides the opportunity for developers to create neat and reusable code… that really saves them from redundancy. OOP uses the concept of objects and classes. Class(discussed above) is a blueprint for the objects that define common attributes and traits.

--

--

Mubbysani
ai6-ilorin

I write anything that catches my fancy and play around with words