0:00

[MUSIC] Well, it's great to see you here in class again.

Â We're getting close to being half way through Week One.

Â Right now, you should start to have a feel for what it's like to program in Python,

Â you know, about expressions, you know, about variables, you know, about

Â functions. But really the number of operations that

Â you have at your disposal when building a Python program is fairly limited.

Â So, what I'm going to do in this lecture is I'm going to tell you about a few more

Â operations that you have at your disposal to help build more interesting programs.

Â In particular, I'm going to talk about remainders in Modular Arithmetic.

Â I'll talk a little bit about how to work with strings and numbers.

Â And then, I'll finish it off by telling you to get access to some more built in

Â functions inside Python. Okay, let's go to it.

Â 0:52

So, let's talk about a few more operations that we can use in Python.

Â For the first part of the lecture, we are going to talk about Modular Arithmetic.

Â Now, you may not have heard that phrase before, but you actually know how to do

Â Modular Arithmetic. In third grade, you learned how to do long

Â division. You had a dividend and a divisor, and you

Â divided the, divided one into the other. What came out was a quotient and

Â remainder. So, that remainder is actually,

Â essentially your computing Modular Arithmetic when you compute remainders.

Â So, I'm going to give you a few examples illustrating how to use Modular

Â Arithmetic. So, let's start of with a really simple

Â problem. Let's say, we have a 2-digit number, and

Â we'd like to compute its tens digit and its ones digit.

Â So, how can we get the tens digit? I think it's not too hard, we actually

Â have something that computes the quotient, it's the energy division operator slash,

Â slash. So, we could say num energy divided by ten

Â so, ten goes into 49 four times, so I'm going to predict we're going to get out

Â the number four. Sure enough.

Â Now, we'd like to also get the ones digit. So, how can we do that?

Â Well, we need to compute the remainder. Whenever we take num, we take the

Â remainder with respect to ten. So, let's see if we can find out what the

Â remainder operation is in Python. Let's go to CodeSkulptor Python.

Â And we're going to squeeze down here. Let's see.

Â Integers and floating-point numbers. Let's scroll down.

Â Hm. Modular Arithmetic that sounds kind of

Â plausible. And sure enough, that's actually what

Â we're going to use. We're going to use the send operator to

Â compute a remainder here. So, we could say, print tens. Now, we can

Â print out the ones digit also and we get 49.

Â And notice we can always reconstruct the original number by just taking ten times

Â the tens digit plus the ones digit. So, there's nothing special about 49 here.

Â That actually works no matter what. Alright, let's talk about another example

Â of Modular Arithmetic using remainder. So, this is the classical example that

Â you'll see, it's called clock arithmetic. And the idea is we're going to want to do

Â computations involving hours, and either a 12-hour or 24-hour clock.

Â So, let's pop over to one note here. And what you can see right here is I might

Â have kind of two tables. The one on the one on left here.

Â This is the 12-hour clock. And so, we list all the hours in the day

Â on the 12-hour clock. We have twelve hours that are a.m., twelve

Â hours in p.m. Notice that the hours start at twelve,

Â And one, two, three, four, five, six, seven, eight, nine, ten, and eleven.

Â If we'd like to do arithmetic on the 12-hour clock, we can almost use remainder

Â to do that. So, let's go down here and consider a

Â problem. Right now, in this clock, I have listing

Â the time as I have the time listed as eight:00 p.m.

Â So, here it is eight:00 p.m. Now, I would like to skip and figure out

Â kind of eight hours ahead what the new time would be.

Â So, let's go down here and we'll just write hours and I'll draw very carefully.

Â 4:09

My handwriting is not so great so I'll try to go slow here.

Â So, we go ahead, we end up here, And where do we end up?

Â We end up at four:00 a.m. And what do we do up here?

Â We went plus eight hours. So, we could do that using remainder.

Â We could take eight plus eight would be sixteen, I take this remainder twelve,

Â that would be four. But now, it's a little awkward here

Â because we have to change a.m. to p.m. and the big problem is that in some sense, if

Â we end up with twelve and we take the remainder twelve we get zero.

Â But, in this clock, kind of zero is twelve.

Â So, the, the 12-hour clock is not a great clock for kind of doing power arithmetic.

Â So, there's another clock, and it's preferred by the military, for probably

Â this is one of the reasons why, it's called the 24-hour clock.

Â There's no a.m. And p.m.

Â The, the starting hour is at zero, it's 0000, and it's 0100, 0200, all the way up

Â to 2100, 2200, 2300. And so, in this model, we can do clock

Â arithmetic in a much cleaner way. So, I have an example over here.

Â 5:28

There are 2000. What do we want to do? We want to go ahead eight hours.

Â So, we drop that real quick. So, I can go here, and that would be I

Â guess, 0400. So, we'd like to go for eight hours here.

Â So, we could do that, we could say, plus, plus eight hours.

Â And so, what can we do? We can use modular, Arithmetic to do this.

Â And this is actually is much cleaner because we could take twenty plus eight,

Â that's 28. We can take a remainder 24, that's four.

Â So, we can really use Modular Arithmetic and remainders to actually do, essentially

Â computation of hours on the military clock very easily, the 24-hour clock.

Â So, let's go back and do that in CodeSkulptor real quick.

Â So, let's pop back over here. So, here we have hours twenty, so we're at

Â 2000.. So, that's military time.

Â And we like to go through and actually figure out what the time will be after we

Â finish an 8-hour shift. So, we could do that, we could say print

Â hour plus shift, remainder 24. So, if we print that,

Â 7:22

Okay. Let's look at one more application using

Â Modular Arithmetic. And this will be something she'll actually

Â do fairly often in the class. You're going to have a 2D game.

Â Objects may be moving around. And you have to deal with what happens

Â when an object gets near the edge of the canvas.

Â Maybe it's going to bang off of it. Maybe it's going to come to stop.

Â But sometimes, you'd like it to actually do maybe something where it wraps around.

Â It goes off one side and comes on the other side.

Â Let me just show you a quick little demo. Here's a part of a game you're going to

Â actually build later on. This is a spaceship.

Â And our spaceship starts flying, and we get close to the edge of the screen , and

Â what's going to happen? Well, it pops on the other side of the

Â screen, it wrapped around. If I go off the top of the screen,

Â I wrapped around. I go back on the bottom of the screen

Â after I round the top. So, how do we model this mathematically?

Â You could store kind of some, some information to represent the, the position

Â of the center of the ship. But when we go off one edge of the screen,

Â we better change that position so that it's kind of in a different position way

Â on the other side of the screen. So, we can use Modular Arithmetic to do

Â that. So, let's go back and look at this. So,

Â lets see where the situation where we are keeping track of the horizontal position

Â of the ship and so we're at this, the frame that I created was 800 pixels wide.

Â So, we are getting close to that right-hand edge, and we say, we need to go

Â right five pixels. How can we do that?

Â Well, I claimed we can do that using remainders, we can say, position is equal

Â to, well, the old position plus whatever the move was, we could do that remainder,

Â The width of the screen, and if we print out position what's going to come out is

Â two. The new, the position of the ship after we

Â move five units right has not overpassed the right-hand side of the screen.

Â The screen is actually over the left-hand screened, side of the screen it positioned

Â to. So, we've popped from the right-hand side

Â to the left-hand side. The interesting thing is this also works

Â the other way. And to do that, you have to understand how

Â a remainder works on negative numbers. If we take the remainder of something

Â that's negative. What happens is we're always going to get

Â a number back in the range from zero to width. Zero to the number that we're

Â taking the remainder of respect to. So, what's going to happen is when we take

Â the remainder of something that's negative we're going to get something that appears

Â on the screen, Something in the right range.

Â We'll just do that tangibly. Let's say, we were the other way.

Â Let's say, we were at position two. So, we're going to counter reverse this.

Â We were positioned to, and we did a move of -five.

Â We kind of move to the left. We're on the left-hand side of the screen

Â and we move left, that's going be -five. Run the same code.

Â Well, what's going to come out, we're going to get two minus five is negative

Â three. So, we have to find -three remainder 800.

Â And I told you that's always going to be a number between the zero and the width.

Â So, it comes out as 797. So, we kind of took -three, we added 800

Â to it. It gives a number between zero and 800,

Â it's 797, so we pop to the right-hand side of the screen.

Â So, notice using remainders here actually works, works in both directions.

Â It works when you're moving left, it works when you're moving right, it works when

Â you're going up, it works when you're going down.

Â So, remainder is going to be your friend when you're doing screen wrapping in your

Â games. The next topic that I'd like to cover is

Â talk a little bit more about how to convert back and forth between data types.

Â In the lecture on Arithmetic expressions, we talked about the operator int and the

Â operator float, and how those kind of converted data in other forms into either

Â an integer or floating-point number. There's times when you're going to have a

Â number and you're going to want to convert it into string so you can print it out.

Â your first three projects are going to involve really just doing lots of

Â printing. You won't get into using the canvas and

Â doing drawing until Week four. So, what I want to do here is just talk a

Â little bit about how you can convert things into a string, and so I'm going to

Â motivate it by just talking about a very simple problem.

Â So, we, we talked earlier about 24, the 24-hour clock.

Â And, in a 24-hour clock, you always kind of think about the, the hours, as being

Â two digit even if there's a zero in front of the, even if it's like three:000 a.m.,

Â it's really 03. So, here three:000 a.m. would be 0300.

Â 12:23

So, we can just print those out. We could print tens and ones, let's just

Â see what that looks like. This doesn't look good.

Â Well, I guess we had a zero and a three. Okay, yeah, I'll have, let's do this.

Â Let's print out one more thing, let's print out a string which has the minutes

Â or it seems the minutes are, or even hour. So, if we do that,

Â We get kind of 03:00.. And you can see there's a bunch of space

Â is in there and it doesn't look very good. So, what could we do here?

Â We could try to convert these numbers into strings, that might be the first thing.

Â And then we could think about the other ways to kind of manage the string to get

Â it to something that looks like what we want.

Â So hm, it turns out that there's a function that we can do that.

Â We could convert the number into string using the str function.

Â So, we can try our next attempt, we could say, print str of tens and str of ones.

Â And then we can keep printing our string called 00. So now, if we do that.

Â Well, dang. It's the exact same thing.

Â This is now a string, this is now a string.

Â But it just turns out they're exactly the same.

Â Hm. But now, that there are strings, we can do

Â some things to them that we couldn't do with numbers.

Â So, for numbers, when we use the plus separator on them, we added the numbers.

Â It turns out, Python likes to take common operators, and try to interpret them as

Â being applied to things that you're not used to.

Â So, for example, there's a plus operator for strings.

Â So, what would plus operator do for strings?

Â Well, it joins the strings together. So, what I'm going to do is I'm going to

Â try one more attempt here. I'm going to say, print plus the string of

Â ones, plus and let's add in 00.. Let's try that, to see if anything good

Â happens. So, notice what happened here now.

Â This plus operator joined the strings together, and it didn't throw an error

Â because this is a string and this is a string and this is a string so it's nice

Â to actually take strings and join them together.

Â And most importantly, he didn't put in all these annoying blank spaces in here.

Â This is an example of what's called string form, string formatting, and you're going

Â to get good at it after the next couple of weeks here.

Â But the most important thing is remember this function str. It converts something

Â into the string just like int converted something into an integer and flow,

Â converted something into a floating-point number, str converts something into a

Â string. Let's finish off and we'll do one more

Â thing. I mentioned that there are lots of other

Â functions available inside Python. the way you get at those functions, these extra

Â functions, these are things called modules and they are kind of things that are

Â outside of basic Python that people have built and added into Python and to get

Â access to those functions, you need to go through and import them.

Â So, in Python, you need to say, import module name.

Â So, if you go into the Hello World page, you'll see something that says, Import

Â Simple GUI." Simple GUI is a module that Scott and I built that actually opens up a

Â canvas and draws Hello World on it. There are two common modules that you

Â should know about, that you'll need to use as the class goes on.

Â One is Math and the other one is Random. And you can take a peek at them up here in

Â the docs. So, we can go to CodeSkulptor Python and

Â scroll down. And there you go.

Â There's the Math module. We'll just scroll down there and see.

Â It has kind of all the common math functions that you're used to.

Â If you're looking for a math function that's not built into Python, you can say

Â import math, and go down here and use all these.

Â We'll go back here. For example, I could say print Math.pi and

Â this is print out a very nice approximation to pi.

Â And the other one that you're going to use this week, it's going to be a random

Â module and it gives you the ability to generate random numbers.

Â So, also, it's also described up here so let's go up and close that.

Â Go back down, I look in the Random module, and it's got a smaller set of functions

Â here. But there's some functions that you're

Â going to be using fairly often. The one you'll probably, the two that

Â you'll probably use the most will be it's going to be random.randrage. And so, that

Â one you should take a look at and figure out how it works and you'll be all good.

Â Okay. So, those are some other functions you can

Â use in this week and the upcoming weeks. See you next lecture.

Â