Hello, I am back with another article. This article is delayed due to my exams and project vivas as you know that I am a final year student of B.Tech Computer Science so this article is delayed now our winter holidays are started and I don't want that my holidays to be found useless without learning anything new so I continue learning python again on the same pace as I am learning before the exams. In my previous article, I talked about the Lists data structure in python. You can read that article in which I talked about Lists and List indexing, slicing, finding the length and reversing of list and some useful function applied on lists. As I talked about lists in my previous article & I talked about an exciting field of computer vision which is a part or we can say the subset of the field of Machine Learning. As I am learning python right now after the module of python I am going to learn Maths for Artificial Intelligence and Machine Learning in which I am going to learn the mathematical fundamental and logics and formulas which are necessary for Artificial Intelligence and then later on the Machine Learning and Artificial Intelligence and Computer Vision. In my project viva, I presented an idea of object detection using computer vision which is I am going to learn in the module of Computer Vision in which the computer program or algorithm is trained by using the different images of an object for detecting the object in front the camera and spits out an output and tell that what is in front of the camera. Before that, an image is a multidimensional array of random numbers we give eyes to a computer but we forget to give vision to computers. So, with the help of computer vision, we are going to give vision to the eyes of the computers.
So, In my today's article, I am going to talk about the classes and objects. So, the common saying is that the word "class" came from the word "classification" which means to classify and a single object is known as an example of that class. For example, we are all humans and we have all the features of a human so our class is human and the different people around the globe every person belongs to human class then each person is considered as an object of the human class. So, we can say that a class is a blueprint and of an object and an object is an example of a class.
To create a class in Python we use class keyword. For Example:
After class keyword the first letter of name of class wriiten in uppercase as illustrated above
We can create functions in a class. For Example:
class Add: a=int(input("Enter First Number: ")) b=int(input("Enter Second Number: ")) def add(x,y): S=x+y print("The Sum Is: ",S) add(a,b)
The Output of following program is:
Enter First Number: 5 Enter Second Number: 6 The Sum Is: 11
In the following program I created a class named Add in which there are two variables a and b whose input is entered by the user and a function add() in which the sum of x and y is stored in S and the parameters are x and y and on time of call the arguments are and b which are entered by user.
Another example of a class where some mathematical operations are going on:
class Calc: a=int(input("Enter First Number: ")) b=int(input("Enter Second Number: ")) def add(x1,y1): S=x1+y1 print("The Sum Is: ",S) def sub(x2,y2): D=x2-y2 print("The Difference Is: ",D) def pro(x3,y3): P=x3*y3 print("The Product Is: ",P) def div(x4,y4): d=x4/y4 print("The Quotiont Is: ",d) def exp(x5,y5): p=x5**y5 print("The Result Is: ",p) add(a,b) sub(a,b) pro(a,b) div(a,b) exp(a,b)
The output of following python program is:
Enter First Number: 10 Enter Second Number: 2 The Sum Is: 12 The Difference Is: 8 The Product Is: 20 The Quotiont Is: 5.0 The Result Is: 100
Here is the example of accessing the function using the class object and constructor:
class Record: def __init__(self, name, address): self.name = name self.address = address r1 = Record("Hrithik","Los Angeles") print(r1.name) print(r1.address)
The output of following program is:
Hrithik Los Angeles
Here the class name is Record and having a constructor in python the constructor is declared as def __init__(self, arg1, arg2,..........,argn) a single argument is necessery in constructor it can be anything rather then self this argument is like the this keyword in C++ language and r1 is a object of class Record by which we accessed the variables such as name and address.
This is all about my this article of learning python for Machine Learning and Artificial Intelligence as soon I learn different concepts of python I'll keep posting. So, there are more to go as I am learning.
Bye-Bye, See You in my next blog. Until then PEACE.