Skip main navigation

New offer! Get 30% off your first 2 months of Unlimited Monthly. Start your subscription for just £29.99 £19.99. New subscribers only. T&Cs apply

Find out more

Chapter 2.2 – Expressions Part 2

Expressions Part 2
So welcome back, now we’re going to continue on the right hand side of assignment statements, and talk a little bit more about some complexity about how, what you can do on this expressions. So, all programming languages have various operators. Where you say x plus one, or x minus one, or something like that. These operators historically come from the kind of characters that were on computer keyboards in the 1960s, literally. These things called teletypes came from the end of World War II. And they had a certain set of characters. And we were going kind of from mathematical character set, where multiplication is a big cross or a dot in the middle and exponentiation is raising a little tiny number above it.
And we just couldn’t represent those on these really rudimentary key punches or rudimentary terminals. So we had to map mathematical formulas and functions into what could be done. And so those keyboards from you know 40 years ago, 50 years ago, they had a plus on it, so plus is a plus. They had a minus on it. But multiplication, which is normally cross, is star. And division, which you can’t put things on top of one another, so you just made a slash for division. And raising to the power is double star. Now, the remainder operator is something that’s not typically on calculators, but it’s really important for computers. And we’ll take a look at this. So the addition operator, pretty straightforward.
I’ve kind of been using that without even talking about it. The multiplication and the division. So this 5280 / 1000, which gives me 5.28. But the one that’s probably the most interesting is this modulo or remainder operation. So the way the remainder operation works is. In particular, if this is an integer, is it does the division? So it’s almost like a division, J / 5, but instead of giving the quotient, this is the quotient, it gives the remainder. So 5 / 23 is 4 remainder 3, the 3 is what you get back. Now you might ask yourself, why is this useful?
So one of the ways to make this useful is to pick a large random number and then use the modulo operator, the remainder operator, with 52 and then you end up with a number between 0 and 51 and then, you can pick a card. So you can take a random number but you modulo at 52, and now you can have a random number that is of card, or if you wanna roll a dice. You’d make a big random number and you’d take at modulo 6 and it tells you what side of the dice. So I like games, and stuff.
There’s other situations where you can do even on odd calculations like is this an odd number or an even number, divide it by two and see what the remainder is. And so, so this whole notion of a modular operator, it’s really useful, in some situations, and so that’s why we obsess about it, a little bit. The power operator. This is 4 x 4 x 4 which is 64. So those are the numeric expressions. Now, these also have an order of evaluation, and the way it works is, and in mathematics, there’s order of evaluation. There are some operators that are more powerful than other operators. And you can always, if you’re clever, just put parenthesis in.
And most programmers always put parenthesis in. So if I was writing this line of code, and I want it to be friendly to you so that you could read it more easily, I would simply put the parentheses in for you. So I’d say, 5 to the 6th power goes first, then this 4 divided by that goes next, then this 2 * 3 goes next, and then we evaluate the rest of these things left to right. So I just added the parentheses that are the same this is exactly the same as what it would be without the parentheses, because this happens first, this part here happens second, this happens third, and then all this other stuff happens fourth.
So there is an order. But, we’re going to teach you what the order is, if you weren’t going to use parenthesis.
So the rule is parenthesis overwrite everything. Exponentiation is the most powerful, multiplication and division and remainder are equal. They’re the next most powerful in addition and subtraction. And then when in doubt, you just go left or right. So if there’s a bunch of additions, you just go left to right. If there’s an addition and subtractions and a bunch of stuff, you go left to right, okay. So, this is a classic exam question on computer science homework that you just say, okay, what does this evaluate to? Now of course, if you have Python you just type it in and it tells you what the answer is.
But, so the way I used to do this back in the day when I was doing these homeworks, cuz I would write the expression down on a piece of paper. And then, I would look through it, and I would figure out what the first thing was and I’d be very careful about this. I’m like, okay, exponentiation happens first, so, I’ll do the 2 to the 3rd power which is 8 and then I’ll rewrite the whole thing. 1+8 / 4 * 5. And then I would forget about this one, and I’d look here, and go like okay, what’s the first thing here. Well, 8 / 4 is the first thing there.
So then, I’d be like, 8 / 4, well that gives us 2. In this case, it gives us 2.0. And then I have a 1 + 2.0 * 5. And I look and that’s the next one. So that ends up being actually, this ends up being 10.0 and that ends up being 11.0. And that’s why this prints out 11.0. And so if there are no parentheses, you can figure out. Another kind of exam question is to ask, what the parenthesis would be? So the exponentiation happens first. The division happens second, and then actually you put another parenthesis because, that result is multiplying by five.
So that’s another way to kind of answer the same question, is say, put the parenthesis in where they belong.
Like I said, parentheses first, power second, multiplication third, addition, and then left to right. So when you got nothing but addition and subtraction you go left to right. When you got nothing but multiplication and division you go left to right, start with the left most one. Sometimes it doesn’t matter. In addition and subtraction, it generally doesn’t matter. But in multiplication and division it can matter, left to right.
Okay, so we’ve been talking about variables, we’ve been talking about constants, and we’ve been talking about expressions. But we also have constants that are integers, we have constants that are floating point numbers and we have constants that are strings. And we can manipulate these, and Python carefully tracks not only what the value in a variable is but what kind of a value it is. So is it a string? Is it integer? Is it a flowing point? And sometimes this makes a difference. And here’s a little example code. We have this + operator and the + operator is looking at its two upper ends 1 and 4.
It’s like, those are integers, I know what to do with integers that means addition. And so it adds this to be 5 and than sticks it in to ddd, ddd and out comes 5. On the other hand, we can actually use this same plus operator concatenating strings. Because the plus operator looks to it’s left, looks to it’s right. Says, those are strings, and I know what you mean. I think you mean to concatenate this. And so then we end up with, hello. This space is very carefully constructed right there, cuz the + doesn’t add a space. So this ends up being hello, space, there. And that little space is the space here.
And so Python knows the type of, in this example, these are all constants, but it knows the type of constants, and it knows the type of variables, and it can do very different things. And it can blow up and be unhappy, as we’ll soon see, based on the type of things. So here we have an example of something that’s unhappy. So I have now concatenated hello and there, and Python’s perfectly happy to do that, and then I stick that in eee. And now I say, I wanna add one to it. It looks to it’s left, looks to it’s right, this is a string and this is an integer. And Python says, I don’t know how to do that.
Python could know how to do it, but it chosen not to know how to do that. And so, you get the dreaded Traceback. And traceback is like syntax error. It’s not Python telling you that you’re a bad programmer or that you’re never gonna be a programmer, or that you’re completely unsuitable for a programmer. What Python is saying I, Python, am lost. You told me to do something I don’t know how to do. You need to go remember what it is that I’m capable of doing as Python. Please come back and fix it. It also means that the program stops.
Meaning that, if you’re in a multi line script and you’re doing a bunch of stuff and there’s a trace back here, the code after that quits, okay. And that’s because you’ve hit this line, Python is lost, and Python is loath to continue. Now, we’ll see in a bit that you’ll be able to force it to continue if you want. But because Python, because you said something that Python didn’t understand, it just quits at that point. And traceback line one in this case, tells you where it is that this thing blew up.
And so this looks like nasty gibberish but after a while, it won’t take you long at all to just relax, look for the word traceback, that means, Python quit somewhere. It tells you where and then you look a little bit further, and it tells you what’s wrong, type error. It’s unhappy with the type and it’s still referring up to this line of code. It says, I can’t convert integer objects to string implicitly. So it’s like, you have told it to do something combining a string and integer. And Python is like, I am lost. But I’m giving you as much clue as to how I got lost. What got me lost? Where I got lost?
I got lost where I got lost, and what caused me to get lost. And Python is trying as hard as it can, to clue you in to what’s going wrong. So don’t look at this as like bad. Look at this as like, I’ve not quite communicated the Python the way I wanna communicate. There’s a way to solve this, there’s a way to do something here. And so if we take Python and we think about Python, say hey, you’re so picky and you blow up if I do the least little mistake on types, help me out here. And so it turns out that Python has a built in function called type. So again, it’s type parenthesis.
Function calls that parenthesis on them, and we pass something in. And we’re like, hey, Python, tell me what the type of the variable eee is. And Python prints out, it’s a string. What is the type of the constant quote hello quote? That’s a string. What is the type of the constant 1? That would be an integer. And so, Python keeps track of the value of variables and constants, the value and the type of it. And so we have to kind of manipulate this and be aware of this, as we move forward. And so there’s several types of numbers that we’ve already sort of been implicitly playing with.
Like I said, you can give a variable to type, or you can give a constant to type. Variables and constants that don’t have decimal places are integers, what are called integers, and variables that have decimal places, are called floating points. Even if this was a 98.0 it would still be a floating point. As soon as you put the point there then that means it’s a floating point. They are represented internally differently. Floating point numbers have a greater range than integer numbers, but they’re not always as precise as integer numbers. So there is floating point numbers have more range.
And less precision.
So, integers are perfect but there’s a limit, like 4 billion, or 4 trillion, or something, there’s a limit. Don’t worry too much about that. There’s a choice that you make. You tend to use floating point numbers for things like temperatures, or speed. It turns out that you don’t use floating point for money. You don’t use it for money. Even though [LAUGH] in our first examples we’ll be bad and we’ll use it for money. But shh! Don’t tell anybody that I’m using floating point for money. If you want, you can play with floating point, and start finding some things where money goes bad.
Okay, there’s actually a number of different movie plots that come to have to do with computer programs that use floating point for money. I think Office Space is one of them that I particularly like, but there’s other ones as well. Why you don’t use floating point for money. I kind of got off track on that but that’s okay. So we also have a set of built in functions that can convert from one type to another. There’s float, there’s int, and so float is a function and again has parenthesis, we passed in 99.
So as this expression is being evaluated, it has to call float, it passes in 99, and then what comes back is 99.0, which is a floating point representation of the number 99. 99 and 99.0 are not the same, this is a floating point number. So when this addition happens, it produces a 199.0. So that is a floating point a 199.0. You are forced converting by putting this calling float, you are force converting to a floating point number. If we look at like i is 42, we ask what type it is. Well that i is an integer. We can take a variable and pass it into the float function and get back 42.0. That 42.0 comes back.
And we say what kind of thing is in f? Well, f is a float. Right, so you can see how we’re manipulating the type of things and controlling them. And any time we get a little confused, we use the type function and say hey, what’s going on here? Why don’t I understand what’s going on here? Integer division is something that actually changed between Python 2 and Python 3. It’s one of the bigger changes, the bigger non-upwards compatible changes between Python 2 and Python 3. And so, this I’m just reviewing. There are somethings that were different in Python 2. So, 10 / 2, even those are both integers, produces a floating-point result.
This makes, kind of doesn’t make much difference if these are evenly divisible, but if they’re not evenly divisible. It makes a big difference, so 9 / 2 is 4.5. Think about like if you put in 9 / 2 in a calculator, you wouldn’t expect it to say 4, and in Python 2, it said 4. It actually truncated, it actually through this bit away, but in Python 3, it just automatically converts divisions to floating point. And so it works out quite nice, okay? So 9 / 2 is 4.5. In Python 2, this used to be 4. And then 99 over 100 is 0.99, exactly what you’d expect in Python 3. In Python 2, that was a 0. Why?
Because it truncated, it didn’t even round the numbers, it chopped them off. So this was really quite a silly, artifact of Python 2. In Python 2, if you get stuck in Python 2, you just use floating point numbers. And, once either side of the division, or any other operation is a floating point number, then the calculation is done in floating point. So, if it’s floating point input on either side, then its floating point output. And this is what we used to have to do kind of in Python 2, is force things to be floating if we’re doing divisions.
It wouldn’t hurt to do it in Python 3, but now integer division in Python 3 makes a lot more sense, thank heaven. Okay, string conversions. So if we read data, which we’re going to see in a second, from the outside world, it comes in as strings. Whether we’re reading from a network or from a database, we tend to get these things as strings. And so. What we’re showing here is a string value that’s 123. But it’s not really 123. It’s three characters. It might as well be abc. So 123 are the first three characters. So we take this string constant, we put it in there. And we say okay what kind of thing is in there? It’s a string.
If we try to add one to it, as we saw before, you add a string and an integer, and Python gets really unhappy. Can’t convert a int object to string implicitly. But what if we know that inside of this string are actually digits and we want to get an integer representation or a floating point representation of that? Well, in that case, we can call the int function or the float function, passing in a string and getting back an integer. So this basically reads these characters, the 123, and goes, that’s 123, and gives us back 123. And we say, what kind of thing is in there? Well now, in ival, there’s an int. Now I’m being mnemonic here.
I’m remembering that this is an integer and this is a string, but Python doesn’t care what I name my variables. Remember, Python never cares about what I name my variables, so if I start naming them conveniently, don’t all of a sudden think that everything that starts with a prefix i is an integer and everything that starts with the prefix s is a string.
It’ll be a number of lectures before I stop reminding you of that.
So ival ends up with an integer, the number 123. And now we can indeed add 1 to it, cuz it’s an integer, and Python’s happy, and out comes a 124. So this works pretty well, unless the string in question has no digits.
So there are no digits, it’s gonna blow up. It’s like, whoa. Now, let’s read it. Traceback means I quit. Where? Line one. It’s always line one cuz we’re in this interactive environment. But if you were in a script, it would tell you what line it is. And then it says invalid literal for int with a base 10 x. And that’s like okay, it’s not working very well. And so, say that’s an invalid letter. And again, we’ll find ways to cope with this in later lectures, okay. Now, how do we get data from the outside world? So this is the keyboard, eventually, we’ll talk to networks and databases and files.
But right now, we want to take the keys from the keyboard and get into a variable. So we have another function, a special function, the input function. And when Python comes running in here, it starts the input function and the parameter to the input function is what’s called a prompt.
It prints out, who are you? And then it waits. And then we type into the keyboard, Chuck, and then we hit the Enter key and then it takes this string right here and then it puts it in this variable right here. It is a string even if I typed in 1234, it would be the string 1234, not 1,234, okay? And so that is the way. And so this program, at this point, pauses until we type and we hit the Enter key, and then it takes that line of input, including spaces, whatever I’ve typed, and puts it in that variable.
And then the program continues, and in this case it just prints out Welcome, and then the contents of the variable nam.
And in this case the space right here that you see between Welcome and Chuck. That is caused by this comma. So we’ve mostly seen print being able to printing one thing. But you can have as many things as you want with commas and print. And every comma adds a space. And so it’s kind of friendly. And if you wanted to you’d have to concatenate these things together to eliminate that space. But most commonly, you’re printing out a list of things you probably want spaces between it, okay? So that’s how we read input. Up next, we’re gonna combine all these things, and we’re gonna make our very first program that does, actually, something useful. So we’ll see you in a bit.
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