Python 3 OOP Program to Add Two Numbers Using Classes & Objects in Terminal Tutorial For Beginners
Jun 1, 2025
Get the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video i will show you how to
0:05
add two numbers inside python using
0:08
object- oriented programming using
0:10
classes and objects so actually this is
0:12
actually a python program i
0:16
uh written right here if
0:18
i just run this program python app py
0:23
you will see it will actually print out
0:25
the sum which is 25 and 15 so these are
0:29
the two numbers we are passing in this
0:31
program and the sum is printed out which
0:34
is 40 so i will show you step by step
0:36
how to write this program this is ask
0:39
inside interviews in competitive
0:41
programming as well so just create a
0:44
simple app py file
0:47
and the classes we actually declare it
0:50
using this keyword class and here
0:53
followed by the name of the class so
0:56
like this if you see we declare this
1:00
class keyword this is the name of the
1:02
class and inside this you actually
1:05
define every class contain this init
1:09
function so this init function
1:12
specify actually you can say it's a
1:15
constructor so whenever in java or
1:19
javascript it's called as constructor
1:21
but in python it's called
1:26
as_init_ so we are passing these initial
1:29
parameters right here which is self and
1:31
these two parameters a comma b so now we
1:35
just need to in initialize it the value
1:37
of self dot a and whatever the value is
1:41
passed we will just equal to like
1:44
this so this is your constructor so this
1:48
constructor function will automatically
1:50
be called whenever you make the object
1:52
of this class
1:54
adder so now if i make an object right
1:58
here if you
2:00
see of this class i will simply say
2:04
instantiate a new object of this class
2:07
and here we just need to provide two two
2:10
variables that we need to add so 10 20
2:14
so here essentially what we are doing
2:16
this we have declared a python class and
2:18
here we are making an
2:20
object and uh we are passing these two
2:23
values which is 10 and 20 like this 10
2:27
and 20 so this will be initialized right
2:30
here and after that
2:33
uh after we make an object we now need
2:36
to declare a simple function inside this
2:39
class which will be responsible for
2:42
adding these numbers so you can actually
2:44
define a function right here again with
2:46
the defaf keyword let me say
2:49
add and now to access these two
2:52
variables which are there inside this
2:55
self variable that you
2:58
see so we have initialized this self
3:01
variable now to access it we do need to
3:03
pass this reference of self and now we
3:06
can easily
3:07
access so we can simply say self do a
3:11
plus self do.b b so we are just doing
3:15
the addition of these two numbers that
3:17
is passed here 10 20 so we are
3:20
referencing it so this is a function
3:22
inside the class so using this self
3:24
variable we are accessing a and b and
3:28
then we are doing this addition and
3:30
returning to this
3:33
function so now this is a object now to
3:38
actually call this function we will
3:40
simply
3:42
say we will call this function by using
3:45
this object so this contains this
3:48
function right here you can see it is
3:50
appearing it in the drop-down so this is
3:52
essentially this function we call this
3:54
function parenthesis that's all if you
3:57
do this now you will see that sum will
3:59
be printed out which is sum is equal to
4:02
30 you can see 10 + 20 comes out to be
4:06
30 so in this easy way you can define a
4:09
series of functions let's suppose i
4:12
write another function which will be
4:13
responsible for subtracting two numbers
4:17
so this will actually return the minus
4:20
so what we can do we can write another
4:22
thing which is
4:25
subtract and here we can call adder dots
4:29
subtract so this this will essentially
4:32
subtract these two numbers so the result
4:35
comes out to be minus 10 so this is the
4:38
logic how which we can declare a program
4:41
python program using classes and objects
4:44
you declare a class right here it has a
4:48
constructor or initialization function
4:50
and then you can write a series of
4:52
functions which will be responsible for
4:55
certain things then we initialize this
4:57
class makes make an object and then we
4:59
call the series of methods like this so
5:02
this is the way by which you can
5:04
initialize
5:06
it thank you very much for watching this
5:09
video guys and also check out my website
5:11
freemediatetools.com
5:13
uh which contains thousands of tools
#Programming
#Scripting Languages
