👩‍💻 Python for Beginners Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone kevin here today i want to show you the fundamentals of programming using python in this video i'm going to assume that you have no programming background at all we're going to start from the very basics and we're going to work our way up step by step and we'll get a little bit more advanced by the end of this video you'll be able to write your very own source code and then run it how exciting is that we're going to start with well why should you even learn how to program and then why python there are so many different programming languages out there why should we choose python then we're going to start with how do you even get python where do you write your code then we'll go through things like operators variables we'll create a function we'll even jump into things like creating an if statement creating loops so your code will run or execute until a certain condition is met we'll even leverage others code by importing libraries into python by the end of this you'll have a very solid understanding of how to get started with programming think of this as a 100 level course that'll give you all of the most essential tools if you want to jump around this video i've included timestamps down below so you can jump to the section that interests you the most to get started you might be wondering well why should i even learn how to program how is this going to benefit me well anytime you go on your phone or anytime you go on your pc you're experiencing the output of something that someone has programmed so you're realizing the benefit think of it this way to program allows you to take maybe an idea that's in your head you can turn it into a series of steps that the computer can understand and execute how cool is that so for example maybe you have a game that you want to create maybe you want to create an application at work to help simplify what you do on a daily basis by programming you can do all of those things it's an incredibly powerful skill to learn how to use okay so you want to learn how to program but why python there's so many different programming languages out there you have c c sharp c plus plus java javascript php and the list goes on and on well recently on my youtube channel i asked all of you what programming language do you most want to learn about and overwhelmingly far and away python was the clear winner it was something like 65 percent of you wanted python and it kind of makes sense when you look at the lists of the most popular programming languages python tends to be right near the top it's been around for a while it's also pretty easy to use and also understand when you code using python it tends to be a very concise programming language it's also a good general purpose programming language and what i mean by that is you can write all sorts of applications maybe you want to write a web app an internal company tool a game or maybe you just want to conduct some scientific analysis you can do all of that with python and all the things that you learned today related to python you can take these same concepts and you can apply it to other programming languages as well and really fundamentally you just want to take some idea you have you want to turn it into a series of steps that the computer can execute and in a sense it's kind of like a different language or spoken language so in english i say hello in german i'll say hello and in french i'll try my best i'll say bonjour and all of those communicate the same idea i'm just saying hello but it's a slightly different way of saying it or speaking it and that's the same with a programming language they have different syntaxes they also have different rules but at the end of the day you're simply trying to communicate something to the computer because you want the computer to do something for you now of course each programming language might have its strengths or weaknesses but you're really accomplishing the same objective all right so why don't we jump on the pc and let's get started with python [Music] here i am now on my pc and first off i want to check if i already have python installed to do that on windows 10 simply go down to your taskbar and within the search field type in command prompt once you type in command prompt click on the best match this opens up your command prompt and to check for python simply type in python and then type in dash dash and version if you have python installed it'll tell you the version of python that you have and here you see i just typed this in and it says python was not found so i need to go and install python if you already have it you can skip the next step if you don't have python like me stick around and i'll show you how you can get python to get python head to the website python.org and that'll drop you on the python home page right near the top you'll see a button right here for downloads if you click on this it'll identify what operating system you're on and it'll recommend the best option however if you're on a different os you can also choose it down here now i'm running windows today so i'll download python 3.9.2 this is currently the most recent version if you're watching this video maybe a few months from now there could be an even more recent version go ahead and download whichever one it recommends to you i'll click on download and here it's kicked off the download process once you finish downloading click on the exe this opens up the install prompt and right up here i could install however before we do that there is an option right here at the bottom that says add python to path let's check that box and then click on install now this now runs through the installation process and it looks like the setup was successful so i'm going to click on close let's now go down below and just like we did before you can type in command prompt and then launch the command prompt once again we can type in python dash dash version hit enter and you should now see that python is installed here i have python 3.9.2 okay now that we've verified that python is successfully installed let's close command prompt and once again let's go down to the taskbar except this time let's type in python and here you should see the best match for python here i see python 3.9 let's click on this this opens up the python terminal and i'll zoom in just a little bit so it's a little bit easier to see and we can co we can type in code directly here and python will interpret it so what's an example that well what if we type in what is one plus two and i'll put a question mark in and let's hit enter and this doesn't work i i see something that says syntax error invalid syntax now the syntax is the rules of a programming language and when i typed in what is one plus two this doesn't align with what python is expecting i need to type in a valid expression that the python programming language understands so instead of typing in what is one plus two well i could just type in one and i'll type in plus two and then i'll hit enter and there it gives me three back so this was a valid expression in the python programming language now what's neat is when i type something into python like one plus two python takes each line of code that i enter so the one plus two it interprets it and then it evaluates it so it gave me three there are other programming languages where you have to compile it first and then you can execute it but with python it'll interpret it as you're going now now that we've typed in just a very basic expression and we've evaluated it and why don't we try a built-in function called print here i could type in print i'm going to open the parentheses insert double quotes and then i'll type in hello world and i'll close my quotes and close my parentheses now this is one of the go-to examples anytime you're starting with a new programming language it's just hello world just testing it out i'll hit enter and here it says hello world so it's sending back exactly what we sent into this function now as we go throughout this our programs are going to get a little bit more complex but this is just showing you the basics of how you can already start executing some code to exit out of the python terminal we can click on the x symbol up here or we can type in another function called exit open your parentheses close your parenthesis then hit enter and that'll close out the terminal window now we executed some of our first code directly in the python terminal but you can also use a text editor to write code on windows there's a text editor called notepad once again go down to your taskbar and in the search field type in notepad i'll click on this right here this opens up notepad and i'll zoom in just a little bit more now you probably never knew that you can write source code in notepad it's actually a pretty powerful app so right here just like we did in the previous example i'll type in print and then i'll open my parentheses put in a quote and then i'll type in hello world once again then i'll close my quotes and close my parentheses so here's my source code written in notepad now of course i want to be able to run this so i need to save it as an appropriate file type i'll go up to the file menu go down to save as and this opens up the save as prompt now maybe i'll just call this file let's call it hello world and if i just leave it as that it'll save it as a dot txt or basically just a standard text document now i want this to be a python file and it turns out that the extension for python is dot py just like the extension for text files.txt python is py so here i'll type in py depending on the programming language that you're using different programming languages have different extensions for example javascript is dot js and every language will have its own extension now that i've typed this in i'll save it to my desktop and then click on save and here now i see the file on my desktop called hello world you'll also notice that it has the python logo on it because we installed python it correctly identifies that this is a dot py file and so it sees that this is a python file now let's test this out to see if we can run this source code that we wrote down below on the taskbar let's type in command prompt just like we did earlier and let's open up the command prompt i've adjusted the command prompt so it's a little bit easier to see and now i want to run this file over here in command prompt to do that let me type in python so this will let the computer know that this is a python file that i want to run i'll put in a space and i'll take my python file and then i'll simply drag and drop it over into command prompt now this is a really cool trick where you can get the location of the file simply by dragging and dropping it in so here basically i'm saying i want to execute this python file next i'll click on enter and here we see that it prints hello world into my command prompt this is exactly what i wanted it to do so python went through and it interpreted the code in this file and then it printed it here on my screen you now know some of the basics of both writing and running python code congratulations you just wrote your first bit of source code and you also ran it you're making some good progress now so far we've just been writing our code in a text editor like notepad and then we've been running it and that works perfectly fine but it's not the most efficient way to write code and i just want to give a parallel example let's say that maybe you're writing a document for school you could write your document in notepad of course it does have some downsides you don't have a spell check you don't have grammar check you can't do formatting so although you can do it it's going to require a lot more work and that same concept also applies to programming sure you could write your code in notepad or we could get what's called an ide and that stands for integrated development environment it'll just make writing code a lot more efficient and also easy and in a few moments we'll see why that is you have all sorts of ides that you can choose from on apple you have xcode on android you have android studio with ruby you have ruby mine and there's also another one called visual studio code and that works very well with python it's a product made by microsoft so of course i like it i used to work at microsoft but the great thing too is visual studio code is entirely free so let's go to visual studio code and let's get this ide to get visual studio code head to the website code.visualstudio.com and once you land on the homepage you can choose the operating system that you have here when i click on the drop down you can get it on mac windows or linux so it supports all of the different major operating systems i'm running windows today so i'll click on download for windows and i'll go with the stable build once the download completes down in the bottom left hand corner click on the exe this opens up the setup process all agree i'll click to accept the agreement and then click on next i'll go with the default location i'll click on next you'll then get a screen that says select additional tasks i recommend checking all of these different boxes next let's go down and click on next on the last screen let's click on install this will now go through and install visual studio code on your computer once you're all done installing let's click on finish and launch visual studio code this now drops us into visual studio code and by default we'll land on the welcome screen if you ever want to get back to the screen again you can click on the help menu and then click on welcome and that'll drop you right here there are a few things we want to do before we can start writing code in visual studio code over on the left hand side there's an icon here for extensions let's click on that this opens up extensions and up here in the search field type in python we want to install the python extension when you type it in you should see python at the very top and when you click on this you'll see that it's python by microsoft it should have something over 30 million downloads and right here if you don't yet have it you can install this extension we're going to need this so click on install and make sure you add it once you finish installing the extension we also want to select python that we just installed as the interpreter for visual studio code and this is really easy to do on your keyboard press ctrl shift and p at the same time so that's control shift and p and that will open up this screen right here and up here type in python colon select interpreter this will allow us to select the interpreter that we want to use now if you remember in notepad when we wrote our code it interprets that code so we basically want to tell visual studio code what interpreter we want to use so i'll select this option right here and now i can choose my interpreter and here you'll see python 3.9.2 so this is what we installed just a moment ago so here i'll select this as my interpreter and now we're all set to start writing code in visual studio code to get started why don't we open up the hello world file that we created previously here i'll go to the file menu and i'll go to open file this opens up my file picker and here on my desktop i see my hello world file here you'll also see that this type of file is now associated with visual studio code this is the visual studio code logo i'll select this file and then click on open now that i've opened the file you should recognize this this is exactly what we wrote into notepad except this time we have it open in visual studio code now you'll start to notice some of the benefits of using an ide or an integrated development environment first off i have line numbers now so here if i add additional lines of code here i get all the line numbers over on the left hand side in notepad we didn't get that also when i write in this function called print it highlights it in different colors so i could identify what is the context or the text that it's printing and what is the function so here i get different colors also when i hover over print it gives me some helpful suggestions on the type of content that i can enter within this function so this will make it a lot easier as we start writing out our code now i can run this code directly in visual studio code i'll simply go up here and i can now right click right down here there's an option that says run python file in terminal when i select that it'll open up a terminal on the bottom of the screen and here i can see that it executed or interpreted this file and here it prints out hello world so it gives me the output of this code right up here along with right clicking on the screen and running the python file in the terminal there i can also go up to the top right hand corner and i can click on this play icon this will also run the python file in the terminal so when i click on this here too it ran this file and here too i see hello world so this is just another way probably a little bit easier to see the output of your code now that we know some of the basics of how to start writing code and how to run that code i want to show you how we can start working with operators to work with numbers and here i'm going to type directly into the terminal down below so i'll go down here to the terminal and just like we did earlier in the python terminal i can type in here so here i'll type in one plus two and i'll hit enter and here it interprets that code and it evaluates it to three so just like we saw earlier one of the things is you'll notice when i typed in one plus two i didn't include any spaces and spaces or white space don't impact the calculation at all so i could also type in one space and i'll type in plus and then another space and hit two and then hit enter and there too it also evaluates it to three so using white space can actually make your code more readable so you could type it in like this or just include some white space it won't affect what the outcome is with python i can use all of the standard operators right up above i showed you how you can do addition but here let's say 5 minus 2. i can also do subtraction and there it evaluates that to 3 as well i could do multiplication so let's say four times four now with multiplication i enter in an asterix that's the multiplication symbol here i'll hit enter and it evaluates that to 16. i could also do division here i'll take 10 divided by 5 and here for division division is the forward slash and then i'll hit enter and that evaluates to two now with python let's say that you start using multiple operators so maybe you're adding and then you're multiplying python follows the default order of operations so let's say i enter in 2 plus 2 and then i want to multiply by 5. what do you think this will evaluate to is it 2 plus 2 which is 4 and then times 5 which is 20 or is it 2 times 5 which is 10 and then you add 2 to that which is 12. well here if i hit enter it evaluates to 12 because it's following the default order of operations multiplication comes before addition if you've ever heard pemdas or please excuse my dear aunt sally before that's how you know what the order of operations is first it'll evaluate whatever's in parentheses then it'll look at any exponents followed by multiplication and division in the order of left to right and then it'll look at addition and subtraction also in the order of left to right up above i could also enter in my expression up here so here i'll get rid of hello world and here i'll say let's print out two plus two here i'll click on run and here you'll see that it executes this code and here this also evaluates to four if i want to use an exponent i'll come up here let's remove the plus symbol and here i'll enter two asterisks so this will be two to the power of two and here if i click on the play button here that evaluates to four two times two is four but let's say i want to do a two to the third here i'll evaluate and it comes out to eight i also have access to the floor operator so let's take an example here let's say i want to calculate five divided by two and here i'll play and right down here i see that's 2.5 so i have a remainder of 0.5 but let's say i don't want to know what the remainder is i just want to know how many times it goes in without a remainder this is also known as the floor i could insert another forward slash and then i could run it and here it tells me that it's two but let's say i just want to know what the remainder is i can use the modulus operator for that i'll get rid of the forward slashes and right here i'll type in the percent symbol then i'll hit go and here it tells me that the remainder is 1. as i'm entering in numbers up above and as i'm working with numbers let's say that i enter a very large number so let's say i enter in maybe 1 million now i could go through here and i could enter a comma and then i'll type in the zeros and if i hit go here you'll notice that it doesn't print what i expect it to that's because we don't want to use commas as we're entering numbers instead when you enter a number just enter it in as is and then hit go and here it prints exactly what we expect so you don't want to use commas when you're entering in numbers now that we've entered in some numbers and we know a little bit about the basics of how you can work with numbers and operators let's start working with some text or what's referred to as strings let's say that i want to print what's up now earlier we printed out hello world and it's the same concept here i'll insert in double quotes and then i'll type in what's up so here's what's up and i'll click on this play icon and down below you'll see that the output is what's up now i surrounded what's up with double quotes now instead of using double quotes i can also use single quotes up here so if i enter in single quotes and then hit play here too you'll see that i get the same output so i can choose whether i want to use double quotes or single quotes so why would i want to use one versus the other well here what's up should actually have a single quote so i'll insert a single quote there now one of the problems is when i enter a single quote here it thinks that this is the end of the string and so now if i try to run it i get a syntax error down below so instead i can use double quotes here and i'll use double quotes at the end and i could use a single quote in the middle and then here i'll run it and it works properly now let's say you wanted to use double quotes within your text well you could surround it with single quotes and that way the double quotes would show up now here too just like we saw before white space doesn't impact how the code runs so if i want to insert a space here and maybe insert a space there and another space here and i run this code here you'll see that i get the exact same output so once again by using white space that can help make your code more readable as you're going through we now know some of the basics of working with numbers and also strings but what if you want to make sure you explain what's happening in your code how do we do that well we can use something called a comment so let's say that i have this code here and i want to explain to someone what this does here i'll bring this code down to the second line and i can insert a comment by entering in the hash symbol or the number sign or the pound symbol so enter that and here i can say this prints some text i've now typed in my comment and let's run this now and here you'll see that this doesn't affect at all what appears down here i simply get what's up so just like we got before now i could also take a comment and i could put it on a line with code so here i'll put it after my code i'll hit run and here too you see that it just gives me what's up and one of the neat things is let's say i start writing a lot of code and it's starting to get fairly complex i can comment out a line of code i'll put the hash symbol in front of my code and here you see it turns green so this is the ide's way of telling me that this is a comment and here now if i hit run you'll see that i get no output that's because i commented out this line of code so here i can bring it back by removing it and now here if i run it once again it shows me what's up so comments are pretty valuable as you're writing code not only to let other people know what's happening in your code and what different blocks of code do but you can also use it when you're troubleshooting to cancel out certain areas of your code until you're ready to test them again next we're going to look at how you can use variables and the easiest way to explain a variable is to use this red bucket that i have here from my son so let's pretend that this red bucket here is a variable and we can name the variable why don't we just call it redbucket just to keep things simple now i can assign a value to this variable so let's take for instance let's say i want the red bucket to equal my name kevin so here i have my name i can take this value and i can assign it to this variable so now if you say well what is the red bucket well right here the red bucket is currently kevin so once again i've assigned that value to it and this is a string or basically my name is referred to as a string it's just a set of characters now i could assign other types of data to this variable as well so maybe i say well actually i want the red bucket now to equal a number so maybe i want it to equal number eight so i'm going to say red bucket you're now equal to eight so i'm going to take my name we're gonna pull that out and here i'll take the number eight and we'll put it in the bucket so now if you ask me well what is the red bucket well it's currently the number eight that's a general concept of a variable the variable can hold different values let's jump in now and see how we can use variables to create variables there are some rules that we need to follow or this is the syntax that python looks for a variable should only contain letters numbers or underscores and also we just need to make sure that we don't start with a number we can't use any spaces in a variable name we also can't use any known keywords to python like break or try and ideally we want to make sure that a variable name is short and descriptive those work best so for example with the red bucket well that was simply called the red bucket so it's pretty short and also descriptive also variable names are case sensitive so if i wrote redbucket with a capital r and another one with a lowercase r python would view those as separate variables so case sensitivity matters okay so let's create our first variable and just continuing the example let's say i want the variable to be called red bucket so i'll give it this name and now i need to assign a value to this variable just like i did with the bucket so i'll type in redbucket and i'll type in the equal sign and i'm going to assign it the value of kevin my name and here i'm using just a single equals sign so what this does is it will assign this value to this variable so now here i can hit enter and let me run this and here nothing shows up yet because i've assigned this value to this variable the next thing we could do is actually print this out so here let me say print and let's type in redbucket what's nice is because i'm using an ide it recognizes that i created a variable called redbucket and i can just click on it here now if i click on the play icon here it'll print out kevin so it's printing out the value that's assigned to the red bucket now let's take another example here let's say i add another line and let me copy this first line and i'll paste it into the second line and here i want to assign 10 to the red bucket so here i start out by assigning kevin and then i assign 10. what do you think we'll print out let's click on the play icon and here it prints out 10. and the reason why is well first it assigns kevin to redbucket and so that's the current value but then on the next line of code it assigns a 10 to the red bucket so the 10 overwrites kevin and then when i print out the red bucket it's taking the last value that was assigned to it so the way to think of this back to the bucket example is i put kevin in the bucket and then when i put 10 in i have to take kevin out and then i put 10 in so at the very end when you ask well what's in the red bucket well the number 10 is the in the red bucket because that's the last item that i put in one thing that's really neat about python is when you're defining your variables you don't have to assign your variable a type so here for example for the red bucket i assigned it my name and that's also known as a string and next i assigned a number which is an integer but i don't have to go and say well the red bucket is a string or a red bucket as an integer it'll automatically know what data type that i'm assigning to that variable so down here in print i can type in type and then i'll open and close the parentheses around this variable and so now let's run this and here it's telling me that it's an integer so basically here 10 is assigned to red bucket and so it's telling me that that's currently an integer let's get rid of this line right here so now kevin will be assigned to red bucket and when i run this here you'll see that a string data type is assigned to this variable now let's say that i want to get rid of a variable so maybe i don't have a need for the red bucket anymore down here i can type in d e l and basically delete and this will delete a variable and here i can type in red bucket again i'll select the suggestion let me get rid of the type in the print so i just want to know what is redbucket so if we run this now i'll get an error because it says redbucket is not defined and that makes sense because here i created the variable i assigned a value here i deleted the variable and here i try to print the variable but there is no variable anymore called redbucket because i deleted it right up above next i want to show you an example of how you can request input and then you can take that input and assign it to a variable so right here i simply typed in kevin and i assigned it to this red bucket but i want to allow input so right up here let's delete kevin or delete your name that you entered in and here we'll type in the input function here i'll open the parentheses and let me type in double quotes and maybe i'll ask the question what do you want to put in the bucket i'll enter a question mark and then an additional space now right down here i don't want to delete the variable so i'll get rid of this so right now i'm going to ask for input and then we'll print out whatever the input is so here let's click on the run icon and this will run it right down here it says what do you want to put in the bucket and so maybe i want to put a shovel in so i'll click down in the terminal type in shovel and then hit enter and here now it prints out shovel because that's now the value of the redbucket variable next i want to show you how we can start using conditional logic in our code basically any expression that you enter in breaks down to either true or false so let's just take an example i'll go down a few lines and let me enter in print and i'm going to ask is 5 equal to 4 one of the things you'll notice is when i ask this question is 5 equals to 4 i entered in two equal signs and that's not a typo up above i just entered in one equal sign so why do i enter one here and down here i enter two well up above i'm taking this value or this input and i'm assigning it to this variable here i want to run a test for equality i basically want to check are these two equal and when you test or check for equality you use two equal signs so here let me remove the code up on top and i'm simply going to print out is five equal to four here when i run it here we'll see that no it's false five does not equal four now you might be saying well of course five isn't equal to four it's not equal to so over here i can also check are they not equal to one another so here instead of the equal sign i'll insert an exclamation mark so here i have an exclamation mark followed by an equal sign so this is my way of asking the computer is five not equal to four and i think we all know that's true so when i run it here i get true back along with equal to and not equal to there are many other operators that i can also use i have less than greater than greater than or equal to and less than or equal to and here you can see the symbol that you can use for these different operations and over on the right hand side you'll see a few examples of how you could use this so i could insert any one of these into that print statement and you'll get this corresponding result we can now take these different operations and we can combine it with variables that we just learned about and so let's type in an example here so i have a young son at home his name is thomas and he's just three years old so let me create a variable and i'll call it thomas age and it's currently equal to three so i'm taking this value of three and i'm assigning it to this variable now let's say i want to check well is he old enough for kindergarten so maybe i type in age at and i'll type in kindergarten and maybe age at kindergarten is five so i'll enter in equals five so i now have two different variables and i've assigned a value to each of those variables now down here i can check the equality of these so maybe i'll say print and let's check is thomas age is that equivalent to the age at kindergarten and so then i'll hit run and here we'll see that's false because basically thomas age is coming back as three age at kindergarten is five and those don't equal one another so we can do this same type of check except we could do it using variables now with this expression here i'm simply checking is thomas's age equal to the age at kindergarten and it comes back as false but what if i want to do something a little bit more complex what if i want to check well if thomas is less than the age of kindergarten well he should probably be in preschool or daycare and if he is five he should be in kindergarten but if he's older than five he should be in some other class and right now this is just doing one check it's just looking at one point is he at the age for kindergarten the easiest way to think of this is like a flow chart so here i have the current age and depending on his age any one of these different scenarios could be true now i won't be able to do that with the simple expression that we wrote that's just checking one thing if i want to check multiple different criteria this is when i can use an if statement so let's jump back into visual studio code and let's look at how we can use this back in visual studio i'm going to delete this last line and i'll keep these two variables the one with thomas's age and the other one with the age at kindergarten and i'll enter down a few lines once again the white space doesn't matter so don't worry you could enter down a few lines and now we want to enter in an if statement an if statement's really easy to write you simply type in if and here now i can do my check so first i want to know is thomas's age less than the age at kindergarten so here i could type in my variable once again it gives me this helpful suggestion of using thomas age and first i can check if he's younger than the age of kindergarten so let me enter in the age of kindergarten so this is going to be my first check once i finish entering in this statement i need to enter in a colon and when i hit enter this drops me down to the next line one thing you'll notice is it automatically indented the line what will happen is it'll check this and if it turns out that thomas's age is less than the age of kindergarten in this case thomas is three the age of kindergarten is five so this would be true it's going to evaluate this code so whatever code i enter in here it'll go through and it'll execute that so let's test this out and let me type in print so i'll enter in this function and maybe we say something like thomas should be in preschool once i finish typing that in i'll go to the end of this line and hit enter again here you see that it continues to indent it so basically i can type in additional code and whatever i type in in this indented space it'll go through and it'll execute all of this code now i don't have anything else i want to run i simply want to print out this message let's now test this out to see how it works i'll go up here and click on run and here we get the text that says thomas should be in preschool now let's say that maybe thomas is 10 now so here thomas's age is 10 and that is not less than the age of kindergarten so when i run here i get nothing back because it evaluates this and it comes back as false so it doesn't execute this code now there's nothing else here so it simply returns nothing now let's say i want some fallback so let's say if this is false i want to say something else down here i can use else so it'll check this and it'll check if this is true but if it turns out being false then it'll fall back to else so here i'll enter an else a colon and here again it indents out so here i could say well if this isn't true then execute this block of code so here maybe i'll insert another print function so let me type in print i'll open up the parentheses and let me type in thomas should be in kindergarten or another class so i've now typed in my else statement so let's try running it to see what it'll happen so it'll check the first if statement that'll evaluate to false and then it should fall back to else let's test this out so when i hit run here it says thomas should be in kindergarten or another class so this logic is working exactly as we expect it to all right now i have two conditions here i basically check the first condition and if this is false it just falls back to this one but let's say i want to check for additional conditions here i'll enter down and let me get out of this block of code so i'll hit the backspace key and right here i can enter something called l if basically else if so you can go through and check any number of conditions it'll start at the top and when it finds one that's true it'll display that message and then exit but if this is false it'll go to the next one and check that condition and i can add any number of else if's in here so i'll type in l if and maybe for this condition i want to know if thomas is five well he should be in kindergarten so here i'll say thomas age and here once again i want to check if it's equal to the age at kindergarten and remember we don't enter in one equal sign we enter into and here i'll enter in the other variable age at kindergarten so this is going to be my check and if this is true we now want to evaluate some code now just like we did before where i entered the colon let's make sure we enter in a colon here at the end that's part of python syntax i'll hit enter and this automatically indents me here i'll type in print and i'll type in some text that says enjoy kindergarten now i've added this else if statement that's checks to see if he's in kindergarten so this last statement no longer makes sense where i say thomas should be in kindergarten or another class so maybe i simply remove this and i say thomas should be in another class so this will just be my catch all at the end so let's test this out to see how it works so let's say thomas is three and i run it in this case the first condition here is true so thomas should be in preschool and here i get the text down below thomas should be in preschool now let's say thomas is equal to five or maybe he's five years old now and we run it here the first condition is false so it skips over that one it looks at the second condition this l if and here thomas is five and that's the age at kindergarten so here i get the text saying enjoy kindergarten and let's say now that maybe thomas is 10. so here when i run it it evaluates the first statement and well he's older than the age of kindergarten so that's false his age is not equal to the age of kindergarten so that's also false and then it falls back to the else statement and so thomas should be in another class so here we've just tested that this is working just like we expect it to next i want to show you how we can use functions in python and if you've ever used any other programming language before you might know them as a subroutine or a method but it's all the same thing so we're going to use functions and you've actually already been using a function we used the print function right here you see that we've been using print multiple times this is a function you call this function you pass in some text and then it prints it out on the screen so that's just a basic example of a function that we've already been using now the cool thing is we can create our own function so what is a function well it's basically a block of code that you can package together with a name and it does something so in this case any time you call print or input it takes some action depending on what that function is the neat thing is it makes your code more modular and also reusable so here for example every time we want to print something on the screen we simply call this function and then it takes that action we don't have to enter in all the different code associated with printing your text on the screen we simply rely on this function so it makes your code a lot more reusable so let's go through and create our own function right now i'll go through and delete all of this code that we had and let's say just to show the value of functions let me just enter in print and then kevin stratvert has a great channel i'm going to enter this in three times i figure the more i say it maybe the more it sticks and the more you read it you might start to believe it so here i've typed it in three different times and now i'm going to run it here i'll expand the terminal just so we can see it and here printed out three times oh but actually look at that it looks like i spelled stratford wrong stratford has a t in the middle and here i just say stravert now because i'm not using a function for this while i'm using the print function but i haven't created a function to print this out three times so i'd have to go through and i'd have to correct every single stratfor throughout my code to correct this and imagine that you reference something many many many times in your code and maybe you make an error you need to update something it could be a pain to have to go through all of your code to update it so instead you can create a function and i'll show you how we can create a function to print this out multiple times and then we can call it whenever we want okay so let's create a function i'll simply go down a few lines to create a function we have to type in def this basically stands for define so i'll type in def and here we can type in a name for our function and i'm going to call this function let's say print kevin so i've typed in my function name and then all functions need to have parentheses at the end so i'll open the parentheses close the parentheses and right down here once again just like we did with the if statement i'll insert a colon so i now have my function when i hit the enter key here too just like with the if statement you see that it indents it automatically so once again it indents and any of the code underneath here will be part of this function next i want to enter in some code in this function so maybe i'll set up a variable and i'll call this text and let me insert some text here in quotes and maybe i'll say kevin stratford has a great channel so that's going to be my text and here maybe i'll just make the error again where i don't have the t now right beneath that here i could use print and maybe i say let's print the text and i want to print this three times so i'll insert it in three times right here i'm going to delete this earlier example here so we just have the function so i have my function now can i run it let's try clicking on run so i click on run and i get nothing back and the reason why is right up here we've defined a function but we haven't actually called this function so i've defined what should happen when i call this but we haven't called it yet so here i'll go down and i'll backspace so i'm out of this block of code that's associated with this function and here now i can call my function so just like we called the print function here i could say print and let's type in underscore kevin and then i'll open and close my parentheses so right now this will call the function so when python sees print kevin it'll look up to this definition for kevin and then it'll execute this code so let's click on run to see what happens now look at that kevin stratford has a great channel and it printed it out three times oh and look there's that error in here so right up here i could go to my function i could add the t and then i can hit run and it looks like that's now successfully updated in all cases now i could go through and i could call this function multiple times this will really boost my ego so now when i hit run look how many times it's printed out kevin has a great channel every single time i reference this function it'll go back up here it'll look at the function definition and then it'll run this code within that function with a function to be able to call the function i first need to define it before i can call it here i'll remove these two references to the function and so let's take the call of the function and i'll move it to the very top so here i call the function and then i define it so if i hit run now here you get an error because here it says we'll print kevin well it doesn't know about that function yet because it comes up later on in the code so once again i need to make sure i call this function after i've defined my function so typically in code you'll see all the different definitions or all the different definitions of the functions at the top one of the neat things too about using visual studio code is here when i click over in the function and i hover over i can collapse this function so let's say i'm writing code and i want to have a whole bunch of functions that do different things i can collapse it and then that way it clears up my view a little bit and here i could expand it if i need to jump into this function and update any of the code within with this one function that i defined on top currently i don't pass anything into the function and i also don't get anything back from the function if you remember the print example here with print i'm passing in certain text that i want to print so basically i'm sending in an argument or a parameter and i can do the same when i create a function so let's say instead of entering the text here within the function let me actually get rid of this line right here and right up here is part of my function definition i'll type in text so as i define my function it's expecting me to pass in some value so let's now go down here and look at the position where i'm calling my function it says print kevin and here i can now pass in a value or basically pass in a parameter so i'll type in kevin stratford has a great channel so here you'll see as i call this function it's passing in this parameter or this argument when i pass this in here in the definition it says it's expecting some text to be passed in and down below the print is taking that text that i pass into this function so now when i hit run here you'll see the same result kevin stratford has a great channel and this is just an example of how you can pass values into your function next i want to show you how we can put an if statement within a function so here again i'll type in def this will define my function and i'm going to call this school age calculator so i want to basically determine based on a child's age where should they be in school and i want to get two different values i want to get the child's age so i'll type in age as one of the parameters or arguments i want to get and i also want the name of the child now just like we did before let's put in a colon here and then let's press enter here once again you'll see that it automatically indents me and so that shows that whatever code i write here is part of this function and now i want to insert my if statement so i'm going to check first off if the age is less than five once again i'll insert the colon and when i press enter here it indents me even more so now i'm within this if statement so if this evaluates to true we're going to execute this code if it's true i want to display some text so here i'll call the print function here i'll open it up and maybe i show some text that says enjoy the time you're still a young guy you don't need to be in school yet so i've typed in some text now earlier we've just been printing out text but i can also print out variables together with text so here i can enter a comma and then i'll type in the name here i'll type in another comma and let me enter in quotes again and i'll say is only here i'll enter another comma and then i'll type in the variable age so here you can see what it'll do let's say the age is three and the name is thomas it'll say enjoy the time thomas is only three so that's what we'll display if he's under five let me hit enter and here now i want to enter another condition so i'll hit backspace so i go back and here i'll enter l if basically else if and i want to say if age let's say is equivalent to 5 then i'll enter the colon and here maybe i'll say enjoy kindergarten and then i'll show the name so here i'll type in print and then i'll open the parentheses and i'll type in the quotes and here we'll say enjoy kindergarten and here i'll type in a comma and let me type in another comma here and then i'll display the name so here it'll say enjoy kindergarten thomas and lastly i want to add one more condition so i'll hit enter backspace and here i can say else colon and here maybe i'll say print and this one i'll just display text and i'll say they grow up so fast exclamation mark i've now finished defining my function and i have all of the different conditions within the function so here i can enter down and i'll press backspace twice so i exit out of both the conditions and also the function so now here i can call my function so i'll call school age calculator and here i need to pass in two different values i'll pass in the age i'll enter a comma and then i'll type in the name thomas so now if i run this here you'll see that it calls the function and then it goes through all of these different conditions and so here well thomas is less than five he's only three so it says enjoy the time thomas is only three and here we could test it out here i'll change it to five and if i run it here it says enjoy kindergarten thomas and let's say thomas is now 10 and i'll run it and here it says they grow up so fast so here now you see how we can call a function and here we pass in values into that function and it uses those values to run an if statement and to evaluate which one is true next i want to show you how you can also get a parameter back from your function so so far we've looked at how you could just run a function on its own how you could pass values into the function and now we're going to look at how you can get a value back from the function i'm going to go through let's remove this code right here and let's start again from scratch for this one it's just going to be a really simple example i want to know what is your age going to be in 10 years and we'll use a function to do this so here once again let's define a function by typing in def and here the function name will be add 10 to age once i've typed in the function name here i want to get one bit of input into my function or one parameter here i'll type in the age so someone will send in their age and then the function will add 10 to that age at the end of the definition of the function let's enter a colon and hit enter here this automatically indents me and now right now i want to figure out what the new age is so here i'll create a variable called new age and i want to set that equal to the age that's being passed in so i'll type in age and i'm going to add 10 to it and right underneath that now once the function runs through and it does its magic i want to return that value so here i'll type in return and i want to return the new age so i'll select this this will send the value back or return the value once the function is done running here now i want to call my function and when i call the function i can assign that return value to a variable so here maybe i create a new variable called how old will i be and i can set that equal to my function so here i'll call the function add 10 to age and here i want to pass in let's say my son who's 3 so i'll pass in 3. so once again if i run this right now it won't actually do anything it's assigning the output which is 13 to how old will i be but now i need to print it out so right down here let me type in print and i'm going to print in how old will i be and now if i hit run here we'll see that that comes back as 13. now once again just to see how this works here i'm saying how old will i be this is a variable and i'm assigning this value to it which is basically my function of add 10 to age and i'm passing in 3. so here it gets the age of three it takes three adds ten it assigns it to new age and once it's done running through the function it returns the new age so the new age is 13 and it assigns 13 to how old will i be and then here it prints out 13. so that's how it works and that's how you can get or you could return a value from a function next we're going to look at how you can use loops what is a loop basically it allows you to execute a block of code multiple times so think of it this way every single week my wife comes to me and says kevin can you take out the trash and she asks me that every single week so in a sense that's kind of like a loop all right let's jump on the pc and let's see how we can create a loop in python so let's now create a loop and there are two different types of loops one of them is called a while loop and the other one is called a for loop we're going to start with a while loop and it's actually pretty easy to do i'm going to create a variable just called x and i'll assign it the value of 0. and so here now i can type in a while loop so i'll say let's say while and let's say x is less than five so while this value is less than five i wanted to execute some code so here i'll insert a colon we do this with our if statements with our functions here when i hit enter now it automatically indents me so i'm now within the loop and here i can just say let's say print x so i'll enter that in and then right after i print x i want to make sure i increment x otherwise it'll be an infinite loop so here i'll type in x equals x plus one just a really simple example we start out at zero and here it'll say if x is less than five in which case it starts at zero it is so then it prints x and then we add one so now x is one and x is still less than five so it'll keep executing until it reaches four so let's run this to see how it works so here it prints out zero one two three four and then once x equals five well it's no longer less than five so then we exit our loop and it's all done no that's the basics of how you can create just a very simple loop there's also another loop called a for loop so i'll go down right here and let's type in four and this is also pretty simple here i can type in 4 and let's say for x in range let's say 5 through 10. and then here i'll close the parentheses enter a colon and here too i'll print the value of x let me delete the code up above and here what i'll do is it'll go through that range so when i hit run here you see that it starts at 5 and it goes all the way up until 10 but it doesn't include 10. so this is another form of a loop that you can use you can do some pretty neat things with loops here for example i have the variable days and i have an array assigned to it and these are just all the days of the week here i can use a for loop to write out all the days of the week so here i could say for d here i'm creating a variable and i'll say in days so here i'm looking at this variable here i'll insert a colon and maybe i just want to print out the value of d so as it goes through each day of the week and here i can enter run and here it'll go through the variable of days and here it prints out every single day of the week so that's pretty cool now one of the neat things too is you can start using some of the logic that we learned up above in your loops so let's say that i want to know what are all of the days before thursday and once we hit thursday i want you to stop here i can insert an if statement so i'll say if and let's say if d is equivalent to let's say thursday so i'll type in thursday and at this point i want it to stop so i'll type in break so here if we click on run here you'll see it goes through monday tuesday wednesday and then it looks like it hits thursday and at that point we stop the loop so at that point we break and we no longer print any of the other days with loops we can also skip over items so let's say that once we get to thursday i don't want it to include thursday in the list but i still want it to continue and look at friday saturday and sunday instead of using break i can use something called continue so now when i run the code here you'll see monday tuesday wednesday and then it found thursday and we simply want to skip over that item so we added continue so here you see it skips over and then we get friday saturday and sunday so once again you can use logic in your loops you could also have it break and continue so far throughout this video we've been looking at some of the fundamentals and we've been writing all of our own code however one thing you can do is you can build on top of what others have already done using libraries and also modules and there are lots of fantastic libraries that you can use let's say for instance that we want to print out the value of pi it turns out there's an existing library called math we could simply type in import and type in math and right beneath that let's use our standard print function and here i'll type in print and why don't we say something like pie is and then i'll insert a comma and here now i can refer to the math library and i'll type in dot pi so i want to get the pi value back and here as i'm typing that out you'll see all sorts of suggestions of what i could use so here when i click on run now here it'll print out the value of pi so once again when you're programming you don't necessarily have to write everything on your own instead you can build on top of what others have done if you were following along today you might have occasionally run into different errors as you were trying to execute your code and they can really be a pain to resolve i want to go through three of the key errors that you might run into and how you can resolve them so let's take one example here i'm going to type in print and i'll type in hello world now if you've been following along you probably know that every time i type in this function and i type in text i need to include my quotes around this but i haven't done that when i hit run right now here i get an error and this is referred to as a syntax error and the reason why is i didn't follow the rules of the print function i didn't follow the syntax rules for python the best way to troubleshoot these is to simply look through your code to find out where the error might be so here i get name error hello world is not defined so here it thinks hello world is a variable so this is one way where you can look at the error message down here and that will help you troubleshoot up above what the problem is now you might run into another type of error let's say i take 10 divided by zero and then i try to run this and we all know that you can't divide by zero right here i get a zero division error division by zero this is referred to as a runtime error it just fails now you could look down here at the error message to see what it was and that might help you troubleshoot where the problem is in your code and typically when you get an error message you could go to something like stackoverflow.com and you can look up your error message to see if other people have encountered similar errors and that'll help you get down to the root cause and lastly there's another type of error that you might encounter let's say i type in name equals let's say kevin just like we did earlier and here now let's say i want to print and i'll say hello name so let's say i want to print the name that i assigned to this variable here let me insert the parentheses and then i'll hit run so down here it just says hello name and it's not printing out kevin now these errors here these are referred to as semantic errors and these are notorious notoriously hard to troubleshoot and the reason why is because everything is working as it should based on how you programmed it but you're not getting the expected results so for these you typically have to sit down look through your code evaluate is everything working how it should so these will tend to take more time all right well that now brings us to the end of this video hopefully by now you understand the fundamentals of programming using python if you enjoyed this video please give it a thumbs up also if you enjoyed this and maybe you'd like to see more content like this in the future please consider subscribing also if you have any feedback or if you want to see me cover any other video topics in the future please leave a comment down below alright well that's all i had for you today i hope you enjoyed and as always i hope to see you next time bye [Music] you
Info
Channel: Kevin Stratvert
Views: 264,474
Rating: 4.9526124 out of 5
Keywords: kevin stratvert, python, python for beginners, python tutorial, tutorial, how to, learn python, python programming, programming, code, coding, if, elif, else, troubleshoot, troubleshooting, language, programming language, variables, variable, define, function, functions, conditional logic, logic, condition, terminal, visual studio, visual studio code, vs code, microsoft, notepad, text editor, ide, integrated development environment, white, for, loop, loops, library, comments, strings, string, text, operator, test
Id: b093aqAZiPU
Channel Id: undefined
Length: 63min 21sec (3801 seconds)
Published: Thu Mar 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.