Skip main navigation

Chapter 2.1 – Expressions

Expressions
9.4
Hello, and welcome back to chapter two. In this, we’re gonna dive a little more into depth on the atoms, the nuggets, the little pieces that make up Python. And then we’re gonna work through our first real program that has a sort of beginning, middle, and end. So here we go. A big part of any programming language is the syntax for constants. The nice thing about this, it’s kind of instinctive. So like 123 or 98.6 numbers, both integer and floating point numbers, we’ve been using these on calculators, they make a lot of sense to us. The kinda constants that are a little bit different are things like string constants.
47.5
So quote Hello World is a string constant, and we use that so our program can say nice things to people that are using our program. And so we call them constants cuz they don’t change, but it’s kind of obvious. Constants, it’s just a piece of the language. So that’s a constant, what else? Variables and reserved words. So the other thing, other than constants are reserved words. And as I said in an earlier lecture, these are like dog language, blah blah blah class, blah blah blah del, blah blah blah else, blah blah blah lambda. So these are words that when you say them to Python, Python expects them to mean exactly what Python wants you to mean.
89.4
Now, why is this special? It’s actually kind of instinctive, it just means don’t use these for other things. Don’t name functions, don’t name classes, don’t name variables. Places where you get choices to use words, don’t use these words. And most of them aren’t too big of a problem. I don’t know maybe break or in or form maybe. Some of these you might naturally use, most of them are kinda of weird enough for that you probably not gonna use use them as variable names. And so the third atom basically is variables. And you get to pick the variable names and it’s a little weird that you get to pick variable names, but you’ll get used to it.
133.9
And so variable names are places where you’re asking Python to allocate a bit of memory. And stick something in it and you’re chosing the label and so if we take a look at this. When we have this assignment statement and remember that assignment statements always have a direction, right? Just think of these equal signs as having an arrow along them. In some languages, I saw a language that uses kind of an arrow, that uses a less than and a dash as the assignment, I’m like that’s how it should be. Cuz equal confuses us, because equal means something different in mathematics than it does [COUGH] in Python or other programming languages.
173.6
So to look at this very first statement, what we’re really saying is, it’s a pretty complex statement. After a while, you’ll just use it. Python find us a spare piece of memory somewhere and give it a label of x and put 12.2 in it. And remember that, remember all that stuff. That’s one of the things that Python does for us, remember stuff. Then we go to the second statement, says, hey, Python, find some memory. Some spare memory that you got laying around, label it y and put 14 into that, okay? And so that’s how these things work and you choose x and you choose y.
213.7
Now, if we keep on going in this code, and now we have another line and we say, hey, x wait a sec, I already told you about x. x already exists so don’t go in and grab any new memory, but stick 100 in that. So 100 goes in, and it wipes out the old value. So that’s what happens, when this sequence starts, this happens first, second, third. And so the third thing is the last thing and so x ends up with a residual value of 100 in it. So that’s the kind of variables and assignment statement.
246.8
Yeah, so naming rules, you can start variable names with letters or underscores, although we avoid underscores because Python tends to use underscores for its own internal purposes. And the rest of it can be letters, numbers, and underscores. And it’s case sensitive, but we don’t want you to depend on that. So spam our lowercase, Spam with one uppercase and SPAM. These, all are different variable names, But you’re not doing anybody any favor if you think that’s being clever. We tend to sometimes use upper case to signal things which we’ll talk about later, but we tend to use mostly lower case.
284.5
Some applications use what’s called camel case which is a mix, but you tend not to depend on the fact that these are unique because you really are misleading person whose reading your code. But we have some good ones, spam eggs spam23, numbers are fine. It’s fine to start with a underscore, it’s bad to start with a number. It is bad to start with a pound sign although that turns into a comment and you can’t use characters other than letters and numbers in the variable. And so those are all good and bad names. Now, we leave these things into sentences or lines.
322.4
And so these things, this is just another sequence of code and it shows how you can also use a variable on the right hand side. So put 2 in some memory and label it x. Pull that 2 back out and then add 2 to it. So this little thing here becomes 4. And then stick 4 and x and then this is a print function. And the way functions work is they are the name of a function followed by a parentheses followed by another parentheses and then you can print something what you want to print. And so this causes on some output 4 to come out. So print is the function that you call to cause output.
360.3
Now, I emphasize that one of the key things about variable names is that you get to name them. And we have a technique called mnemonic, I think I pronounced that right. And the idea is that when you choose a variable name, you should choose a variable name to be sensible. And Python doesn’t care whether you choose mnemonic variable names or not. And the name that you choose for a variable does not communicate any additional information to Python. Okay, so when you’re a beginning student sometimes if you use variable names that are too good, it’s confusing. So you’ll notice as I write especially in these first two chapters.
398.5
Some of my code uses really dumb variable names and some of them uses really clever ones. So I go back and forth to emphasize to you that name of a variable as long as it’s consistent within a program, doesn’t matter. And Python is perfectly happy. So here is a little code. It’s four lines of code, and start at the top. [NOISE] Run this line and that says find a little piece of memory and label it that thing and stick 35 in it. Find a little piece of memory, label it that thing, stick 12 in it. Pull that first thing out and pull that second thing out, multiply them together and pull them into yet a third variable.
436.9
If you look at these things really closely, they’re all unique and distinct. And you do have to look at them very closely then you print the third thing out. And Python loves this code. Python thinks great. I don’t know why you picked those variables, I do not care. All I care is that they’re unique, Python really has no opinion about these variables. But if you hand this code to a friend and you say what is code doing? They’re like why did you pick such insane variables? Cuz I have to stare at it, and the different between these two things is just the letter z and the letter p.
473.2
So this one we’re putting the p guy out and this time we’re pulling the z guy out, right? And these two just have a difference of those letters I think. I mean you can do it, Python loves this stuff. So Python, this is perfectly understandable code. To human beings, it is not perfectly understandable code. So people aren’t going to like you, but Python is gonna be very, very happy. I can’t draw a happy snake, otherwise I would draw a happy snake. So this is tacky, bad, not friendly. So let’s make it a little more friendly. You could choose variable names. Python looks at these two things as those are the same. They’re totally the same. Python doesn’t care.
519
But now, we can look at it and go, I see. The first variable’s a and we’re putting that a thing there and the second variable is b, we’re putting the b thing there. And we’re multiplying we’re gonna put them in a c thing then we’re printing the c thing out. That’s pretty nice, so this is a little better, right? At least we can parse it and read it and understand what the code is going to do without having to check to see if these two characters are the same or different. So that’s better but it’s not mnemonic, okay? Mnemonic means that we choose a variable name that makes sense for what we’re using it for.
550.9
Again, the variable name in Python looks at all these three things as the same. All Python cares is that this matches this, this matches that, and that matches this. But humans are much happier, humans are much happier. Now, we understand mentally that the first variable 35 was the hours, the second was the rate per hours, and then we computed the pay and we printed it out. So this is really understandable. The problem is for beginning programmers, it sometimes is too understandable, right? It’s too understandable, Right? Python knows about payroll? Because if I name a variable hours shouldn’t Python know that that means hours? And if I name a variable pay, does that mean that Python knows about this?
604.8
And the answer is no, Python treats all these three as equal. Python doesn’t look at the name of your variables and think, hey, you just had a bug, right? So what if for example, I rewrote this code and I said hours times hours. And it is like wouldn’t Python say, hey, that doesn’t make sense, why would you even multiply in a payroll calculation? Why would you multiply hours times hours? And the answer is Python will happily do it, cuz it’s not looking at those words. But a human being is trying to understand what your code is or trying to help you when you’re writing code, debug your code will be very thankful that you chose good names.
644
And now I can look at your code and if I see you say pay equals hours times hours, why did you multiply hours times hours? Didn’t you really mean to multiply hours times rate? So mnemonic variables are only for humans. We will later see things that will confuse you, when I name one variable, friend, And another one friends, And be like, so Python understands plurals. And the answer is, no, Python doesn’t understand plurals, okay? It’s really helpful to read stuff, so that when something has more than one thing in it, you might maybe use a variable that’s got a plural in it. So okay, enough about that, I hope I made my point.
687.5
These are all the same to Python, but human beings certainly prefer the second or the third. And so, but be careful after chapter ten, you’ll start using the mnemonic variables and it won’t be a problem. But in the first few chapters, you have to worry about it. So as I mentioned assignments statements basically are not like mathematics, equal sign mean equality. Assignment statements means arrow and the key thing is you can almost think of this as like there is a little wall there and it completely computes this expression. This is an expression on the right hand side, gets that down to a single variable and then writes it into the memory location.
729.4
And that’s why it is possible to have the same variable on both sides. Because this side happens first, ignoring this side and then once this side is done, then it actually puts it into that other side. And so if, for example, x before this statement started had a 0.6 in it. It’s like firewalls this part off and it computes this part here by pulling the 0.6. The old value of x, doing the computation, running through the things, getting the new value. And then after its got that new value, then and only then does it store the 936 into x. And that overwrites the 0.6 that was in x, okay?
775.2
And so one side finishes, produces result, and then it can override. It doesn’t mean you have to put the variable on both sides, but it’s why you can put it. And so sometimes we’ll say something like x = x + 1 and that’s our way of adding 1 to x. And that’s because whatever x is, maybe x is 6 or something, this becomes a 7 and then the 7 goes into x and then it overwrites to to the 7. We call this increments. We’ll see this soon, okay? But that’s why you can put the same thing.
807.1
Now in mathematics x = x + 1 makes absolutely no sense, but in programming it is one of the more common things that we do. Okay, so that gets us through variables and constants. And now, we’re gonna talk a little bit more in detail about expressions.
This article is from the free online

Programming for Everybody (Getting Started with Python)

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now