Coding

Functions in Python – Beginner Python Tutorial #3 (with Exercises)

  • 00:00:00 what's up guys and girls and welcome
  • 00:00:01 back to another Python tutorial video in
  • 00:00:03 this video we're going to be having a
  • 00:00:05 lot of fun and that's because we are
  • 00:00:06 going over functions functions allow you
  • 00:00:09 to simplify your code by breaking it
  • 00:00:11 into small reusable components and
  • 00:00:14 they're used super frequently when
  • 00:00:16 you're programming before I go through
  • 00:00:18 functions will do that challenge problem
  • 00:00:21 that I introduced in the last video and
  • 00:00:23 then finally we'll end this video by
  • 00:00:25 doing a couple of practical function
  • 00:00:27 examples where we kind of have we we use
  • 00:00:30 all of the skills we've learned thus far
  • 00:00:31 and try to apply it to solve some
  • 00:00:34 problems as always the video outline
  • 00:00:37 will be posted in the comments so make
  • 00:00:39 sure to check that out if you want to
  • 00:00:40 find a specific thing that we go over
  • 00:00:43 and please if you like this video or
  • 00:00:46 learning anything from this series
  • 00:00:48 throw me a big thumbs up and also
  • 00:00:49 subscribe so that you don't miss any
  • 00:00:51 future tutorials alright as I always
  • 00:00:54 open up a new sublime text 3 window and
  • 00:00:57 save a new Python file I'm just calling
  • 00:00:59 this lesson three dot py and to begin
  • 00:01:02 we're going over that problem that I
  • 00:01:04 introduced in the last video so we're
  • 00:01:07 trying to find the we're gonna try to
  • 00:01:09 write a set of conditional statements
  • 00:01:11 that identifies numbers as special
  • 00:01:14 numbers and special numbers are defined
  • 00:01:15 as numbers less than 100 or greater than
  • 00:01:18 or equal to 300 that are perfectly
  • 00:01:20 divisible by 3 7 or both
  • 00:01:21 so let's begin just by defining a number
  • 00:01:24 as and then like and around 123
  • 00:01:27 we'll miss around with this as we get it
  • 00:01:29 going so what we're gonna do for this
  • 00:01:31 set of conditional statements is I think
  • 00:01:34 it's important we should begin by just
  • 00:01:36 identifying whether or not it is a
  • 00:01:39 special number and then we'll go into
  • 00:01:41 breaking down case by case so if it's a
  • 00:01:45 special number then it has to be begin
  • 00:01:47 with this line here has to be either
  • 00:01:52 less than 100 or number has to be
  • 00:01:57 greater than or equal to 300 and that's
  • 00:02:01 just this line right here so I'm gonna
  • 00:02:04 group that together because only one of
  • 00:02:06 these things has to be true but if it is
  • 00:02:07 a special number one of these like def
  • 00:02:09 does have to be true so we got that then
  • 00:02:13 the second part of something being a
  • 00:02:14 special number is that it's perfectly
  • 00:02:16 divisible by three Sevenfold so we need
  • 00:02:19 to do and so we're now leading it to the
  • 00:02:23 second condition we have number in mod
  • 00:02:27 three so that does divide it by three
  • 00:02:30 then take the remainder equals equals
  • 00:02:32 zero so that makes it perfectly
  • 00:02:33 divisible or number mod seven equals
  • 00:02:39 equals zero and this statement over here
  • 00:02:43 will be true if it's either divisible by
  • 00:02:45 three divisible by seven or if these are
  • 00:02:48 both true it still is true so this now
  • 00:02:52 identify is if it's a special number so
  • 00:02:54 I'm just going to say print a special
  • 00:02:56 number and then we can write it down
  • 00:03:00 here hopefully you can see this else
  • 00:03:03 print and then I'm just gonna copy what
  • 00:03:06 I told you to say not a special number
  • 00:03:11 whoa these quotes are weird
  • 00:03:13 copy and pasting didn't work for me all
  • 00:03:17 right
  • 00:03:17 not a special number so we should all
  • 00:03:20 have this on the screen right now so 123
  • 00:03:23 would not be a special murmur because
  • 00:03:25 it's not less than 104 greater than
  • 00:03:27 equal to 300 so if I try let's say 317
  • 00:03:33 that now it's just not perfectly
  • 00:03:36 divisible by 3 or 7 but it would meet
  • 00:03:39 this qualification so let's just try
  • 00:03:41 something we know that's divisible by 3
  • 00:03:43 so I'm gonna do 303 and that would be
  • 00:03:46 101 times 3 so this should say special
  • 00:03:49 number and that looks good and when
  • 00:03:51 we're writing something like this make
  • 00:03:53 sure you play around and try to test all
  • 00:03:54 the edge cases to make sure it's
  • 00:03:56 actually working properly
  • 00:03:57 so we have it identifying it's a special
  • 00:04:00 number or not now we just need to write
  • 00:04:01 a set of nested if statements to figure
  • 00:04:05 out if what condition it is so I'm gonna
  • 00:04:07 start with if number mod 3 equals equals
  • 00:04:13 0 and number mod 7 equals
  • 00:04:17 equals zero then it would be this case
  • 00:04:24 right here divisible by both so I'm just
  • 00:04:26 going to copy that in and paste it here
  • 00:04:32 then we could do else if so Elif number
  • 00:04:40 mod 3 equals equals zero so right now if
  • 00:04:46 it didn't pass this condition then we
  • 00:04:48 know it's one or the other because it
  • 00:04:50 did pass this condition so I'm going to
  • 00:04:52 now just so number mod 3 equals equals
  • 00:04:54 zero and if that's the case then because
  • 00:04:58 it didn't pass this above condition it
  • 00:05:00 must only be divisible by perfectly
  • 00:05:02 divisible by three so I'm going to copy
  • 00:05:04 that and print that out
  • 00:05:08 we need quotations there we go
  • 00:05:11 visible y3 and then finally you could
  • 00:05:13 either write Elif number mod seven
  • 00:05:15 equals equals zero but that's actually
  • 00:05:17 not necessary you can just do else
  • 00:05:19 because we know it's either divisible by
  • 00:05:21 three or seven from this statement up
  • 00:05:23 above so we can do prints divisible by
  • 00:05:30 seven and then we should just this is
  • 00:05:34 this is the completed challenge problem
  • 00:05:38 but you should always just confirm that
  • 00:05:41 it's working divisible by three cool if
  • 00:05:45 we think about it let's say that's what
  • 00:05:48 does the number that is divisible I'm
  • 00:05:50 gonna just comment all this out real
  • 00:05:51 quick I'm gonna find a number that's
  • 00:05:53 divisible by seven perfectly so we'll
  • 00:05:56 just do like seven times seven years or
  • 00:06:01 50 that'll be three hundred fifty and we
  • 00:06:05 know that that works that's above three
  • 00:06:07 hundred so we're actually we could just
  • 00:06:09 do a number like twenty one just to make
  • 00:06:11 sure it does the both condition because
  • 00:06:13 that's less than 100 but divisible by
  • 00:06:14 both so we'll try a number equals twenty
  • 00:06:18 one and see if that works
  • 00:06:21 divisible by both cool and then a number
  • 00:06:23 that wouldn't be divisible by three
  • 00:06:25 would be someone like 70 but it would be
  • 00:06:28 divisible by seven cool it all looks
  • 00:06:31 good
  • 00:06:31 and you could more robustly test this if
  • 00:06:33 you wanted to but I think it looks good
  • 00:06:35 and also testing is a feature of
  • 00:06:38 programming that is super important and
  • 00:06:40 I'll make sure that I'd make a video on
  • 00:06:42 like the proper way to test your code in
  • 00:06:44 the future okay now that we've done that
  • 00:06:49 let's move into functions the main focus
  • 00:06:52 of this video so I wanted to delete all
  • 00:06:54 of this code here I might post it on my
  • 00:06:58 github if you're looking to just get the
  • 00:07:00 final code so check the description for
  • 00:07:03 that delete save okay now we have a
  • 00:07:05 brent blank slate I'll make this a
  • 00:07:07 little bigger too okay so functions so
  • 00:07:13 to introduce the functions I want to
  • 00:07:14 show you a concrete example of why
  • 00:07:16 they're so useful so here I have a
  • 00:07:19 turtle program and what this turtle
  • 00:07:23 program does is it builds this animation
  • 00:07:27 so it's just drawing Pentagon's on the
  • 00:07:30 screen and if you've never seen the
  • 00:07:32 Python turtle package I really recommend
  • 00:07:34 checking it out it's a fun way to play
  • 00:07:36 around with your Python skills I've made
  • 00:07:39 a video kind of going over everything
  • 00:07:41 you can do in it and I'll post that link
  • 00:07:43 straight above me right here so check it
  • 00:07:45 out if you haven't already okay so
  • 00:07:48 that's what happened it ran these
  • 00:07:51 Pentagon's and the code for that is all
  • 00:07:56 over here on the left side so as you can
  • 00:07:59 see it's a lot of code it's a hundred
  • 00:08:05 and forty five lines here but if you
  • 00:08:09 look closely you know I'm moving the pen
  • 00:08:12 I'm making a turtle called Bob setting
  • 00:08:15 his speed in color and then I'm setting
  • 00:08:18 his position initial position and I'm
  • 00:08:19 drawing Pentagon right here and then I
  • 00:08:23 move locations draw another Pentagon
  • 00:08:25 here move locations again draw a
  • 00:08:29 Pentagon and every time I draw this
  • 00:08:31 Pentagon it's the same exact thing like
  • 00:08:34 it's kind of seems silly that I have to
  • 00:08:36 copy and paste so many times when I'm
  • 00:08:38 doing the same exact thing to draw a
  • 00:08:40 Penta
  • 00:08:40 so this is why we use functions so this
  • 00:08:44 was our kind of our code without
  • 00:08:47 functions and now I over here on this
  • 00:08:49 tab I wrote the same exact code it does
  • 00:08:51 the same exact thing you can see here
  • 00:08:58 wow this is so fun does the same exact
  • 00:09:04 thing but now we have this function
  • 00:09:08 called draw Pentagon which takes in the
  • 00:09:11 turtle which would be the turtles name
  • 00:09:12 which in this case would be Bob then has
  • 00:09:15 an x and y variable that tells it where
  • 00:09:19 to start the Pentagon so this is the
  • 00:09:23 exact same thing as we're doing here
  • 00:09:25 with this go to but now we've replaced
  • 00:09:27 50 and negative 50 with X&Y placeholders
  • 00:09:31 you know if you don't understand this
  • 00:09:34 fully right now don't worry about it I'm
  • 00:09:35 gonna break down how to actually write a
  • 00:09:36 function in a second I'm just showing
  • 00:09:38 you that I took this same exact code
  • 00:09:40 from this put it over here made this a
  • 00:09:43 function and then I was able to get
  • 00:09:45 everything in there within 36 lines so
  • 00:09:48 we just cut down over a hundred lines by
  • 00:09:50 using function so pretty useful and also
  • 00:09:54 it's easier to read now now I just see
  • 00:09:56 I'm drawing a bunch of Pentagon's as
  • 00:09:58 opposed to like all this kind of messy
  • 00:10:02 set of lines okay so that's a function
  • 00:10:06 I'll put this code in the description as
  • 00:10:10 well on my github page okay let's write
  • 00:10:15 our first function on this blank file so
  • 00:10:17 we're gonna define a function called a
  • 00:10:19 span and to do that we would write def
  • 00:10:23 spam parentheses and then colon so this
  • 00:10:28 def whenever you writing a function you
  • 00:10:29 have to start with def which means
  • 00:10:31 defined then saying we're defining a
  • 00:10:33 function called spam so this can be
  • 00:10:35 whatever name you want it to be here and
  • 00:10:37 then open and close parenthesis and then
  • 00:10:40 if you need to like pass things into
  • 00:10:42 your function you can like list them off
  • 00:10:44 here and I'll show you how to do that in
  • 00:10:45 a second we're doing a really basic
  • 00:10:47 exempt right now and you're gonna see
  • 00:10:48 why it's called spam in a second so
  • 00:10:51 measured I'm trying to write a function
  • 00:10:53 to just spam
  • 00:10:53 you guys with with messages I can do
  • 00:10:56 define spam and then I could have it
  • 00:10:58 print out something like subscribe Keith
  • 00:11:03 he is the best
  • 00:11:08 so if I was trying to spam you I could
  • 00:11:11 now save that and also know that just
  • 00:11:15 like if statements this has to be
  • 00:11:16 indented inside the function name so now
  • 00:11:21 what this means is that whenever I call
  • 00:11:24 spam parentheses spam parentheses it
  • 00:11:30 will actually print out subscribe keith
  • 00:11:33 he is the best
  • 00:11:35 so if I really wanted to spam you I
  • 00:11:38 could just keep copying and pasting that
  • 00:11:40 so right now I have it like 10 times
  • 00:11:43 save that and as you can see it copied
  • 00:11:49 that 10 times and printed it out so
  • 00:11:52 that's a really basic function let's
  • 00:11:55 that move into something a little bit
  • 00:11:56 more I useful I guess unless I guess
  • 00:12:00 this maybe is useful for me all right
  • 00:12:02 got spam let's uh let's write a function
  • 00:12:06 that cubes a number so we pass in a
  • 00:12:09 number and it returns that number cubed
  • 00:12:15 so like if we passed in 3 it would do 3
  • 00:12:18 times 3 times 3 which is 27 so we're
  • 00:12:22 gonna call this cube or we'll call it
  • 00:12:27 cube number Oh what the heck happened
  • 00:12:30 cube number and in this case we have to
  • 00:12:37 pass in the number we want cubed so in
  • 00:12:40 these parentheses we write like
  • 00:12:43 something like X and this can be
  • 00:12:45 anything that you write in this
  • 00:12:46 parenthesis it doesn't have to be X it
  • 00:12:48 could be number or something it's just
  • 00:12:52 this this spot right here just acts as a
  • 00:12:54 placeholder so def cube number and I'm
  • 00:12:58 just going to make it X just to be to
  • 00:13:02 make it simple and I could do something
  • 00:13:04 like
  • 00:13:07 so print X and then to exponent are two
  • 00:13:12 asterisks is to the Q to the root of
  • 00:13:16 three so this is X to the third power
  • 00:13:20 and now if I call cube number of three
  • 00:13:28 it right it prints out 27 so there's one
  • 00:13:33 thing you that you would kind of I feel
  • 00:13:35 like I soon when you're writing
  • 00:13:36 functions you use this print statement
  • 00:13:38 so it's actually not the case that we
  • 00:13:40 want to use the print statement in the
  • 00:13:41 functions we actually want to use a
  • 00:13:43 statement called return and I'm gonna
  • 00:13:46 show you why right now so if I left this
  • 00:13:49 as print let's say I wanted to set a
  • 00:13:51 variable just called a or something like
  • 00:13:53 as the cube of three now if I try to
  • 00:13:58 print a you'll see that if I leave this
  • 00:14:01 as print it doesn't actually print out a
  • 00:14:05 so I'm gonna it says none here right and
  • 00:14:11 the reason is is yes it printed out 27
  • 00:14:14 but printing is not allowing us to
  • 00:14:17 capture that value so we really want to
  • 00:14:20 capture that value what we should use in
  • 00:14:22 functions and this is like you
  • 00:14:25 definitely use this in functions you
  • 00:14:26 don't use printing functions unless
  • 00:14:27 you're just like checking something you
  • 00:14:29 use return so I'm gonna do return X to
  • 00:14:33 the third power and now if I run this
  • 00:14:36 you see it just says 27 and that's
  • 00:14:40 because right now it didn't print return
  • 00:14:43 doesn't print it out automatically it
  • 00:14:45 only printed it once we set it to a and
  • 00:14:49 now it leaves us with 27 ok so that's a
  • 00:14:53 very simple example and just to note
  • 00:14:55 like as I told you before you can change
  • 00:14:58 this X to anything you want I could
  • 00:14:59 change it to something like dilly dilly
  • 00:15:02 and if I returned dilly dilly to the
  • 00:15:08 third power it still doesn't give me an
  • 00:15:12 error and I could even show you I could
  • 00:15:14 raise this like 7th power so you know
  • 00:15:16 it's like actually changing it up and as
  • 00:15:19 you can see
  • 00:15:20 working with dilly dilly it would work
  • 00:15:21 with something like John Cena really
  • 00:15:25 anything you want to put in here it
  • 00:15:27 would work as long as you're consistent
  • 00:15:29 with this is what you pass in this is
  • 00:15:33 what you passed into the seventh power
  • 00:15:34 that's what we want back so this would
  • 00:15:36 still work so let's do the square could
  • 00:15:40 be nine that still works but as the
  • 00:15:43 convention you probably shouldn't use
  • 00:15:45 John Cena as your variable name you
  • 00:15:47 should pass in things that are
  • 00:15:48 descriptive so X is like descriptive
  • 00:15:52 enough it just knows that it's just like
  • 00:15:54 a number of placeholder you could also
  • 00:15:56 do something like numb and you am to
  • 00:15:59 specify this is the number you're
  • 00:16:00 passing in so be descriptive with these
  • 00:16:02 variable names that's the basic example
  • 00:16:05 alright let's move into it a little bit
  • 00:16:08 more of an exam a little bit more
  • 00:16:10 complicated in examples still pretty
  • 00:16:12 straightforward you could also do
  • 00:16:15 something like define get some so let's
  • 00:16:19 say we wanted to maybe take a second and
  • 00:16:21 try this on your own say we wanted to be
  • 00:16:24 able to write a function that took three
  • 00:16:27 numbers and added them all together
  • 00:16:30 so whatever free numbers we took it
  • 00:16:33 would just combine them and return the
  • 00:16:35 combined sum so try to write that
  • 00:16:37 function real quick okay I'm gonna write
  • 00:16:42 it right now so I'm gonna define a
  • 00:16:44 function called get some that's kind of
  • 00:16:47 a fun name get some and this will take
  • 00:16:49 in three values I'm just gonna call them
  • 00:16:51 for simplicity sake a B and C and so now
  • 00:16:57 if I wanted to find the sum of a B and C
  • 00:17:01 I would return so these are placeholders
  • 00:17:04 for three numbers I would have just
  • 00:17:06 returned a plus B plus C and if I then I
  • 00:17:14 would have so what am i what's happening
  • 00:17:17 if I ran this doesn't do anything
  • 00:17:19 because we haven't actually called the
  • 00:17:20 function we've just defined it but I
  • 00:17:22 could do get some let's do one seven
  • 00:17:32 and like three or something so one plus
  • 00:17:34 seven would be eight plus three would be
  • 00:17:36 11 so this should hopefully return us 11
  • 00:17:39 we run that oh my god I was just fell
  • 00:17:42 back it doesn't actually say anything
  • 00:17:44 yet because return does not print it
  • 00:17:47 just returns the value so that we can
  • 00:17:49 print it out so we could do something
  • 00:17:51 like print get some 173 and as I said
  • 00:17:55 before this year return 11 see if it
  • 00:17:58 does yes it does cool so as you can see
  • 00:18:02 get some 173 turns 11 if I tried
  • 00:18:06 something like maybe I try to add my
  • 00:18:09 name in there that's gonna give us an
  • 00:18:11 error because you can't add a string in
  • 00:18:14 a name or numbers in a string but we
  • 00:18:18 actually could change this function to
  • 00:18:21 something like sergeant on this gun I
  • 00:18:26 could change this function to be
  • 00:18:28 something called like combined name so
  • 00:18:34 instead of now adding numbers let's say
  • 00:18:37 we wanted to like take someone's name we
  • 00:18:39 had their first the middle and their
  • 00:18:40 last name and we wanted to just shorten
  • 00:18:42 it and like combine it as one word so I
  • 00:18:45 could do pretty much the same thing so
  • 00:18:47 when we're adding strings if I did
  • 00:18:48 something like teeth plus galley that's
  • 00:18:55 my last name I could do print Keith plus
  • 00:19:01 galley and as you can see down here it
  • 00:19:04 combines those two things and I also
  • 00:19:06 could add a space in there by doing
  • 00:19:08 Keith oh my god what happened
  • 00:19:12 I can't type so hard with all this
  • 00:19:14 pressure of being filmed
  • 00:19:15 Keith space galley so we could take
  • 00:19:19 something like combining of a B and C
  • 00:19:21 and I could do like and let's say so I
  • 00:19:27 could do something like let's say our
  • 00:19:29 name was Neil Patrick Harris we'll take
  • 00:19:32 that as our name so a combined name of
  • 00:19:37 passing in the a B and C here Neil
  • 00:19:41 Patrick
  • 00:19:44 Harris and to be more descriptive we
  • 00:19:50 should instead of calling us a B and C
  • 00:19:52 we should call this first middle and
  • 00:19:55 last it doesn't matter what we call it
  • 00:19:58 but now it's like okay you're gonna just
  • 00:20:00 add together the first middle and last
  • 00:20:03 names here last and if I run this didn't
  • 00:20:10 print anything yet but I could do full
  • 00:20:13 name equals combined name Neil Patrick
  • 00:20:16 Harris and I could print full name and
  • 00:20:22 as you see it says you know how true
  • 00:20:23 Harris if we wanted to add the spaces we
  • 00:20:25 just do quotations with space in between
  • 00:20:29 Oh what the heck happened I didn't add
  • 00:20:33 here now I got this and one thing I'll
  • 00:20:38 show you that's just something we'll be
  • 00:20:39 doing in the future just kind of a cool
  • 00:20:41 little thing to kind of we'll work
  • 00:20:44 towards is you can do all sorts of
  • 00:20:46 things on strings so I could just get
  • 00:20:49 the first letter of Patrick by doing
  • 00:20:52 middle bracket zero and don't worry
  • 00:20:54 about understanding this at all I'm
  • 00:20:55 gonna cover it in a video on strings
  • 00:20:57 that I'll post in a few weeks okay so
  • 00:20:59 this is functions in a nutshell let's
  • 00:21:01 actually like test our skills and do
  • 00:21:04 some practice problems will be going on
  • 00:21:06 a site called coding bat comm I think
  • 00:21:09 this is a really good site to kind of
  • 00:21:11 work on the skills that you've been
  • 00:21:13 learning in these videos so definitely
  • 00:21:15 try to do some of these exercises on
  • 00:21:17 your own I'll post the link in the
  • 00:21:19 description so we're on coding bat comm
  • 00:21:21 slash Python and we're gonna click on
  • 00:21:25 the warm-up section so click warm-up one
  • 00:21:29 and we'll start out by doing the problem
  • 00:21:33 monkey trouble nice it's fun named
  • 00:21:36 monkey trouble alright so we have two
  • 00:21:39 monkeys a and B and the parameters a
  • 00:21:41 smile and B smile indicate if each is
  • 00:21:44 smiling we are in trouble if they are
  • 00:21:46 both smiling or if neither of them is
  • 00:21:48 smile return true if we are
  • 00:21:50 trouble okay so take a sec try this
  • 00:21:52 problem and then unpause the video when
  • 00:21:55 you're ready to see the answer okay
  • 00:22:00 solve this problem we're gonna have to
  • 00:22:01 use our knowledge of if and else if and
  • 00:22:03 else statements that we've learned in
  • 00:22:05 the previous video so we have the
  • 00:22:07 function monkey trouble a smile B smile
  • 00:22:09 and it says return true if they're both
  • 00:22:14 the same or return false if they're
  • 00:22:17 opposites so false is we're not in
  • 00:22:20 trouble so we'll return false first if
  • 00:22:23 they're not the same so we can say
  • 00:22:27 something like if a smile and not B
  • 00:22:32 smile so this would say if this is true
  • 00:22:36 and this is not true so this is false
  • 00:22:39 then we want to return false and then we
  • 00:22:44 could write L if not a smile and B smile
  • 00:22:50 so this would be a smiles false and B
  • 00:22:55 smile is true we could write it return
  • 00:22:59 false and this is just the first way we
  • 00:23:02 can solve it I'm going to show a simpler
  • 00:23:03 way to solve that in a second and we
  • 00:23:06 could finally say the other the other
  • 00:23:07 case would be they're equal to each
  • 00:23:10 other so they're both false are both
  • 00:23:12 true so we just write else return false
  • 00:23:17 return true and if I run this what
  • 00:23:21 happened I try to save it
  • 00:23:22 oops go it did get all cracked cool but
  • 00:23:26 we're actually doing a little bit more
  • 00:23:28 than we needed to here
  • 00:23:29 so the kind of neat thing to see is that
  • 00:23:32 we don't have to do this not stuff here
  • 00:23:35 we can just do if a smile equals equals
  • 00:23:41 B smile so this would mean if they're
  • 00:23:44 both true or if they're both false we
  • 00:23:47 want to return true because that's when
  • 00:23:50 we're in trouble
  • 00:23:51 otherwise we want to return false oh my
  • 00:23:55 god it just did control eyes again and
  • 00:23:57 now if I run this they're still all
  • 00:24:00 correct and we're still not quite done
  • 00:24:02 we can do it one
  • 00:24:03 or even simpler way what I could write
  • 00:24:06 is instead of even having any if
  • 00:24:07 statements I could just write return a
  • 00:24:13 smile equals equals B smile because this
  • 00:24:17 will evaluate to true or false and we
  • 00:24:19 want this to be false if we if they're
  • 00:24:24 equal to each other
  • 00:24:25 oh what the heck turn a smile equals P
  • 00:24:28 smile yeah so or we want it to be true
  • 00:24:30 they're equal to each other we are in
  • 00:24:32 trouble so we could all we have the
  • 00:24:34 return is just whether or not they're
  • 00:24:35 equal to each other because the cases
  • 00:24:37 that they're not equal to each other
  • 00:24:38 this statement right here would be false
  • 00:24:41 and we get all of the answers correct so
  • 00:24:44 this is the simple list best answer you
  • 00:24:47 could have for this one all right let's
  • 00:24:49 do one more problem before we end this
  • 00:24:51 video so we'll do some double so given
  • 00:24:58 two int values return their song unless
  • 00:25:01 the two values are the same then return
  • 00:25:03 double their song so to begin let's just
  • 00:25:05 do the first thing we'll just return the
  • 00:25:07 song and this is the same thing as get
  • 00:25:10 some but we just wrote so we can just do
  • 00:25:12 return a plus B right oh my god I keep
  • 00:25:17 pressing ctrl s and as you can see it
  • 00:25:20 didn't quite work and that's because we
  • 00:25:22 didn't take into account if the values
  • 00:25:24 are the same so what we can do is we'll
  • 00:25:28 do if a equals equals B then we want to
  • 00:25:32 do one thing and then if a is not equal
  • 00:25:35 to B then we just return the sum as is
  • 00:25:38 normal so if a equals equals B we want
  • 00:25:44 it to return double the sum so we would
  • 00:25:48 return we do this multiple ways we could
  • 00:25:52 write this line and that's kind of a
  • 00:25:53 cool thing about these exercises and
  • 00:25:55 about coding in general there's not just
  • 00:25:57 one answer you can do things tons of
  • 00:25:59 different ways so I'm going to do it
  • 00:26:01 this way I do two times a plus B but
  • 00:26:04 note that because a equals B you could
  • 00:26:06 just do like two times two times two
  • 00:26:10 times a or four times a there's many
  • 00:26:13 ways to do it so a equals being returned
  • 00:26:15 two times to some otherwise
  • 00:26:17 trying to some run that we got it all
  • 00:26:19 working cool cool
  • 00:26:21 alright guys that's all I'm gonna cover
  • 00:26:23 in this video hopefully you get a you
  • 00:26:25 feel like you have a good grasp of what
  • 00:26:26 functions are definitely recommend keep
  • 00:26:29 doing problems on this coding bat
  • 00:26:30 website and if you have any questions on
  • 00:26:33 how to do specific examples in here feel
  • 00:26:35 free to let me know in the comments and
  • 00:26:37 I'll help you out I'll post the next
  • 00:26:39 tutorial video by next week we'll play
  • 00:26:41 go over either four and while loops or
  • 00:26:44 we'll go over lists and Python so be
  • 00:26:46 sure to subscribe to not miss that if
  • 00:26:49 you learned something throw a big thumbs
  • 00:26:50 up this video I think that's it so peace