00:00The following content is
provided under a Creative
00:04Your support will help
MIT OpenCourseWare
00:07continue to offer high quality
educational resources for free.
00:11To make a donation or
view additional materials
00:13from hundreds of MIT courses,
visit MIT OpenCourseWare
00:31PROFESSOR: All right.
00:33Let's get started, everyone.
00:37Welcome to the second lecture
of 60001 and also of 600.
00:42So as always, if you'd like to
follow along with the lectures,
00:46please go ahead and download
the slides and the code
00:48that I'll provide at least an
hour before class every day.
00:53So a quick recap of
what we did last time.
00:56So last time, we talked a little
bit about what a computer is.
01:00And I think the main takeaway
from the last lecture
01:03is really that a computer only
does what it is told, right?
01:06So it's not going to
spontaneously make
01:08decisions on its own.
01:10You, as the programmer,
have to tell it
01:12what you want it to do
by writing programs.
01:16So we talked about
simple objects.
01:18And these objects were
of different types.
01:22So we saw integers,
floats, and Booleans.
01:25And then we did a couple of
simple operations with them.
01:28Today, we're going to
look at a different--
01:30a new type of object
called a string.
01:33And then we're
going to introduce
01:35some more powerful things
in our programming toolbox.
01:42So we're going to look at how
to branch within a program,
01:44and how to make things-- how
to make the computer repeat
01:47certain tasks
within our program.
01:51So let's begin by
looking at strings.
01:53So strings are a
new object type.
01:56We've seen so far
integers, which
01:58were whole numbers, floats,
which were decimal numbers,
02:02and we have seen Booleans,
which were true and false.
02:05So strings are going to be
sequences of characters.
02:09And these characters
can be anything.
02:11They can be letters,
digits, special characters,
02:17And you tell Python that you're
talking about a string object
02:20by enclosing it in
quotation marks.
02:23So in this case, I'm creating an
object whose value is h-e-l-l-o
02:32And Python knows it's a
string object, because we're
02:34enclosing it in quotations.
02:36They can be either double
quotes or single quotes,
02:38but as long as you're
consistent, it doesn't matter.
02:41And this object, we're binding
it to this variable named hi.
02:46And we're using that using
the equals sign, which
02:49is the assignment operator.
02:51So from now on, whenever we
refer to this variable hi,
02:55Python is going to say, oh,
I know what the value is,
02:57and it's that string
of characters.
03:00So we're going to
learn about two things
03:02that you can do on strings
today, two operations.
03:05One is to concatenate them.
03:07And concatenation is
really just a fancy word
03:09for using this plus
operator, which
03:11means put the strings together.
03:14So I have this original
variable named hi,
03:18and I create a new
variable called name.
03:20And in it, I'm going
to assign the string
03:24a-n-a to the variable name.
03:27And when I use the plus
operator in between hi and name,
03:31those two variables,
Python is going
03:32to look at the
values of those two,
03:34and it's going to just
put them together.
03:39I'm going to switch to Spider.
03:43And this is just that
example from the slides.
03:49So let's see what happens.
03:50So I have the variable
hi, the variable name,
03:54and I'm just concatenating
those two together.
03:57And then I'm going
to print that out.
03:59So if I run the code, notice
it prints out "hello thereana."
04:06And there's no space because
the concatenation operator,
04:09the plus, doesn't add
any spaces implicitly.
04:13So again, another example
of just computer just
04:16doing what it's told.
04:17If we want to add a space,
we'd have to actually insert
04:22So that's this
line here, line 8.
04:25And in this line,
we're concatenating
04:27the value of the
variable hi with a space.
04:31Notice we're putting
it in quotation marks.
04:37So if we'll go ahead
and print that value,
04:41notice this was that
garbage greeting there.
04:45And now we have a
proper greeting, right?
04:52So that's the concatenation
between strings.
04:56And then the other
thing we're going
04:57to look at related to
strings is the star operator.
05:04So that's this one
here on line 10.
05:07So Python allows you to use
the star operator, which
05:09stands for multiplication,
between a string and a number.
05:15And when you do that,
Python interprets it
05:19as repeat that string
that many number of times.
05:25So in this case, I'm
creating a silly greeting,
05:29and I'm concatenating the value
of hi, which is "hello there"
05:33with the space plus the name.
05:37So notice here, I'm
using parentheses
05:38to tell Python, do this
operation first, and then
05:42multiply whatever the
result of this is by 3.
05:48So if I print that
out, it's going
05:51to multiply the space
with my name three times,
05:55and it's going to concatenate
that with "hello there."
05:58So that's exactly what
it printed out there.
06:02Last lecture, we talked
a little bit about print.
06:05Today, I'm going to talk about
some nuances related to print.
06:09So you use print to
interact with the user.
06:12It's cool to write programs that
print things out to the user.
06:15So the key word
here being print.
06:19And then you put
parentheses after print.
06:22And in the parentheses,
you put in whatever
06:25you want to show the user.
06:27So in this little
program, I have--
06:30I created a variable named x.
06:31I assigned it the value
1, and then I print 1.
06:38So I'm taking the number
one, the integer 1,
06:40and I'm casting it to a string.
06:43And you'll see why in a moment.
06:46So I want to bring
to your attention
06:47a couple of things here.
06:48So in the first print, I'm
using commas everywhere here.
06:54And in the second
print, I'm using plus.
07:01So by definition, if
you-- you can use commas
07:04inside a print-- inside
the parentheses of print.
07:08And if you use a comma, Python
is going to automatically
07:13add a space in
between the two things
07:17that the comma is in
between, the values.
07:20So "my fav num is"
is the first thing.
07:23And the second thing is
whatever's after the comma.
07:29So if you use a comma, Python
is going to automatically insert
07:34Sometimes, you might want
that, sometimes you might not.
07:36If you don't want that, you
can use the concatenation
07:39operation, the plus.
07:41And you can add all
of your little bits
07:44together to create
one big string.
07:48If you're using
commas, the items,
07:50the objects in
between the commas,
07:52do not all have to be strings.
07:54That's the plus side
of using commas.
07:56But the downside is you
get spaces everywhere.
08:00If you use plus
operator, the plus side
08:03is Python does exactly
what you tell it to do,
08:06but everything has to
be a string object.
08:09So "my fav num is"
is a string object.
08:12You have to convert all of
your numbers to string objects,
08:18So if we look at Spider--
This is the same-- almost
08:30So here, I don't
have spaces anywhere.
08:34So you can see that
the first line here
08:37has commas everywhere.
08:39So I'm going to have spaces in
between every one of the things
08:43that I'm printing out.
08:47This line here is sort of a
combination between commas
08:54So depending on where
I used the comma,
08:56I'm going to have
an extra space.
08:58And this line here just has
concatenation everywhere.
09:02So if I run this, notice this
very first line added spaces
09:08everywhere in between
all my objects.
09:10The second one added
spaces somewhere.
09:11And you can sort of trace
through and see exactly where
09:14the spaces were added.
09:16And the last line here
didn't add spaces anywhere.
09:33So printing things out
to the console is nice,
09:37but the second part of sort of
writing an interactive program
09:40is getting input from the user.
09:43And that's the more
interesting part.
09:45So if you've done problem set 0,
you might have sort of already
09:49tried to understand
this on your own.
09:52So the way you get
input from the user
09:55is using this command
function called input.
10:00And inside the parentheses,
you type in whatever you'd
10:03like to prompt the user with.
10:07So in this case, in my
example here, I have input,
10:11and then here I said
"type anything."
10:14So the user is going
to see this text here,
10:16and then the program
is just going to stop.
10:19And it's going to
wait for the user
10:20to type in something
and hit Enter.
10:23As soon as the user
types in Enter,
10:27whatever the user types
in becomes a string.
10:31If a user types in a
number, for example,
10:33that becomes the
string of that number.
10:36So everything the
user types in is
10:38going to be made as a string.
10:43In this line right here,
whatever these the user types
10:46in becomes a string.
10:47And we're going to
bind that string object
10:50to this variable named text.
10:54So now, further in my
program, I could do whatever
10:56I want with this variable text.
10:58In this case, I'm
going to print 5*text.
11:03So if the user, for
example, gave me "ha,"
11:07I'm going to print "ha" 5 times.
11:10If the user gave
me 5, what do you
11:13think the user is--
what do you think
11:15is going to be printed out?
11:28Oftentimes, you don't want to
work with numbers as strings,
11:32You want to work with
numbers as numbers, right?
11:34So you have to cast.
11:36And we learned
that last lecture.
11:38You cast by just putting
in this little bit
11:41right in front of the input.
11:43And you can cast it to
whatever type you want.
11:45Here I cast it to an int, but
you can also cast to a float
11:48if you want to work with floats.
11:50And that converts whatever
the user typed in,
11:53as long as it's some number that
Python knows how to convert,
11:57into the number itself.
11:59So in this case, if
the user gives me 5,
12:01I'm going to print out 5 times
5 instead of 5 five times.
12:07So that's the code here.
12:14So the first bit
is I'm going to get
12:16the user to type in anything,
and I'm going to put 555.
12:23And then when I type in the
number, since I'm casting it,
12:25I'm going to do operations
with the number.
12:28AUDIENCE: [INAUDIBLE]
12:32PROFESSOR: Why do you
want to cast to-- oh.
12:37The question is why do you
want to cast to a string?
12:41Why do you want to cast
a string to a number?
12:42AUDIENCE: [INAUDIBLE]
12:46PROFESSOR: Oh, so
Python always--
12:50whatever you type
in, just by default,
12:53by definition of
the input command,
12:55Python always makes it a string.
12:58So if you want to
work with numbers,
12:59you have to explicitly
tell it, I'm
13:00going to work with a number.
13:03So even if you give
it the number 5,
13:04it's going to think
it's the string 5.
13:09That's just how input works.
13:13The next thing we're
going to look at
13:18is ways that you can start
adding tests in your code.
13:25And before you can start
adding tests in your code,
13:29you need to be able to
do the actual tests.
13:32So this is where comparison
operators come in.
13:39So here, let's assume that
i and j are variables.
13:44The following comparisons are
going to give you a Boolean.
13:48So it's either going to say,
this is true or this is false.
13:51So that's going to be your test.
13:54So if i and j are
variables, you're
13:56allowed to compare
ints with ints,
13:58floats with floats,
strings with strings.
14:01And you're allowed
to compare ints
14:03and floats between
themselves, but you're not
14:05allowed to compare a
string with a number.
14:09In fact, if you even try to
do that in Python-- in Spider
14:13here, if I try to say, is
the letter a greater than 5?
14:18I get some angry
text right here.
14:22And this just tells
me Python doesn't
14:24understand the
meaning of-- how do I
14:26compare a string with a number?
14:31So just like in math, we can
do these usual comparisons.
14:36We can say if something
is greater than something,
14:38greater or equal to, less
than, less than or equal to.
14:41I'd like to bring to your
attention the equality.
14:44So the single equals
sign is an assignment.
14:46So you're taking a
value, and you're
14:48assigning it to a variable.
14:49But when you're doing
the double equals sign,
14:51this is the test for equality.
14:53Is the value of
variable i the same
14:55as the value of the variable j?
14:58And that's, again,
also going to give you
14:59a Boolean either true or false.
15:02And you can also test for
inequality with the exclamation
15:06So that means, is the
value of the variable i
15:09not equal to the value
of the variable j?
15:12True if yes, false if no.
15:17So those are comparison
operators on integer,
15:19floats, and strings.
15:21On Booleans, you can do
some logic operators.
15:25And the simplest
is just inverting.
15:30So if a is a variable
that has a Boolean value,
15:35not a is just
going to invert it.
15:37So if a is true, then not
a is false, and vice versa.
15:42This is a table that sort of
represents what I've said here.
15:45So you can do-- you
can use and and or.
15:49These are key words in Python.
15:52You can use those two
key words on variables,
15:54on Boolean variables.
15:57And you get the result
a and b is only true
16:01if both a and b are true.
16:04And a or b is only false
if a and b are false.
16:11And this is the complete
table just in case
16:13you need to reference it.
16:17So now that we have ways to do
logical-- question right there.
16:21AUDIENCE: [INAUDIBLE]
16:26PROFESSOR: Yeah, great question.
16:27So what does it mean to
compare a string with a string
16:29with the greater than?
16:30So that's just going to compare
them, lexicographically.
16:34So does it come first
in the alphabet?
16:37So we can even test that out.
16:39We can say, is a greater than b?
16:48So b comes later in
the alphabet than a.
16:54So now we have ways
to do the tests.
16:56So we can add some branching
to our programming toolbox
17:02now that we have
ways to do tests.
17:05This is a map of MIT.
17:06I'm going to go through
sort of a little example
17:10to motivate why we would want
to do branching in our code.
17:15And I think after this lecture,
you'll be able to sort of code
17:17up this algorithm that
I'm going to explain.
17:20So most of us see MIT as a maze.
17:21I first did when I came here.
17:26When I first came
here, obviously, I
17:28signed up for the free
food mailing list.
17:30And MIT, being a maze, I
had no idea where to go,
17:34what the shortest
path was to free food.
17:37So one way to think about
it is all I wanted to do
17:40was get to the free food.
17:44A very simple algorithm to
get there would be to say,
17:47OK, I'm going take
my right hand,
17:49and I'm going to make sure
that my right hand is always
17:53And I'm going to go around
campus with my right hand
17:55always being at a wall.
17:56And eventually, I'll get
to where the free food is.
17:59There might not be
any left, right?
18:03So the algorithm is as follows.
18:05If my right hand always
has to be on a wall,
18:07then I'm going to
say, if there's
18:10no wall to my right
side, then I'm
18:12going to go right
until I get to a wall.
18:17Then if there's a wall to my
right, and I can go forward,
18:22I'm just going to
keep going forward.
18:26If I keep going forward, and
there's a wall to my right
18:28and in front of me, I'm going
to turn around and go left.
18:31And then if there's a wall
to my right, in front of me,
18:34and to the left, then I'm going
to turn around and go back.
18:37So with this fairly
simple algorithm,
18:40I just follow the path always
keeping the wall to my right.
18:46And eventually, I would
end up where I need to be.
18:50So notice, I used, just in
plain English, a few key words.
18:54If, otherwise, things like that.
18:57So in programming, we have
those same constructs.
19:01And those same sort
of intuitive words
19:03can be used to tell
Python to do something
19:07or to do something else or to
choose from a different set
19:14And this way, we
can get the computer
19:16to make decisions for us.
19:18And you might be
thinking, well, you
19:20said that computers can't
make decisions on their own.
19:24You, as programmers, are
going to build these decisions
19:26into the program,
and all the computer
19:28is going to do is going to reach
the decision point and say,
19:31OK, this is a decision
point, should I go left
19:34or should I go right?
19:35Or which one do I pick?
19:36And these sort of decisions are
created by you as a programmer.
19:40And the computer just
has to make the decision
19:45So in programming, there's
three sort of simple ways
19:47that you can add control
flow to your programs.
19:50And that's making one
decision and choosing
19:53whether to execute something
or execute something else.
19:57The first is a simple if.
20:01And given a program
that just linearly
20:04has statements
that get executed,
20:07whenever I reach
an if statement,
20:11you're going to
check the condition.
20:13The condition is
going to be something
20:15that's going to get evaluated
to either true or false.
20:21So I've reached
the condition here.
20:25And if the condition
is true, then I'm
20:26going to additionally execute
this extra set of expressions.
20:31But if the condition
is false, then I'm
20:33just going to keep going
through the program
20:35and not execute that
extra set of instructions.
20:41How does Python know which
instructions to execute?
20:44They're going to be inside
this what we call code block.
20:48And the code block is
denoted by indentation.
20:51So it's going to be
everything that's
20:53indented is part of
that if code block.
20:58Typically, four
spaces is indentation.
21:02So that's how you
write code that
21:06decides whether to execute
this extra thing or not.
21:10Now let's say I don't just
want to execute an extra thing,
21:14I want to reach a
point where I say,
21:17I'll either go down this path
or I'll do something else.
21:22That's this right here.
21:27So this if else construct
says this is my code,
21:34I've reached my
decision point here,
21:37if the condition
inside the if is true,
21:42then I'm going to execute maybe
this set of statements here.
21:48But if the condition
is not true,
21:50then I'm not going to execute
that set of statements,
21:53and instead I'm going to
execute under whatever else is.
22:00So using this
construct, I'm either
22:02going to do one set of
expressions or the other,
22:06And after I've executed
one or the other,
22:08I'm going to continue on with
just the regular execution
22:20So we're able to either
choose one thing,
22:22choose one thing or
another, but what if we want
22:24to have more than one choice?
22:27So if some number is equal
to zero, I want to do this.
22:31If it's equal to 1,
I want to do this.
22:33If it's equal to 2, I want
to do this, and so on.
22:36That's where this
last one comes in.
22:39And we introduced this other
key word here called elif.
22:45So that stands for
short form for else if.
22:49So first we check if
this condition is true.
22:53So we're going
through our program,
22:54we've reached our
decision point,
22:56if the condition is true,
we're going to execute maybe
22:59this set of instructions.
23:04If the condition is
not true, maybe we'll
23:06check-- if the
condition is not true,
23:09we will check this
next condition.
23:11That's part of the
elif right here.
23:14And if that one's
true, we're going
23:16to execute a different
set of instructions.
23:18You can have more than one elif.
23:21And depending on
which one's true,
23:22you're going to execute a
different set of instructions.
23:25And then this last else
is sort of a catch all
23:28where if none of the previous
conditions were true,
23:31then just do this last
set of expressions.
23:35So in this case, you're
going to choose between one
23:38of these three-- one
of these four roots,
23:40or however many you have.
23:43And then when you're
done making your choice,
23:45you're going to execute the
remaining set of instructions.
23:51So the way this works is if
more than one condition is true,
23:54you're actually just going
to enter one of them.
23:57And you're going to enter the
very first one that's true.
24:01So you're never going
to enter more than one
24:02of these code blocks.
24:05You always enter one, and
you enter the first one
24:08that evaluates to true.
24:15So notice that we denoted
code blocks using indentation.
24:19And that's actually
one of the things
24:21that I really like about Python.
24:22It sort of forces you to
write pretty code and nice
24:26looking code and just
code that's very readable.
24:31And that forces you to indent
everything that's a code block.
24:36So you can easily see sort of
where the flow of control is
24:39and where decision making
points are and things like that.
24:44So in this particular example,
we have one if statement here,
24:49and it checks if two
variables are equal.
24:55And we have an if, elif, else.
24:58And in this example, we're
going to enter either this code
25:01block or this one or
this one, depending
25:04on the variables of x and y.
25:06And we're only going
into one code block.
25:08And we'll enter the
first one that's true.
25:13Notice you can have
nested conditionals.
25:16So inside this first if,
we have another if here.
25:22And this inner if is only going
to be checked when we enter
25:28the first-- this outter if.
25:36I do want to make
one point, though.
25:39So sometimes, you might forget
to do the double equals sign
25:41when you are checking for
equality, and that's OK.
25:46If you just use one
equals sign, Python's
25:48going to give you an error.
25:50And it's going to
say syntax error,
25:53and it's going to
highlight this line.
25:55And then you're going to know
that there's a mistake there.
25:58And you should be
using equality,
26:00because it doesn't
make sense to be
26:01using-- to assign-- to be making
an assignment inside the if.
26:12So we've learned
about branching.
26:13And we know about conditionals.
26:17Let's try to apply
this to a little game.
26:22And spoiler, we
won't be able to.
26:24We'll have to learn
about a new thing.
26:27But back in the 1980s,
there was the Legend
26:29of Zelda-- cool
graphics-- where there was
26:33a scene with the lost woods.
26:36Oversimplification if
anyone's a Zelda die hard fan.
26:40But the basic idea was
if you entered the woods,
26:45you entered from the
left to the right.
26:47And then as long as
you kept going right,
26:49it would show you the same
screen over and over again.
26:53And the trick was you
just had to go backward,
26:56and then you'd exit the woods.
27:00Using what we know so far, we
could sort of code this up.
27:04And we'd say
something like this.
27:06If the user exits right,
then set the background
27:08to the woods background.
27:11Otherwise, set the background
to the exit background.
27:15Now let's say the user-- and
then in the else, we're done.
27:18Let's say the user went right.
27:20Well, you'd show them
the woods background,
27:22and now ask them again,
where do they want to go?
27:25If they exit right,
set the background
27:26to the woods background.
27:27Otherwise, set the background to
the exit background, and so on.
27:31So you notice that there's
sort of no end to this, right?
27:35How many times-- do you
know how many times the user
27:38might keep going right?
27:39They might be really
persistent, right?
27:41And they'll be like maybe
if I go 1,000 times,
27:44I'll get out of the woods.
27:48So this would probably
be-- who knows how deep?
28:00So with what we know
so far, we can't really
28:02code this cute little game.
28:07And specifically, a while loop.
28:11So this code here that could
be infinitely number of nested
28:16if statements deep
can be rewritten
28:18using these three lines.
28:21So we say while the
user exits right,
28:24set the background to
the woods background.
28:26And with a while
loop, it's going
28:28to do what we tell it
to do inside the loop,
28:30and then it's going to
check the condition again,
28:32and then it's
going to do what we
28:34say it should do
inside the code block,
28:36and it's going to check
the condition again.
28:39And then when the condition--
as long as a condition is true,
28:42it's going to keep doing
that little loop there.
28:45And as soon as the
condition becomes false,
28:47it's going to stop
doing the loop
28:48and do whatever's
right after the while.
28:53So that's basically
how a while loop works.
28:58That's the key word.
29:00The condition is
something that gets
29:01evaluated to true or false.
29:03And once again, we have a
code block that's indented,
29:07and it tells Python,
these are the expressions
29:08I want to do as long as
the condition is true.
29:16So the condition is true,
you evaluate every expression
29:19When you reach the end of the
expression-- end of the code
29:22block, you check
the condition again.
29:24If it's true still, you
keep doing the expressions.
29:27Check it again, and so on.
29:32So here's a little game.
29:35And with these lines
of code, we were
29:38able-- we can code up
the lost woods of Zelda.
29:43Even worse graphics, by the
way than the original Zelda
29:46is this one that
I coded up here.
29:48So I print out the
following things.
29:50"You're in the Lost Forest.
29:54And my program's going to say,
"You're in the Lost Forest.
29:58It's going to get user input.
29:59It's going to say while the
user keeps typing in right,
30:03show them this text,
and ask them again.
30:07So I'm asking them again by
just saying input here again.
30:11That's going to just keep
getting input from the user.
30:15And if the user doesn't type in
right, and maybe types in left,
30:18you're going to exit out of
this loop, and print out,
30:21"You've got out of
the Lost Forest."
30:24So I have to show you this,
because I spent too much time
30:30But I decided to improve on
the code that's in the slides.
30:37And I've written here ways that
you guys can also improve it.
30:41So if I run my code--
"You're in the Lost Forest.
30:46So if I say left, then yay,
I got out of the Lost Forest.
30:51But if I go right,
then I'm stuck, right?
30:56I took down some trees.
30:57You can see there's
no more trees here.
30:59I made a table, and
then I flipped it over.
31:04So the expansion to this
if you want to try it out--
31:07I put this in the comments
here-- is try to use a counter.
31:12If the user types in
right the first two times,
31:14just make that a sad face.
31:17But if the user types
in more than two times,
31:19make them cut down some trees
and build a table and flip it.
31:24That's a cute little
expansion if you
31:25want to test yourself to make
sure you are getting loops.
31:30So so far, we've used while
loops to ask for user input.
31:34And that's actually somewhere
where it makes sense
31:37to use while loops,
because you don't actually
31:39know how many times the user
is going to type in something.
31:43You can use while loops
to keep sort of a counter
31:47and to write code
that counts something.
31:52If you do that, though,
there's two things
31:55you need to take care of.
31:56The first is the
first line here,
32:00which is sort of an
initialization of this loop
32:06And the second is
this line here,
32:09which is incrementing
your loop counter.
32:15The reason why the
second one is important
32:17is because-- let's look
at our condition here.
32:20So while n is less than five.
32:24If you didn't have
this line here,
32:26you would never increment n.
32:29So every time through the loop,
you just keep printing zeros.
32:33And you would have
an infinite loop.
32:34I do want to show,
though, what--
32:37if you do have an infinite loop,
it's not the end of the world.
32:40So I can say something like--
so while true, print zero.
32:53So this is going to give me an
infinite loop in my program.
33:08So notice it's just printing the
letter p over and over again.
33:12And if I let it go
any longer, it's
33:13going to slow down the computer.
33:15So I'm going to hit
Control-C or Command-C maybe.
33:18And it's going to stop
the program from printing.
33:22So just in case you ever
enter infinite loops
33:24in your programs, just go to
the console and hit Control-C,
33:28and that's going to
stop it from sort
33:31of slowing down the computer.
33:35So going back to
this example, I was
33:36saying that if you're using
counters-- variables in order
33:40to sort of count up
inside the while loop,
33:42you have to take
care to initialize
33:44a counter variable first.
33:46And then to increment
it, otherwise you'll
33:49enter an infinite loop.
33:51That feels a little bit tedious.
33:53And so there's a shortcut for
doing that exact same thing.
33:57So these four lines,
you can rewrite those
34:00into these two lines right here
using this new type of loop
34:07So the for loop says, for some
loop variable-- in this case,
34:11You can name it
whatever you want.
34:13In range 5-- we're
going to come back
34:15to what range means in
a little bit-- print n.
34:22So every time through
the loop, you're
34:23going to print out
what the value of n is.
34:26Range 5 actually
creates internally
34:31a sequence of numbers
starting from 0
34:33and going to that
number 5 minus 1.
34:36So the sequence is going
to be 0, 1, 2, 3, and 4.
34:41The first time through the
loop, you're going to say n
34:45Or internally, this
is what happens.
34:47N gets the value of 0.
34:48You're going to print n.
34:51Then you're going to
go back to the top.
34:55Then you're going to go
execute whatever is inside.
34:58So you're going to print 1.
35:00Then you're going
to increment that
35:01to the next value
in the sequence.
35:03You're going to print
out 2, and so on.
35:07So this is the general
look of a for loop.
35:12So we have for some
loop variable-- again,
35:16can be named whatever you
want-- in range some number.
35:21Do a bunch of stuff.
35:23And again, these are part
of this for loop code block.
35:26So you should indent
them to tell Python
35:29that these are the things
that you should do.
35:32So when you're using
range some number,
35:34you start out with variable
getting the value 0.
35:41With variable having
value 0, you're
35:44going to execute all
of these expressions.
35:47After all the expressions
in the code block are done,
35:50you're going to go
on to the next value.
35:55You're going to execute
all these expressions
35:57with the variable being
value 1, and then so on
36:01and so on until you go
to some num minus 1.
36:10That-- so using
range in that way
36:13is a little bit constraining,
because you're always
36:16going to get values
starting from 0
36:18and ending at some
num minus 1, whatever
36:21is in the parentheses in range.
36:23Sometimes you might want
to write programs that
36:25maybe start at a custom value.
36:28Maybe they start at 5.
36:29Maybe they start at minus 10.
36:32And sometimes you might
want to write programs
36:34that don't go with-- don't
expect the numbers by 1,
36:37but maybe skip
every other number,
36:39go every two numbers, or every
three numbers, and so on.
36:42So you can customize
range to your needs.
36:47The one thing you do need
to give it is the stop.
36:50So if you give it only one
value in the parentheses
36:52that stands for stop.
36:55And by default, start is
going to have the value 0,
36:57and step is going
to have the value 1.
37:01If you give it two things
in the parentheses,
37:04you're giving it start and stop.
37:06So the first being start,
the second being stop.
37:08And step gets this
value of 1 by default.
37:12And if you give it three
things in the parentheses,
37:15you're giving it start,
stop, and step in that order.
37:22And you're always going to
start at the start value
37:26and stop at-- or so you're going
to start at the start value,
37:30and you're going to
go until stop minus 1.
37:32So those are the
sequences of numbers.
37:36So in this first
code right here,
37:39my sum is going to
get the value 0.
37:40And you're going
to have a for loop.
37:44We're going to start
from 7, because we're
37:46giving it two numbers.
37:47And when you give
it two numbers,
37:49it represents start and
stop with step being 1.
37:53So we're starting at 7.
37:55If step is 1, the
next value is 8.
38:00What's the value after that?
38:05If we're incrementing by 1?
38:12And since we're going
until stop minus 1,
38:17we're not actually
going to pick up on 10.
38:21So this loop variable,
i, the very first time
38:23through the loop is going
to have the value 7.
38:28So my sum is going
to be 0 plus 7.
38:37That's everything that's
inside the code block.
38:40The next time through the
loop, i gets the value 8.
38:45So inside the for
loop, my sum gets
38:52whatever the previous value
was, which was 7, plus 8.
39:00The next time through
the loop, my sum
39:04get the value 7 plus 8 plus 9.
39:08Obviously, replacing that
with the previous value.
39:13Since we're not going through
10, that's where we stop.
39:15And we're going to
print out my sum, which
39:17is going to be the value
of 7 plus 8 plus 9.
39:26AUDIENCE: [INAUDIBLE]
39:27PROFESSOR: Do they
have to be integers?
39:32That's a great question.
39:33We can try that out.
39:34I'm not actually sure right
off the top of my head.
39:38So you can go on Spider and
say-- let's say in this example
39:52So we can say 7.1, 10.3-- yeah.
39:58So they have to be integers.
40:09So that's that example.
40:10And let's erase that.
40:13In this particular example,
we have start, stop, and step.
40:16And here, we're going
every other value.
40:20So we're starting at 5.
40:22Tell me what the next
value is supposed to be.
40:25If we're taking every other one.
40:277, and then 9, and then--
are we doing 11 or not?
40:37So we're going to
the end minus 1.
40:41So it's possible
that sometimes you
40:43write code where you might want
to exit out of the loop early.
40:47You don't want to go
through all of the sequences
40:51Maybe there's a condition
inside there where you just
40:53want to exit the loop early.
40:55Inside the while
loop, maybe you want
40:56to exit the loop before the
condition becomes false.
41:00So that's where the
break statement comes in.
41:02So the break works like this.
41:06It's going to-- as soon
as Python sees this break
41:09statement, it's
going to say, OK,
41:13I'm going to look at whatever
loop I'm currently in.
41:18I'm not evaluating
any expression
41:20after it that comes
within my loop.
41:23And I'm going to
immediately exit the loop.
41:26So I'm going inside
this while, this while,
41:28I'm evaluating this
one expression,
41:30and I suddenly see a break.
41:33Expression b does
not get evaluated.
41:37And break is going
to immediately
41:39exit out of the innermost
loop that it's in.
41:43So this while loop that
has condition 2, that's
41:46the innermost loop that
the break is found in.
41:50So we're going to exit out
of this inner most loop here.
41:54And we're evaluating
expression c.
41:57And notice, we're
evaluating expression c,
41:58because it's-- expression c is
part of the outer while loop.
42:05It's at the same
level as this one.
42:08And these ones are part
of the inner while loop.
42:14Last thing I want to
say is just a little bit
42:16of a comparison between
for and while loops.
42:18So when would you
use one or the other.
42:21This might be useful
in your problem sets.
42:23So for loops you
usually use when you
42:24know the number of iterations.
42:27While loops are very useful
when, for example, you're
42:29getting user input, and
user input is unpredictable.
42:32You don't know how
many times they're
42:33going to do a certain task.
42:36For both for and
while loops, you
42:38can end out of the loop
early using the break.
42:40The for loop uses this counter.
42:42It's inherent
inside the for loop.
42:45A while loop you can use a
counter in order-- you can use
42:48a while loop to count things.
42:50But you must initialize the
counter before the while loop.
42:53And you have to remember to
increment it within the loop.
42:56Otherwise, you maybe
lead to an infinite loop.
43:00We've seen as the very
first example of a for loop
43:04that the while--
the for loop could
43:06be rewritten as a while
loop, but the vice versa
43:08is not necessarily true.
43:11And the counterexample to
that is just user input.
43:14So you might not
know how many times
43:16you might do a certain task.
43:20That's all for today.