How to Create and Use Functions in Python! Beginner Tutorial

LeMaster Tech
183 followers
453 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 create functions in Python!!
This is episode 5 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=p46ih5Y_xXM&t=14s
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 five in the python
tutorial series so if you missed the
first ones leading up to this on getting
started with python and some of the
basics of variables and uh using
certain different basic functions of
python
be sure to check those out if you're
unfamiliar with those concepts because
we'll be using those as we go forward so
in our previous video we explored how to
build a four function calculator but it
had its limitations all you could really
do was call it once and do one operation
with it and today what we'll be taking a
look at is using functions in python and
a function is basically any code that
you might want to use and reuse and it
would make more sense to group it in
some sort of
reoccurring code structure where you can
use it over and over again so if you
think about an actual calculator
adding subtracting multiplying raising
something to an exponent these are
things you would want written as
functions not just one offs because you
want to be able to call it any time
so what we're going to do today is we're
going to start by creating an addition
function and right at the beginning it's
going to look just like what we did for
our calculator but you'll start to see
the value of it as this video goes on so
in python to create a function you start
with this def that's defining something
as a function and then you give your
function a name python has something
called snake case where you name things
lowercase and if you're going to have
multiple words like add two
numbers use underscores in between the
various words to
just keep it consistent with python's
naming convention but all we're doing is
add
if you have to pass variables in like
there is a way we'll take a look at
later where you could do the add
function and you could actually pass the
numbers that are being added in you put
those in these parentheses with commas
in between them just specifying that
those are parameters that will be used
inside the function but we're actually
going to do all of our gathering of the
numbers and adding them together inside
the function so let's get started the
two things you're going to need to add
something together are going to be
two inputs which again if you missed the
video on getting user input be sure to
go back and check that out but we're
going to ask the user for two numbers
i'm just going to go ahead and copy that
down
and once we gather two numbers
enter a second number
then inside this function we're just
going to go ahead and print
the two numbers added together
and right there you can see
that we're skipping all in the previous
four function calculator we had four
different if statements saying
if it's this operator if it's that
operator if it's that operator do this
and and you're grouping
this code into one spot so
to create a full calculator you would
just
sorry
to create a full calculator you would
create a define add a defined
subtraction multiplication division if
you had an advanced calculator you could
create additional functions
but then
as you'll see all you need to do to
actually call one of these is type in
the name of your function and then these
parentheses and if you created a
function with parameters which we'll
look at in a minute
you would put your values in here
so we're just going to go ahead and run
this
and we're going to see it asks us for a
number we say 12 it asks for a second
number 23 there gives us 35 and you
could be saying
hey this looks exactly like not using a
function and just hard coding these
lines of code
and and you're correct until you start
seeing how we can add a second function
and we'll call this
go again
and we're gonna ask the user after they
add
if they want to
add two more numbers and so this is
actually we're gonna make a variable
called again and we are just going to
ask for an input
want to add again
and we'll tell them what sort of values
we're looking for yes or no
and then what we're going to do is if
they enter
yes and again if you're confused about
any of the formatting that you're seeing
here be sure to go back and look at the
previous videos or you can just
trust me that it is the way it is
because it is the way it is
um but if they enter
if they say they want to go again we're
going to run that add function again
just like we did the first time and then
the only other thing we need to do is
after we're done adding we actually have
to call
this go again function so before i run
it if you just see
these are not actually doing anything
until called that's the nature of a
function it's a block of code that you
store somewhere and you can call on
anytime you need it but it's not going
to run so python runs top to bottom and
the first actual instruction that does
anything here is us calling this add
function so you can see you're going to
come through and add once and then we're
running the go again function and it's
asking if you want to add again and you
have the option to say yes or no i mean
we could add the condition saying like
else
return or else pass i mean you can put
things in that are basically null code
like else pass here is us saying
anything other than if being again is
just going to exit the function you
could put this in it's completely
unnecessary for this scenario
so we're going to run that again we're
going to add 12 to
348 it's 360. and now we'll see let's
say yes i have another edition i want to
do well that was 360. let me add 834 to
it
oh
you can't type 360 plus 834 in
let's do
23
133. yes i want to do it again 12
and be sure not to type out the entire
mathematical equation as an input but
there you can see we can say yes and
then it's going to keep looping until we
actually say no we're done
so right away you can see rather than
have to stop this code and start this
code stop this code and start this code
to add multiple different sets of values
together we can just go ahead and
create functions that call each other
and give the operator the power to exit
anytime they'd like or run it again as
many times as they like so then what you
get is
the ability to do something like this
where you also create a
subtraction function
and we're just gonna do number one minus
number two if they call this
but then now we'll say if you want to
add again
then we will say
what function
and we will tell them to do
addition or subtraction
and so now we're going to basically do
this entire nested
loop that we just created we're going to
do the same thing but this time we're
going to check if and we'll call that
function
we're going to check
and say if the function is
addition
we're going to go ahead and add
but then again going back to what we
already talked about if the function is
subtraction
then we are going to
sub
and why are we getting red squiggly
lines under add
ants up
because they have to be indented inside
the ifs of course
formatting is something that you'll be
fighting with as long as you write code
so don't worry about it
and in this scenario we actually will do
an l if um you you don't really need to
but for clean code sake you're saying
you never want to run both of these
things
so now let's just go ahead and say
we're going to start by adding and we'll
do one plus two want to add again
yes but this time i actually want to
subtract and now i want to do 24 minus
12 and we get 12. and so i mean there
are all kinds of ways you can clean this
up obviously rather than saying want to
add again you could say want to go again
rather than just starting by calling the
add function you could actually call the
go again again function
so you actually have the power to choose
what your initial
you know
function that you run is right off the
bat and you get to say whether or not
you want to do
addition or subtraction
that that could be a nice
feature to add my point is not showing
you every possible iteration of
functions my point is
when you create
functions and they actually tried to run
a function that had already stopped but
when you create functions you give
yourself an enormous amount of power to
do things that
you would have to hard code every
scenario
um you can see right away if you if you
start answering prompts incorrectly it
just exits it doesn't crap out it
doesn't give you an error message it
just exits um and and
there are all sorts of things you can do
inside of functions you can do error
handling you can call sub functions you
can pass
a whole other routines inside of
functions
we'll get much more into advanced
functions as the tutorial series goes
through but for now
this should cover the basics of why you
would want to use functions and how
functions work if you have any specific
questions about what you saw here or the
best way to go about another application
just drop a comment below and i'll try
to get back to that as soon as possible
in the meantime
be sure to check out the channel
leave a like and subscribe if you found
this stuff useful and as always thanks
for your time and good luck with your
code thanks bye
you
