From the course: Python for Non-Programmers

Variables

- [Instructor] Now that we've written our very first line of Python code and we have our environment all set up here. It's time to learn the absolute fundamental building block of Python. And that is variables. So because we're learning a new concept, we should create a new repl. So let's go ahead and click on the repl logo here to get back to the dashboard or home page. And let's now create a new repl. So again, we need to make sure that we have Python selected. And because we are going to be learning about variables, I'm going to name this variables. Now what are variables? Variables simply just hold a piece of information that can change over time. It's really just that simple. They can hold numbers, they can hold texts. And as we get further in this course, you'll learn that they hold other types like booleans. So just to start out here, let's go through an example of, let's say we want to create a variable that represents how much money we have in our wallet. Now, shocker, as of recording this video, I took a look in my wallet. I have $41 in there, so not a ton, not too shabby either, but if we want it to represent this amount of money in my wallet, we would do that in Python, by first giving a name to our variable, which I'm just simply going to call wallet. Okay, this is now the name of our variable. Now I said the variables, hold a piece of information. How do we put some information into this new variable called wallet? Well, we have the name and then we hit space, do an equal sign, another space, and then put in some sort of value. So for me, because I have $41, I'm going to say wallet is equal to 41. Essentially what's happening here is we're taking the value of 41 and putting it into the variable called wallet. And now that we have this code, you're saying, great, let's go ahead and run this and see what it looks like. So hit this run button here and you'll notice on the console, nothing shows up. And the reason for this is that in Python, even if you create a variable and put some information in it, you will not see any of that information on the console until you print to the console. Now, I know the word print typically means that you're going to put some ink on some paper, but in the programming world, and specifically with Python, whenever you print something, you're just getting some information to show up on the console. So let's go ahead and create a few new lines after the term where we create wallet and set it equal to 41. Let's type all lowercase print, just like we did last video and put some open and closed parentheses. But now, instead of putting the words like hello world, or hello Nick inside of there, let's put the word wallet that variable that we just created. So I'm going to say print with parentheses wallet, and now let's go ahead and hit the run button and see what we get. And now you can see that the number 41 appears. So if we ever want to check what a variable is holding, what piece of information it is be it numbers, text, whatever. We have to print out that information to the console. So now that we have this down, I'd like to share a tip with Python. You'll notice that I put a blank space between line one and three, where first we created the variable and then we printed out the information inside of the variable. Why did I do that? For me just stylistically I like to have a little space. And in Python you can have as many or as little spaces in between your lines of code. This will still run completely fine, as well as this. They're all the same. So if you just like to have space in your code, you can use as much or as little as you'd like. Now, one more thing with variables is that the value that they're holding can change over time. So let's say with our wallet, I went and bought some pizza. And so now my wallet only has, let's say, $32 inside of it. I can update the value of the variable. I can say wallet is now equal to 32. Previously it was equal to 41, but because I've spent some money, I now only have 32. And if we want to see what the value of this is, again, we're going to have to do a print to see the new value of wallet. So let's go ahead and hit the run button. And you'll notice in the console when we first printed, wallet was equal to 41, and now it's equal to 32. So, variables hold a piece of information and they can change over time. Now to really cement this concept, to make sure that you've got a good grasp on it. I'm going to introduce a challenge. And for the rest of this course, after we learn a concept, I'm going to have a little challenge for you. And I really want you whenever I present you a challenge, and I tell you to pause the video, pause the video and try it out on your own. It's really going to help your learning and make sure that you're understanding what I'm saying. And you're not just listening and following exactly what I'm doing. So the challenge that I have for you is I want you to make a variable called day and set it equal to the date of the month. So whether it's the 13th, the 15th, the fourth, whatever, make that variable day and set it equal to the date of the month. So hit pause, and go do that on your own now. Okay. Hopefully that went well for you. For me at the time of recording day is equal to the 21st. So I said, day is equal to 21.

Contents