Math & Variables in Python - Beginner Python Tutorial #1

Video Statistics and Information

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