Coding

Math & Variables in Python – Beginner Python Tutorial #1

  • 00:00:00 what's up ladies and gentlemen and
  • 00:00:02 welcome to the first real video in my
  • 00:00:04 Python programming series in this video
  • 00:00:07 we will be going over a few different
  • 00:00:09 things we're gonna start this video off
  • 00:00:12 by going over the basic math
  • 00:00:13 capabilities of the Python programming
  • 00:00:15 language then we'll jump into the
  • 00:00:18 concept of variables which is basically
  • 00:00:21 just assigning values to different names
  • 00:00:24 it'll also go over the different types
  • 00:00:26 of variables there are and then finally
  • 00:00:29 we'll jump into the super-fun complex
  • 00:00:32 math capabilities of the Python language
  • 00:00:34 anything that you can do on your
  • 00:00:36 calculator you can also do in Python
  • 00:00:39 which makes it a super powerful tool if
  • 00:00:42 you haven't done so already make sure to
  • 00:00:44 set up your Python development
  • 00:00:45 environment I'll post a link on how to
  • 00:00:48 do that on a previous video I made
  • 00:00:49 either here or here I don't really know
  • 00:00:52 which side that comes up on and that
  • 00:00:53 we'll just go over setting up Python 3
  • 00:00:56 and also sublime text which is the
  • 00:00:58 editor that I use in all of these videos
  • 00:01:00 and also I just want to mention before I
  • 00:01:03 begin the best way to learn Python is by
  • 00:01:06 doing so make sure to follow along with
  • 00:01:08 this video and also check out the
  • 00:01:10 description of this video for some links
  • 00:01:12 to exercises that you can do I think at
  • 00:01:15 this point in time probably the best
  • 00:01:17 exercise you can go through is what's
  • 00:01:19 called Python turtle in the description
  • 00:01:22 there'll be a link that goes over this
  • 00:01:23 and it's basically just this cool 2d
  • 00:01:26 drawing package that you can use in
  • 00:01:28 Python it's very straightforward it's
  • 00:01:29 pretty fun to see how your drawing was
  • 00:01:31 kind of here your little animations
  • 00:01:33 develop as your Python skills develop
  • 00:01:36 alright that's all I got to begin this
  • 00:01:38 video let's just jump into this tutorial
  • 00:01:41 alright to start up this video open up a
  • 00:01:44 new sublime text window and save a new
  • 00:01:47 Python file with the name someone like
  • 00:01:50 lesson 1 py you can name it whatever you
  • 00:01:53 want but just make sure to include that
  • 00:01:55 dot py file extension that's how we know
  • 00:01:57 it's a Python file and I'm going to go
  • 00:01:59 ahead and save this in My Documents
  • 00:02:00 folder but you can save it wherever you
  • 00:02:03 want
  • 00:02:03 in the setup video I showed you guys
  • 00:02:06 that you can use this function called
  • 00:02:08 print surrounded by parentheses to print
  • 00:02:11 out basic basic pieces of time
  • 00:02:13 like hello world and then if I save that
  • 00:02:17 with ctrl s or command s if you're on
  • 00:02:19 Mac and then run it with going up to
  • 00:02:22 tools then build or just typing in ctrl
  • 00:02:25 V as it says here it prints out the
  • 00:02:27 words hello world here and I can change
  • 00:02:31 that piece of text or whatever I want so
  • 00:02:33 I could change it up to be something
  • 00:02:34 like keith is the man and if i save that
  • 00:02:39 with ctrl s and build it it says keith
  • 00:02:42 is the man down here alright let's move
  • 00:02:45 into something a little bit more
  • 00:02:46 interesting let's start doing some math
  • 00:02:47 and python and also i just want to
  • 00:02:50 quickly note that feel free if i'm going
  • 00:02:52 too slow for you to put me on 1.25 or
  • 00:02:54 1.5 speed x speed all right so we can do
  • 00:02:58 some all math we can think of we can do
  • 00:03:01 in python basically so start out let's
  • 00:03:03 just do a basic addition problem two
  • 00:03:05 plus four I save that and then run it it
  • 00:03:08 will evaluate the two plus four and give
  • 00:03:10 us six where is it it's not here so it
  • 00:03:14 did run this two plus four we actually
  • 00:03:17 have to go ahead and print out that two
  • 00:03:20 plus four so it actually logs the answer
  • 00:03:22 to the screen so if I do print two plus
  • 00:03:24 four and then make sure to save it
  • 00:03:26 between whenever you make a change to
  • 00:03:28 your code and then run it with control B
  • 00:03:30 gives us six we can do the other basic
  • 00:03:34 math using kind of so subtraction is
  • 00:03:37 pretty intuitive just the minus sign
  • 00:03:38 negative to perfect multiplication you
  • 00:03:42 might not know what it is right off the
  • 00:03:44 bat but it's the asterisk so if you want
  • 00:03:46 to multiply something to asterisk
  • 00:03:48 asterisk four is two times four run that
  • 00:03:52 it gives us eight cool if we're going to
  • 00:03:55 do division we do too backslash four and
  • 00:03:59 that does division 0.5 and I try to not
  • 00:04:04 real quick if you are running Python two
  • 00:04:06 you might actually see zero here instead
  • 00:04:09 of 0.5 the reason for that is that
  • 00:04:11 Python 2 is you take two integer values
  • 00:04:14 and divide them it returns an integer
  • 00:04:19 value it doesn't return a decimal but in
  • 00:04:21 Python 3 they made this change so that
  • 00:04:23 if you divide two integer values it
  • 00:04:25 gives you the desk
  • 00:04:26 value that you expect so 2/4 and python
  • 00:04:29 3 is 0.5 we can also do exponents so
  • 00:04:35 exponents let's say we wanted to do 2 to
  • 00:04:37 the 3rd power that would be 2 double
  • 00:04:40 asterisk 3 so that's 2 to the 3rd power
  • 00:04:43 save it run it that gives us 8 as we
  • 00:04:46 expect I think I'll probably put a math
  • 00:04:49 cheat sheet in the description of this
  • 00:04:51 video so if you need a place to give it
  • 00:04:53 to remember all these commands check out
  • 00:04:55 that math cheat sheet so that gives us 8
  • 00:04:58 and all right let's move into something
  • 00:05:02 will let's move in two variables the
  • 00:05:04 concept of variables and that's storing
  • 00:05:06 values in names so let's say I wanted to
  • 00:05:10 store a variable called age and I'm
  • 00:05:12 currently 23 years old so we want to set
  • 00:05:15 that to 23 so if I use an equals sign 23
  • 00:05:19 that's going to set the variable age to
  • 00:05:23 the value of 23 so I go ahead and now
  • 00:05:26 print out that age and run that it
  • 00:05:30 instead of printing the word age it
  • 00:05:33 prints out the values stored in age
  • 00:05:35 which is 23 and this is kind of the key
  • 00:05:38 distinction for why when we printed out
  • 00:05:40 text we use quotations the quotations
  • 00:05:43 tell the print statement hey we want
  • 00:05:45 exactly this piece of text called age so
  • 00:05:48 I printed this it would not give me 23
  • 00:05:50 over giving me age so that's a key
  • 00:05:53 distinction between variables and string
  • 00:05:55 text okay and we can save other types of
  • 00:06:00 variables so let's say I wanted to
  • 00:06:02 instead of age let's say I wanted to do
  • 00:06:05 name so my name is Keith so I want to
  • 00:06:10 save a variable called Keith and I put
  • 00:06:13 that in a variable name called name so
  • 00:06:16 if I now print out a name and run that
  • 00:06:21 it gives me Keith just what I wanted to
  • 00:06:23 get back so this type the text objects
  • 00:06:28 in programming that's known as a string
  • 00:06:30 so anytime you use exact text we call
  • 00:06:34 these string objects so this is the
  • 00:06:36 string object keith and i'm building
  • 00:06:39 that
  • 00:06:40 one more interesting piece of type of
  • 00:06:44 boolean we can do or type of variable we
  • 00:06:47 can do is called a boolean and a boolean
  • 00:06:50 value is either true or false so let's
  • 00:06:55 call a variable like is Python fun and
  • 00:07:00 yeah yeah it is yeah I think Python
  • 00:07:02 super fun so we're gonna set this to
  • 00:07:04 true so is Python fun if I print that
  • 00:07:07 out is Python fun it says true yeah
  • 00:07:15 Python super fun and just note that you
  • 00:07:18 have to use the uppercase T in true if I
  • 00:07:22 did a lowercase T it would think it's
  • 00:07:25 the variable true instead of the boolean
  • 00:07:28 value true so if I ran that it would say
  • 00:07:31 true is not defined and maybe we were
  • 00:07:35 being pessimistic and we said maybe we
  • 00:07:38 weren't happy with this video so far and
  • 00:07:39 we said his Python fun we wanted to set
  • 00:07:42 that to false we would just do the false
  • 00:07:45 with a capital F so and you see that it
  • 00:07:47 highlights purple and that's how you
  • 00:07:48 know you have the correct value there
  • 00:07:50 but we'll say that's true so to
  • 00:07:55 reiterate we have integer values here 23
  • 00:07:59 is an integer value keith is a string
  • 00:08:04 value and then finally is Python fun
  • 00:08:06 that stores a boolean value I'll put
  • 00:08:08 these in the cheat sheet to that these
  • 00:08:11 are the different types of variables we
  • 00:08:13 also can have like other types like
  • 00:08:15 maybe age 2 and we put this as a decimal
  • 00:08:21 so maybe I'll you know I just turned 23
  • 00:08:24 so let's say I'm 23 point 0 2 or
  • 00:08:26 something like that so now this is a
  • 00:08:29 decimal value all right cool all right
  • 00:08:34 I'm gonna believe that just to clear up
  • 00:08:35 some space all right I think this is a
  • 00:08:38 good time to also mention the different
  • 00:08:40 ways we can name variables so as you can
  • 00:08:42 see I just did age all lowercase here I
  • 00:08:45 didn't name all our kids here and then
  • 00:08:48 this one I did a little bit more come
  • 00:08:49 Alexa did some capitals some lowercase
  • 00:08:53 so when we name a variable we can use
  • 00:08:58 lowercase letters we can use uppercase
  • 00:09:03 letters we can use underscores and we
  • 00:09:09 can use numbers but we can start with a
  • 00:09:11 number we can start with the letters and
  • 00:09:13 then numbers can be included but we have
  • 00:09:15 to make sure we start with the number
  • 00:09:16 you're with a letter so we could take
  • 00:09:20 this is Python fun and it would be a
  • 00:09:24 totally valid variable name to like kind
  • 00:09:27 of stretch it out and be kind of
  • 00:09:28 obnoxious with it and call it something
  • 00:09:30 like is is Python programming fun so
  • 00:09:38 this is super long but it works just
  • 00:09:41 fine as a variable name is Python
  • 00:09:43 programming fun true and I can print
  • 00:09:45 that out is Python programming fun true
  • 00:09:51 and I'm pretty sure you guys can check
  • 00:09:54 this out if you want pretty sure the
  • 00:09:56 length of this can be pretty much as
  • 00:09:57 long as you want
  • 00:09:59 but for good practices you should keep
  • 00:10:01 it pretty short and descriptive the
  • 00:10:03 reason we want to use variables is so
  • 00:10:05 that we can kind of be descriptive and
  • 00:10:07 nice and neaten write our code like
  • 00:10:09 English text like if you see age in your
  • 00:10:12 code you know oh they're talking about
  • 00:10:13 some age even this is Python programming
  • 00:10:17 fun it gets a little bit long but it
  • 00:10:19 tells me something about the code like
  • 00:10:22 I'm just not throwing a bunch of
  • 00:10:23 throughs around it's giving meaning to
  • 00:10:26 my code so when we write these names we
  • 00:10:29 want to just give like meaning it's kind
  • 00:10:31 of be short and concise but give meaning
  • 00:10:33 to whatever we're doing so like like
  • 00:10:36 name you know it's short and concise but
  • 00:10:39 it gives meaning to what I'm passing
  • 00:10:41 around that's Keith and so you'll see
  • 00:10:45 these variables all throughout all
  • 00:10:49 throughout
  • 00:10:50 your Python experience alright let's
  • 00:10:54 move into something more complicated
  • 00:10:56 let's start doing some kind of math that
  • 00:10:59 might not be as intuitive so I want you
  • 00:11:02 guys to try to figure out what this
  • 00:11:04 percent sign is doing in my code so
  • 00:11:06 let's say I did 12% to ram that gives me
  • 00:11:13 zero if I do
  • 00:11:17 12% 3 it gives me zero I do
  • 00:11:25 12% five what is it gonna give me it's
  • 00:11:29 gonna give me two then I do 12 print 12%
  • 00:11:34 7 gives me five so try to figure out let
  • 00:11:41 me take a second to try to figure out if
  • 00:11:42 you can think or figure out what this
  • 00:11:45 percent sign is doing you can plug in
  • 00:11:48 your own numbers here but it actually is
  • 00:11:50 a pretty useful function but it's not
  • 00:11:53 intuitive what it does if you just maybe
  • 00:11:54 are seeing it the first time take a
  • 00:11:56 second to do that alright so I'm gonna
  • 00:12:02 explain what it does so this is actually
  • 00:12:03 this percent sign is taking is called
  • 00:12:06 modulo division and so if we did 12
  • 00:12:09 divided by 2 12 divided by 2 2 would go
  • 00:12:13 into 12 perfectly 6 times and it gives
  • 00:12:16 us 0 as a remainder we did 12 divided by
  • 00:12:20 3 it goes into in perfectly 4 times and
  • 00:12:22 gives us 0 as a remainder if we did 12/5
  • 00:12:25 though it would divide into it two times
  • 00:12:29 but b remainder 2 so it's basically just
  • 00:12:31 giving us the remainder of the division
  • 00:12:33 problem and 12 by 8 divided by 7 is the
  • 00:12:35 same thing it goes into it once but has
  • 00:12:37 remainder 5 so if I did something like
  • 00:12:39 total divided by 8 it'd give us 4 so
  • 00:12:43 that's what the percent sign does it
  • 00:12:45 helps us out because we could do like we
  • 00:12:48 can kind of figure out things are odd or
  • 00:12:49 even using this module division like if
  • 00:12:52 I did any number modulo 2 the / – and if
  • 00:12:57 there's a remainder of 1 then it's odd
  • 00:12:59 if there's a remainder of 0 it's even so
  • 00:13:01 that's kind of a cool little
  • 00:13:03 thing also you have stuff like floor
  • 00:13:06 division so if you do double backslash
  • 00:13:09 say 12 double backslash 5 and run that
  • 00:13:14 it gives us just 2 so it's taking me 12
  • 00:13:18 divided by 5 which is something like 2.4
  • 00:13:22 and it's just returning the floor of
  • 00:13:25 that is returning the whole number the
  • 00:13:27 first whole number that is less than 2
  • 00:13:30 point 4 which is just 2 so that's for
  • 00:13:33 division and we can just I can keep
  • 00:13:35 going on and on about all these
  • 00:13:36 different functions but if you want to
  • 00:13:39 have a complex like you want to know all
  • 00:13:43 the different math things you can do
  • 00:13:44 there's this library in Python called
  • 00:13:47 math so I'm going to bring this in real
  • 00:13:49 quick so this is the Python math library
  • 00:13:53 and it has all sorts of useful functions
  • 00:13:55 that you can use in Python and this is
  • 00:13:59 just one library if there's even more
  • 00:14:00 things that are not found here just do a
  • 00:14:03 Google search on like how to do this in
  • 00:14:05 Python you'll probably find a library
  • 00:14:06 that includes it so let's say I wanted
  • 00:14:09 to include wanted to do a complex math
  • 00:14:12 let's say I wanted to take the sine of
  • 00:14:15 something so say we're in a trigonometry
  • 00:14:18 class and I wanted to take the sine of a
  • 00:14:20 value so sine of like 12 and if you
  • 00:14:23 don't know what sine means don't worry
  • 00:14:25 about it just a complex math
  • 00:14:26 trigonometric equation I run that so
  • 00:14:30 sine is not defined oh why is it not
  • 00:14:32 defined well it's not defined in Python
  • 00:14:36 as like Python at its core but if we go
  • 00:14:40 into that library that I just showed you
  • 00:14:42 and I type in my control f for sine you
  • 00:14:46 see that it gives me math dot sine oh so
  • 00:14:49 I can use math dot sine to find the sine
  • 00:14:52 of the value so let's do change this to
  • 00:14:57 math dot sine of 12 and shoot it still
  • 00:15:02 is not working why is that this not
  • 00:15:03 working I did exactly what it said and
  • 00:15:06 the reason for this is because to use
  • 00:15:09 this math library Python doesn't know
  • 00:15:11 what this math library is unless we
  • 00:15:14 import it
  • 00:15:16 to our file so we can import all sorts
  • 00:15:18 of libraries so we're importing math and
  • 00:15:20 then doing math a sine of 12 and if you
  • 00:15:23 didn't know off the top of your head
  • 00:15:25 math that sine of 12 is negative 5 or 3
  • 00:15:28 whatever so yes we can do method sine
  • 00:15:31 and let's say I just want to do math dot
  • 00:15:33 sine of like 90 degrees or 180 180
  • 00:15:37 degrees that should give us that's at
  • 00:15:40 the zero axis so that should give us
  • 00:15:42 zero and shoot it doesn't give us 0 and
  • 00:15:46 if I look into the documentation the
  • 00:15:48 reason it didn't give us 0 is because it
  • 00:15:51 returns the sine of X radians so I have
  • 00:15:54 to do this in radians so the 180 in
  • 00:15:57 radians is PI so if I do math cosine of
  • 00:16:00 pi pi is not defined so actually there's
  • 00:16:03 a in the same library if I look up pi
  • 00:16:08 math dot pi I can actually use this
  • 00:16:12 constant so you can use math the e use
  • 00:16:14 math itaú math not infinity if you want
  • 00:16:17 like to use some sort of infinite value
  • 00:16:20 so math dot sign of math dot pi this
  • 00:16:24 should give us zero what the heck
  • 00:16:26 oh basically does it's kind of weird
  • 00:16:28 that it gives us this is saying times 10
  • 00:16:32 to the negative 16th it's saying it's
  • 00:16:33 not exactly there but it's pretty much a
  • 00:16:35 zero so I can do math left pi divided by
  • 00:16:39 two that give us one pretty cool stuff
  • 00:16:43 so that's that's what I mean all I
  • 00:16:49 really want to cover in this video we've
  • 00:16:50 done basic math the additions
  • 00:16:52 subtractions we've done variable names
  • 00:16:54 so I could do like math value equals
  • 00:16:59 math dot sign of math pi divided by two
  • 00:17:02 and now if I just printed out math value
  • 00:17:07 it gives us this one point zero that we
  • 00:17:10 had an last thing so that was variable
  • 00:17:15 names and then we went into the complex
  • 00:17:18 math we went into the you know the
  • 00:17:20 module division
  • 00:17:22 which was that % and we went into the
  • 00:17:25 floor division which was the double
  • 00:17:27 backslash and then finally I went in and
  • 00:17:30 showed you guys how to use this math
  • 00:17:32 library where you have all sorts of cool
  • 00:17:35 math capabilities as you can see in this
  • 00:17:37 thing check this out if you want gives
  • 00:17:40 us all of this alright so that's all I'm
  • 00:17:44 going to cover in this video hope you
  • 00:17:46 learned something as I said before check
  • 00:17:49 out the turtle Python package to have
  • 00:17:51 some kind of fun things you can do and
  • 00:17:53 show off to your friends at this stage
  • 00:17:55 in your Python development and be sure
  • 00:17:58 to subscribe to this channel to not miss
  • 00:18:00 any of the future tutorials that I'm
  • 00:18:02 going to be posting I'm going to be
  • 00:18:03 trying to post them weekly so be sure to
  • 00:18:06 subscribe to not miss one of those and
  • 00:18:09 if you learned anything in this video it
  • 00:18:10 would mean a lot to me if you gave that
  • 00:18:11 a big thumbs up it encourages me to make
  • 00:18:15 more videos like this all right thanks
  • 00:18:17 guys for watching peace