Python 3 Program to Add Two Numbers Using Command Line Arguments in Terminal Tutorial For Beginners
151 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you how to add
0:04
uh two numbers uh using command line
0:07
arguments inside Python we can directly
0:10
pass the numbers in the command line
0:13
instead of hard coding it inside the
0:16
Python program so for this we use system
0:19
module which is a built-in module inside
0:21
Python so if you want to pass some
0:23
command line arguments you use this
0:26
package and uh now to receive these
0:30
arguments use
0:31
this property here which is system
0:37
argv and by default the first argument
0:40
is your actual Python script so we can't
0:42
write zero here so this will be the
0:45
second argument which will be the first
0:46
number of the addition which will be
0:49
passed similarly we will be receiving
0:52
the second number
0:54
so again we'll be using
0:56
argb and two so this will be the first
1:00
number second number and then we can
1:02
simply print out the
1:07
result like this so x +
1:11
y so this is a very simple program now
1:15
we simply are passing these two
1:18
arguments
1:20
so if I try to run this script
1:23
here without passing anything if I
1:26
directly write python app py you will
1:28
see it will give me this error that you
1:31
haven't passed this argument so what we
1:34
need to do we need to pass two numbers
1:37
here 10 20 so now if you see it is
1:42
saying it is not essentially
1:47
uh add adding these two numbers it is
1:50
just concatenating it so 10 20 so this
1:53
we don't want we need to increment this
1:56
for doing this we need to first of all
1:58
change this to an integer we need to
2:00
type cast
2:02
it so in this way by default everything
2:06
is a string so we need to change this to
2:08
a number by type casting it so this now
2:11
has become integer so if you do this now
2:16
you'll
2:17
see this is again a string here so we
2:21
need to again type cast this number to a
2:24
string so we can do this now you will
2:28
see the sum is equal to 30
2:31
so type casting is very important
2:33
feature whenever you want to convert
2:36
your string to integer we use this
2:38
function which is int and then whenever
2:42
we want to convert your integer to
2:44
string we use this function string we
2:46
also have this as well float this is for
2:50
use for floating
2:52
numbers so this is a program which
2:55
actually you can pass dynamic command
2:58
line arguments
3:00
so so 15 + 20 comes out to be 35 so you
3:05
can do this for addition multiplication
3:07
division anything the logic will remain
3:10
the same you use this system module then
3:13
we receive these arguments by this
3:16
properties and
3:19
this and in this way you can do this
3:24
thank you very much for watching this
3:26
video and uh also check out my website
3:30
freemediatools.com
3:32
uh which contains thousands of tools
#Computer Science
#Programming
#Scripting Languages