What I want to do in this video is really just
expose you and introduce you to the idea
of even what a computer program is.
And just in case you want to follow along--
and I highly recommend you do that, because the real way
to learn computer science is to really fiddle with things
yourself-- this is a Python environment.
So I'm going to be doing a lot of the programming in Python.
And right here, the environment is called pyscriptor,
P-Y-S-C-R-I-P-T-O-R. It's free.
It's an open-source piece of software.
And I'm using Python, I believe I'm using Python 2.6 or 2.7.
As long as you're using Python 2,
your examples will be the same as mine.
They'll work the same way.
If you use Python 3, you're going
to have to use slightly different variations every now
and then to make it work properly.
And I'll try to make notes for those when they occur.
So let's just start writing ourselves the computer program.
And what's cool about this is we can write our computer program
right here.
And really, we're just editing text in a file.
That's all it is.
It's a set of instructions.
And the computer is just going to start, for the most part,
at the top of this file.
And just go down and read the instructions.
Although, you'll see later on that there's
ways to tell the computer to jump around and loop around
within the instructions, so it can do things over and over
again, or skip other things.
But with that said, let's write ourselves a simple program.
And while we do this, we'll expose ourselves
to some of the core concepts that
exist within a computer program.
So one of the main-- so let me just
write a very, very, very simple computer program.
So one very simple computer program
would literally just be an expression.
So three-- So let me just write, print 3 plus 7.
So it's, literally, just going to take 3 plus 7 and print it.
So it's going to pass it to the print function, which
comes with Python.
And maybe I'll write it like this, print 3 plus 7.
And so let's save this file.
So there's, literally, only, if you think about it, only one
command here, at the top line.
It says print 3 plus 7.
Actually, let's add another command,
just so you can see that it's going to go from the top down.
And then let me add another one, print 2 minus 1.
And then, let's do, print, this is a chunk of text.
And let's see what this computer programmer, right here,
is going to do.
So let me save it.
I've saved it as the file, testarea.py.
The .py signifies that it is a Python file.
And now, let me run the program.
And what's nice about these development environments,
these IDE, Integrated Development Environments,
is that you can kind of type and run
your programs in the same place.
And it also color codes your text.
So you can see what's a function, what's
not a function, the different data types.
And we'll talk more about data types in the future.
But let's just run this program to see what happens.
So there we go.
We ran it.
So it printed 10, then it printed 1,
then it printed, this is a chunk of text.
So it did exactly what we told to do.
And it did it in the order.
It started up here.
It printed-- it evaluated 3 plus 7 as equal to 10,
and then it printed it.
It printed 10 here.
Then it printed 2 minus 1.
Then it did that there.
And then it printed, this is a chunk of text.
Now, one thing I want to introduce you to,
fairly early on, is the idea of data types.
So even when you saw this example,
you might have had the gut feeling that look,
there's something kind of different about a 3 or a 2,
or a 1, or a 7, and this chunk of text.
This is a number.
I can kind of just, I feel like I can just kind of add numbers.
They're representing some type of quantity.
While this, over here, is representing-- well,
it's representing a chunk of text.
And your intuition would be right.
These are different data types.
The 3's and the 7's and the 1's, these are numeric literals.
And in this particular case, they are integers.
And you can-- this right over here,
this is actually a string, which is
a word you'll hear a lot in computer science.
And it's really referring to a string of characters.
And in Python, we can actually ask
it, what are the types of these things.
So you can pass them to the function type.
And so now it should print the type of 3 plus 7, not just 10.
So let's try that.
And I'll just print 2 minus 1, to just show you
the difference.
And then I'll print the type of this chunk of text.
And so, let's save it.
I'll just type Control S. That's a shortcut to save this.
And then, I'll try to run this program.
So there you go.
It evaluated this statement.
And to evaluate this, it starts on the inner parentheses,
3 plus 7 is 10.
Then it tries to take the type of 10, which is a type int,
and then it prints that type int.
And you see it right here.
It says, type int.
Int is short for integer.
Then it says print 2 minus 1.
It does that on this line right here.
It prints 1.
And then it prints the type of this whole thing
right over here.
So instead of printing it itself,
it just printed its type.
And its type is a string.
Now, the next thing I want to introduce you to,
as we just fiddle our way, experimenting with programs,
is the idea of a variable.
Because one of the things is, we're
going to want to store these things in different places.
And as we'll learn in future videos,
in Python it's more of like, we want
to have labels on these things.
And the labels can change.
But let's see-- or we could put them
in different types of labels.
So let's write a completely different program over here,
and I'll do it using variables.
So let's-- and what's cool about Python,
or some people don't like it, is you can put any type of data
in any type of variables.
So you could say, a is equal to 3 plus 5.
And then we can say b is equal to a times a, minus a, minus 1.
And then you can say, c is equal to a times b.
And then, you could have something
like-- I'll put some space here, just
to make it look a little bit cleaner--
c is equal to a times b.
And then we can say, let's print c.
And so if you want, you can go ahead
and try to figure out what c is going to look like,
or we can just run this program.
So let's run the program first.
And then we can go back to see that it actually
did the right thing.
So I'm just going to save this program.
And now I'm going to run it.
Well, I got 440 for c.
So let's see if that makes sense.
So 3 plus 5 is 8.
And so, the label a will refer to 8.
So any place in the program, until we redefine
a, any time you use a, it's going to say, whoa, a is an 8,
a is referring to 8.
So when you go down over here, and we're defining b,
it'll say, OK, a times 8.
It uses order of operations.
So in order of operations, you do your multiplication first.
And so a times-- especially when you're comparing it
against subtraction-- so a times a, that's going to be 64.
And then we have 64 minus a.
So 64 minus 8 is 56, minus 1 is 55.
So b is 55, and then c is going to be a, which is 8, times 55.
And 8 times 55 is indeed 440.
So it all worked out.
And so maybe you want to-- you've defined these things,
and you want to see what happens when you get different a's.
And you can try that out.
You can just change what happens here for the different a's.
So maybe we'll have a is equal to-- let's
make it equal to negative 6.
Now let's run our program and see what happens.
We get negative 246.
And you can verify it for yourself.
You go line-by-line, and have these variables
refer to what they're defined to be referring to,
and see if you get this response right over here.
Now, if programs were just a bunch of commands
that you just always go straight through,
you wouldn't be able to do really interesting things.
And so to do really interesting things,
you're going to start seeing things
like conditionals and loops.
And a conditional and a loop is something
like-- let's do it like this.
So I'll just leave that stuff over there-- and we'll say,
if a is less than 0, maybe we will print c.
And if, or else, we'll print c minus a.
So this is interesting.
And you might already have a gut for what's
going to happen here, but let's save it.
And it's amazing how much you can
get done with just these conditionals.
So this is saying, if a is less than 0, do this.
Otherwise, if a is not less than 0, do this over here.
So notice, we're not just going to go straight down.
Depending on whether a is less than 0 or not,
it's going to either execute this line,
or it's going to execute this line.
And the way that Python, you'd only
execute this line if a is less than 0, is it's indented here.
And the indent, it's part of this clause.
And the way it knows that there are new clauses forming right
here is this colon right over here.
And then the way to know what to execute if none of this
happens, or if a is not less than 0, it's
within this else clause.
And if you want to do something else after this,
regardless of whether a is less than 0 or not,
you just take it out of the clause
by getting rid of the indentations.
So now we can just print.
We are done with the program.
Actually, let's add some other stuff in this clause.
So let's print, a is less than 0.
So notice, this is not going to be evaluated.
We have this inside of a string.
So it's just going to print that thing.
And then over here, we'll say, print a is not less than 0.
So this is an interesting program.
And let's just run it now.
All right, let's hope it runs.
All right, so I saved it.
And now, let's run the program.
And it says, it printed a is less than 0.
So we could scroll up a little bit.
It printed-- so this is, we ran the program-- it
printed a is less than 0.
So it shows that we're inside of this clause.
So it printed this, then it printed
c, which is negative 246.
It does not execute this, because this
needed to be executed only if a is not less than 0.
But then it breaks out of that clause,
and it prints this, no matter what.
We are done with the program.
And let's just change a to try to see
if we can get this other clause to print.
So let's make a greater than 0.
So let's make a equal to 9.
And now, let's run the program.
And so there, a is 9.
So it says, is a less than 0?
Well, 9 isn't less than 0.
So it's not going to execute this.
It's going to go to the else clause.
And so, it's going to print, a is not less than 0,
which it did over here.
Then it printed c minus a, which is 630.
Then it breaks out of that clause.
And regardless of whether a is less than 0 or not, it prints,
we are done with the program.