1. Home
  2. Explore
  3. Computer Science
  4. Python Programming Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Python Programming Basics

Transcript

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. Now, 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. OK, so you want to learn how  to program, but why Python? There are so many different programming  languages out there, you have C, C Sharp,   C++, 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% 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  learn 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 a spoken language. So, in English I say hello,  in German I'll say hallo,   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. 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 --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’s 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 –version, hit enter, and you should now see that  Python is installed. Here I have Python 3.9.2. OK, 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 type in code  directly here and Python will interpret it. So, what's an example of that? So, what if we type in what is 1 + 2 and I'll  put a question mark in and let's hit enter. And this doesn't work. 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 1 + 2? 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  1 + 2, well I could just type in 1   and I'll type in + 2, and then I'll  hit enter and there gives me 3 back,   so this was a valid expression in  the Python programming language.

Now, what's neat is when I type something  into Python, like 1 + 2, Python takes each   line of code that I enter. So the 1 + 2,  it interprets it, and then it evaluates it. So, it gave me 3. 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 to. Now, now that we've typed in just a very  basic expression, and we've evaluated it, 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 parentheses,   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, and 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 .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 .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 .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 .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.  Now of course it does have some downsides. You don't have a spell check. You don't have a 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 can 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 RubyMine,   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.

I’ll 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’d 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 this 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 control, shift, and P at  the same time, so that's control, shift, and P,   and that'll open up this screen right here and  up here, type in Python: 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 up 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 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 1 + 2 and I'll hit enter and here it interprets  that code, and it evaluates it to 3. So just like we saw earlier. One of the  things is you'll notice when I typed in 1 + 2,   I didn't include any spaces and spaces or  whitespace don't impact the calculation at all,   so I could also type in 1, space, and I'll type  in plus, and then another space, and hit 2,   and then hit enter and there too it also evaluates  it to 3, 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 – 2,   I can also do subtraction, and here  it evaluates that to 3 as well. I could do a multiplication, so let's say 4 * 4. Now with multiplication, I enter in an asterisk. 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 / 5 and   here for division, division is the forward slash,  and then I'll hit enter and that evaluates to 2. 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 + 2  and then I want to multiply by 5. What do you think this will evaluate to?

Is it 2 + 2 which is 4 and  then times 5 which is 20? Or is it 2 * 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 is 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 2 + 2.

Here I'll click on run and here  you'll see that it executes this   code Thanks for the suggestion! I’ve added this  to my list. Stay tuned. Cheers!and here this also   evaluates to 4. If I want to use an exponent,  I'll come up here. Let's remove the plus symbol,   and here I'll enter 2 asterisks, so  this will be 2 to the power of 2. And here if I click on the play button,  here that evaluates to four 2 * 2 is 4. But let's say I want to do a 2 to the 3rd. Here I'll evaluate and it comes out to 8.  I also have access to the floor operator,   so let's take an example here. Let's say I  want to calculate 5 / 2, and here I'll play   and right down here I see that's  2.5, so I have a remainder of .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 2.

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,000,000. 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,  whitespace 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 I'll 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. Now 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 could 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 red  bucket, 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 #8. So, I'm going to say red bucket, you're now  equal to 8, so I'm going to take my name. We're going to pull that out, and here I'll take  the number 8 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 8. 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,  we'll now simply call the red bucket,   so it's pretty short and also descriptive. Also, variable names are case sensitive, so if  I wrote red bucket with a capital R and another one with a lowercase r, Python would view those as  separate variables, so case sensitivity matters. OK, 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. I'll type in   red_bucket 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 could 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 red bucket. What's nice is because I'm using  an IDE, it recognizes that I   created a variable called red bucket  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 will 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 Red bucket,   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 is 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 del, 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 red bucket? So if we run this   now, I'll get an error because it says red bucket  is not defined and that makes sense because here I   created the variable, I assigned the value, here I  deleted the variable, and here I try to print the   variable, but there is no variable anymore called  red bucket 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 red bucket 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 5 equal to 4.

Here when I run it, here  we'll see that no, it's false. 5 does not equal 4. Now you might be saying, well  of course 5 isn't equal to 4. 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 5 not equal to 4? 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 5, so I'll enter in equals 5,   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 5, 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’ 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 at kindergarten, well,   he should probably be in preschool or daycare.  And if he is 5, 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' 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 is  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’  age less than the age at kindergarten? So here I can 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 at kindergarten,  so let me enter in the age at 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 will check this and if it  turns out that Thomas’ age is less than the age   at kindergarten. In this case, Thomas is 3, the  age of kindergarten is 5, 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’ 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 fall back, 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 will happen. So, it'll check the first if statement,   that'll evaluates a 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. Right now, I have two conditions here. I basically checked 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 elif, 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,

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent