Search

# My Journey Of Learning Python For Artificial Intelligence by Hrithik Sharma

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:

```class Add:

```

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: "))

S=x+y
print("The Sum Is: ",S)

```

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: "))

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)

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:
self.name = name

r1 = Record("Hrithik","Los Angeles")
print(r1.name)

```

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.

See All