How to Make a Simple Calculator With Python | Python Beginner Projects

LeMaster Tech
181 followers
340 Video Views·Apr 23, 2025
This is the best place to learn and practice python skills for beginners looking to get started with Python! Learn how to build a 4 function calculator in just 8 minutes!!
This is episode 4 in an introduction to python series coming from LeMaster Tech.
Be sure to give us a like, drop a comment, or subscribe to the channel for more content like this!
The previous video:
https://www.youtube.com/watch?v=2f_FPbc3TCw
python download:
https://www.python.org/downloads/
pycharm download for windows:
https://www.jetbrains.com/pycharm/dow...
hello and welcome back to the channel
this will be video 4 in our python
tutorial series so if you missed the
first few on installing and setting up
python and pycharm initial introductions
to variables and getting user inputs go
ahead and check those out if you're not
familiar because we will be continuing
from where we left off in this video
so for this video we're creating a basic
four function calculator and what that
means is we're going to be taking three
user inputs
num1 num2
and operation
and so let's get right into it uh first
thing we're going to have is a variable
which we'll call number one and we're
going to have that be an input
where we ask the user to enter
first
number just like that
and
we want this to be a numerical
variable type so if you remember from
past videos when you want to make sure
variable is going to come in as an
integer you go ahead and use int and
then
the operation we're just going to make
that an input where we prompt the user
to
enter
operation and since we need this to be
some fairly specific info let's even
give them you know plus
minus
multiply
divide give them the options for the
operations so they know what we're
looking for
and then we'll make number two just like
the first number of being integer we'll
ask for an input and we will say
enter
second
number and you can see already i get
kind of uh
loosey-goosey with whether i use one
parentheses and two parentheses they're
identical to python so it's honestly
sort of whichever one i click at the
time
so it's not
it's good to be consistent but i'm also
trying to show you can use either of
these and it'll be just the same so
right now if we run this it shouldn't do
anything but it should take our three
prompts and you can actually see
we can even enter things that aren't
those correct operations in here but
then the code doesn't do anything so
that's what we'll be doing next
we have three variables that are now
gathering operator
info but we haven't used it to do
anything and before we haven't done
anything conditional where we check what
a variable actually is and do specific
specific functionality based off of what
was entered so
that's going to be handled in something
called an if statement
and again if you're familiar with any
general coding concepts this shouldn't
be too new to you but in general an if
statement is going to check that a
variable in this case our operator is
equal to
a specific thing so
first we're going to handle the addition
case and an important thing to note in
python is you need to double equal signs
if you're checking equivalency
single equals sign is going to tell the
code you want to set a value to that
variable double equals sign is a
comparison checker
and then if statements as well as four
and while loops and things we'll cover
in future videos they need to end end
with a colon so they know where the end
of the conditional statement is and this
is our first introduction to indentation
in python this is very important when
you put code underneath a conditional
statement it goes in by one indentation
so if you were to put another if
statement inside of that and then
conditions inside that it's going to be
another level of indentation down that's
how visually that's how python knows
what code is contained in what modules
so for now if operation is equal to
addition then what we want to do is
print the value of
number one
added
to number two
and we're going to go ahead and take
that and we're going to copy that down
as we handle our next condition
if the operation is subtraction then
we're going to do number one minus
number two
and this code actually will work and it
is valid
but something that's kind of cool you
can do inside python as well is an l if
and this code can only be executed if
this code did not run
so
for the case of looking for a specific
operation it doesn't make that much
sense but let's say we were checking for
a name
and we want to say if operation if name
equals peter print your name is peter we
would use an l if
your name was pete you would print out
pete
but because you wouldn't want to see
that p was in the name but not the whole
name that's a little bit of a
complicated explanation but it'll come
in handy later
just something to start thinking about
so we'll go ahead and copy this in two
more times we'll handle the
multiplication and
division scenarios
and we should be able now
i'm sure it'll show up if i'm in there
we should be able to now say five plus
three
and right there you can see it gives us
eight before finishing the routine so
let's try a few other functions let's
try
nine
times
two
and
it looks like uh oh of course because i
left them all as
subtraction when i copied them
let's go ahead and run nine times two
again because it should not be seven
i'm not great at math but i knew that
one
so there we go 9 times 2 is 18 but
you're gonna notice a few of the issues
in our calculator that we can actually
go ahead and take care of right here if
you want to enter a number that's a
decimal right now because obviously
we're not always going to be dealing
with whole numbers we get an error
because we're saying we expect an
integer so here's our introduction to
the second type of
python variable that handles numbers
rather than the int function we're going
to use what's the float function meaning
floating point decimal value
so we're going to change both of our
numbers to floats and what you'll
immediately see
is it accepts values like 5.4 we can do
addition and we can give it 1.2
and it's going to go ahead and return
for us
6.6 in quite a few decimal places i'm
actually fascinated at where that 5
comes from
but
that's so the float is what lets you
handle things more precise than integers
so we should be able to go ahead and say
5.6 let's subtract from it
ah 1.9
and there you go it's it's running math
and uh it's adding some decimals which
is neat but
you can see right there we're getting
decimal values
and then
one other limitation that we're going to
find is if we say well the first number
is 56 the operation is you know
three and you accidentally put a number
in there we're not handling that anyway
it just thinks it's running the code but
it's also not doing anything
we can actually handle this situation
by adding some code up here that says
else and this is saying we've checked
and our operation is none of these four
things
and we can go ahead and give them a
conditional that says
operation entered
was invalid
so all we're doing here is we're saying
you know what we checked it's not
addition multiplication subtraction or
division we went through and it's none
of those so that means your operation is
invalid so if we run this now
let's say we want 12
and we want
we accidentally added a name and we want
to add it to 2
operation entered was invalid there you
have that right there and that's pretty
cool we've built a basic calculator that
handles four functions as well as lets
you know whether or not you've got a
valid operator so
as always thanks for tuning in hope you
enjoyed the video feel free to leave a
comment if you have any questions i'll
try to clarify anything for you
and be sure to like and subscribe if you
found this content useful and be on the
lookout for more tutorials coming soon
thanks bye
