Learn Python With Dhawal -9- Chapter


This chapter is going to be more deep in theory than something to do practically yourself. If you are someone who already has a good understand of OOP and SOP concepts then you can surely skip this chapter or just give it a read incase you want to revise. In the world of programming languages there are two types of languages mainly which are classified based on the type of approach they have towards the style in which a particular code is been written and the surrounding logic is built.

A programming language at the end of the day is a language and each language has its own way of writing and reading certain things. We have seen what python has in its grammar vocabulary for the most basics. Python at times also is regarded as a scripting language as it is used in form of Django in creation of websites and popular web apps. Out of all the languages python is most easiest at the same time versatile as it gives you minimum amount of programming constraints to be dealt with. Alongside a con of python is that it is indeed an Higher level aka an advanced level language for which you ought to know basics of coding and how to go around it.

Hence even if it looks super cool and simple to write the code in python but as you go ahead the complexities make it worse for the beginners as according to me python isn't an ideal language to be taught if you are learning to code for the first time. Python is made while keeping in mind the flaws of the original and older programming languages in mind and countering them.

It surely makes it easier for professionals to write huge codes without worrying about a stupid bracket here or a silly semicolon there but it is a huge headache when it comes to writing huge programs due to its over simplicity. Many of you might be aware that just like JAVA, we can also embed C++ and C# or at times even traditional C code depending upon the interpreter that you have been using in both JAVA and Python.

You will be having a question that how all of this is related to Object Oriented Programming Approach, right? Let me explain. JAVA, and Python are OOP Languages, C++ is partially OOP and C is totally SOP ( Structure Oriented Programming Language) also known as Procedural languages as they follow a particular procedure. For python you really don't need to bother about SOP but still to know how it is different I am explaining both SOP and OOP.

SOP  or Structure Oriented Programming Languages like C follow a structural way of writing a code and forming a syntax, for ex :
void main()
    int a=5;
    int b=6;
    int c=a*b;
    printf("%d is the result",c);

Here as you can see, just like we have in our english language there are full stops to indicate where we are terminating a sentence or command in lingo of machines. A semicolon is used to indicate end of a command and whereas curly brackets are used to mark the start and end of a loop, program or basically any set of statements or code. When you are writing a program in this language you have to take care of its syntax and structure the code properly. Here indentation was just introduced to make code more legible and it didn't had any other use.

If you compare this to a program in python which gives the same output you'd understand what I am talking about.

def fun():
    print (c," is the result")

If you see the difference in the lines of code and complexity, you can clearly judge yourself which one looks much cleaner and simpler. Above in C code, if you forgot a semicolon your program won't compile, it apart from that if you had written 'char a' instead of 'int a' it would've shown you error here python takes all that miniscule and menial load away from the programer and assumes certain things which makes it easy for programmers to focus on bigger aspects and issues than dealing with tiny missing semicolons and braces.

Now as you know what and how things work, you'd be better able to understand how OOP works and what it actually is and what makes it so better or different over SOP.
You'd find the distinguishes and other points a lot around the web and I am not interested in doing the menial work but here I am to explain the concept as don't be judgy about the format as I am not writing answer in an exam :P.

In OOP main concept is an object, everything is around that object. You can create, assign as well as delete objects. Now what is an object and how it makes things easier and better? An Object is nothing but an instance of a Class. Now, what is a class?

A class is a file in layman terms in which you have written your program. Generally you just write one class in one file to keep it simple. Think of it as a single class and name of your class is name of your program.

You can try this if you want but not needed.
Since we had been dealing with jupyter notebook till date, we had not been worrying about class or programs and everything else. But since we are moving ahead on the concept of class and building multiple class programs ahead, we have to look into how an actual program is written and executed without crutches of IDE or any fancy software.

If you have python installed in your system, you can open the terminal or command prompt or whatever you call in your respective operating systems.  Then you need to open notepad or any similar program ( I recommend Notepad++ as its open source and much fun to use for simpler things) and write down a simple basic python code and then save it as .py extension.

Now once you are done with that, open terminal and head into the directory where that piece of code is located, for ease you can just keep that code on your desktop and right click to open terminal there on windows.

You need to locate your directory by using command 'cd/Desktop' keep in mind desktop d can be large or small so google up on how to locate it in the terminal. Later just in terminal type 'python' followed by the filename.py and let the code run. You would realise there was no difference in it when you tried the code inside IDE and when you tried it like this. It is because IDEs and other such software make it easier to write code and focus less on miniscule things like this.
It ends here.

So, now since you have a much better understanding of class and what it is let me explain what an object is and what it's instance means. In SOP whenever you had to bring in functionality that was written in some another code file aka class, you had to either copy the whole thing or just write it again in a way that it doesn't hamper your next code. As time passed there arose many work arounds for this like the import which just eased the work on the programmer's part since you didn't have to copy it manually but the machine behind the scenes did the same thing for you.

So now to end this stupidity as where we used loops and functions to avoid redundancy of code and make it faster here we had issues with redundancy again. Imagine you had to write a program for calculator and each of the functions like addition, subtraction etc are in their own classes. Now if you wanted to have a button for percentage or compound interest, you had to copy all that code again in those respective classes or basically import.

In OOP approach, an object is created, now object is nothing but an instance of class. Here, instead of copying everything we just point at a particular thing we need and then its either copied or just linked up depending on what language are you using. Think of it as a direction pointer on a map or bookmark on a book.

So when you are coding next time and a machine is executing, you get rid of that redundancy. Again there are various redundancies based on compilers and interpreters but that's another whole subject and huge topic of its own. Here we dealt with the redundancies from programmer's end with OOP.

Note :
Although OOP is better, it's not all pro and it has some of its cons as well. Here I am linking some distinguish points about OOP. Now you can read more about OOP Approach here. You can totally skip it if you don't want to read as it won't much matter but you need to know some concepts and are to be kept in mind while writing the code such as encapsulation, inheritance and polymorphism.

You can totally skip this if you feel confused  or don't understand it now but I'd recommend to just go over it and give it a read as it might help to you understand it much better when I'd explain it while working on a code.

Here are some basic concepts of OOP. (Source webopedia )

  1. Abstraction: The process of picking out (abstracting) common features of objects and procedures.
  2. Class: A category of objects. The class defines all the common properties of the different objects that belong to it.
  3. Encapsulation: The process of combining elements to create a new entity. A procedure is a type of encapsulation because it combines a series of computer instructions.
  4. Information hiding: The process of hiding details of an object or function. Information hiding is a powerful programming technique because it reduces complexity.
  5. Inheritance: a feature that represents the "is a" relationship between different classes.
  6. Interface: the languages and codes that the applications use to communicate with each other and with the hardware.
  7. Messaging: Message passing is a form of communication used in parallel programming and object-oriented programming.
  8. Object: a self-contained entity that consists of both data and procedures to manipulate the data.
  9. Polymorphism: A programming language's ability to process objects differently depending on their data type or class.
  10. Procedure: a section of a program that performs a specific task.

In Next chapter we will learn about importing libraries and some other basic inbuilt fancy functionalities of python and its libraries.


This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.


Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.


Post a Comment

Do comment and share your thoughts.