Variables in Python

Rob Percival
A free video tutorial from Rob Percival
Web Developer And Teacher
4.5 instructor rating • 41 courses • 1,939,693 students

Lecture description

Learn how to create variables in Python.

Learn more from the full course

The Complete Python & PostgreSQL Developer Course

Build 9 projects—master two essential and modern technologies in Python and PostgreSQL

21:39:30 of on-demand video • Updated January 2021

  • Understand software and programming with Python from beginning to end
  • Create Python applications ranging from beginner, basic applications to advanced, useful and engaging applications
  • Learn how to extend Python applications by using a professional data storage which is PostgreSQL
  • Understand advanced concepts extremely useful in jobs and interviews, such as data structures and algorithms
English Hi will come back in this video. Let's look at variables in Python. Think of a variable as a box. You can put anything you want in the box but naturally once the thing is in the box you can't see it anymore. So for example if you put all your kitchen equipment in a box then you no longer know what's inside the box. You just know that you've got a box that maybe has kitchen equipment in it or maybe it has something else and I say this because it's really important for you to know what's inside your box and the way you would do that in normal life would be you would put a tag on the box you would write something on the box saying kitchen equipment for example. So this is the same thing in programming. You've got your boxes you can put anything you want inside them but you need to give your boxes names . And in this case your variables needs to be named Well so that's you know exactly what they contain . So here's what I mean. Let's create a variable called X just type X and then equal something and this something is going to be inside of x inside your box colleagues. In this case I'm going to put the value 5. So X is 5 the number 5. And when I press Enter Python is going to put the 5 inside a box with a name X.. So let's accept that that's happened. And then when we type X Python is going to evaluate x and it's going to get the value that inside it and it's going to give it back to us. So there you go that's the five. And now comes the all important question which is essential. I can't stress this enough how important it is in programming. What is five. What does it mean. Five could be at this point anything it could be 5 apples it could be five years. It could be five blenders. It could be anything. So it's very important that you don't name your variables X but you name them something useful something self-explanatory so that later on when you see your variable when you see X somewhere in your program you know what it means. What is inside it. So in this case let's say that our variable x is five years. So instead of X I would call it age and I would say age is five. And now you would know that 5 is probably years old or it could be something else it could be months . But at least it's got a slightly better name now. OK. So we've learned how to put a value inside a variable. And now let's look at something we've looked at in the previous video but using variables instead of normal plain values. So what would happen just as a recap. When I do age plus the string years what is going to happen if I type age plus years. What we want is for it to say five years. So what is going to happen. The answer is not what we want unfortunately is of the era and the error is in saying unsupported Opteron pipes for plus end and string the operand in mathematics are the different values that you are using . And the plus is the operator and that's the symbol in the middle. And what it's telling you is that you cannot use the plus symbol for an integer and a string or a whole number and a string and it doesn't know what to do with the plus symbol. The plus symbol doesn't have any meaning for these two separate distinct types. So what do we do about this. Well that is something we're going to be looking at in the very next video. But before we do that let's find out how we can convert these five years into a number of seconds. And the reason I want to do this is because the program we're going to be building in this section is . And year to second converters so the user is going to give us their age in years and we're going to give it back to them in seconds just to get acquainted with Python and learn a bit more about this. So how would we convert this age variable into a number of seconds. Let's start small before we are able to convert between number of seconds we need to be able to convert it to a number of days and then days can turn to hours hours contard to minutes minutes and turn to seconds. How would we calculate the number of days in five years and you can ignore any leap years. And the answer is you would do age multiplied by 365 because there's 365 days per year and that would give you five times 365 which is the number of days in five years. So knowing that how would we calculate the number of seconds. Well you've got your age which is the number of years times 365. So that gives you days. And now what would we want to do. Multiply it by 24 which is the number of hours in each of those days and then 60 which is the number of minutes in each of those hours and then 60 which is the number of seconds in each of those minutes. And when we do that we get a long number which is the number of seconds in five years. And this is going to be important because we're going to be using this in order to convert the user's age when they run our program to the number of seconds. And also in the next video we're going to be looking at exactly how we can fix this error and make sure that we are not getting these unsupported operand type errors. OK. So I'll see you in the next video.