-
What I want to do in this video is to expose you
-
and introduce you to the idea
-
to what a computer program is.
-
And just in case you want to follow along
-
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, this environment is called PyScripter. P-Y-S-C-R-I-P-T-E-R. It's free,
-
it's an open-source piece of software.
-
And I'm using Python..Python I believe I'm using Python2.6 or 2.7.
-
As long as you are using Python 2 your examples will be the same as mine,
-
they work the same way. But If you're using Python 3 you are going to have
-
to use slightly different variations every now and then to make it work properly.
-
I'll try to make notes for those when, when they occur.
-
So lets just start writing ourselves a computer program.
-
What's cool about this is, we can write our computer program right here.
-
And really we are just editing text in a file.
-
That's all it is.
-
It's a set of instructions and the computer is going to start for the most part.
-
at the top of this file and just go down and read these instructions.
-
Although you will later on
-
that there's a way to tell the computer to jump around and to loop around
-
within the instructions so that it can do things over and over again or skip other thing.
-
With that said, lets write ourselves a simple program
-
and while we do this we will expose ourselves to some of the core concepts that exist within a computer program.
-
Let me write a very very simple computer program.
-
so one very simple computer program.
-
would literally just be an expression.
-
So let me just write 'print 3+7', so it's literally
-
just going to take 3+7 and print it. It's going to pass
-
3+7 to the print function which comes with Python.
-
Maybe I will write it like this: print(3+7)
-
Lets save this file. So there's literally only one...if you think about it. Only one command
-
here on the top line here. That says print 3+7.
-
Actually, let's add another command,
-
just so you can see that it's going to go top down.
-
let me add another line: print(2-1)
-
and then let's do: print("this is a chunk of text")
-
Now let's see what this computer program right here is going to do.
-
so let me save it.
-
So let me save it.
-
I saved it as the file "testarea.py". Tells a... a...That's a
-
The .py extension signifies it is a Python file.
-
Now let me run the program.
-
What's nice about these development environment ?
-
This IDE or Integrated Development Environments is that
-
you can kind of type and run your program in the same place.
-
It also color-codes your text, so you can see what's a
-
function, what's not a function, the different data-types...we will talk about more data types in the future.
-
Let's run the 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 it to do.
-
And it did it in the order.
-
It started up here, it evaluated 3+7 as equal to 10
-
and it printed it, it printed 10 here.
-
and then it printed 2-1, finally it printed "this is a chunk of text"
-
Now one thing I want to introduce you too.
-
fairly early, it's the idea of data types.
-
So maybe when you saw this example,
-
you had the gut feeling that there is something different
-
between the 3 or 2 or1 or 7 and this chunk of text.
-
This is a number. I can add numbers. They're representing
-
some type of quantity. While this over here is representing
-
a chunk of text. And your intuition would be right.
-
These are different data types.
-
The 3 and 7 and 1... these are numerical literals.
-
In this particular case, they are integers.
-
And you can..in this one over here,
-
this is actually a String,
-
which is a word you hear a lot in computer science.
-
this is really..referring to a string...of characters.
-
and In Python we can actually ask what are the types of these things.
-
So you can pass them to the function "type"
-
so now it should print the type of 3+7, not just 10.
-
Let's try that. I'll just print 2-1 to show you the difference.
-
Then I'll print the type...the type of this chunk of text.
-
The type of this chink of text.
-
And let's save it. I just type CTRL+S, which is the shortcut to save this.
-
and then I'll try to run this program. So there you go.
-
It evaluated this statement. It starts at the inner parenthesis.
-
3+7 is 10. Then it tries to take the type of 10, which is int
-
a type int, then it prints that type int.
-
You see it right here. It says type 'int'.
-
int is short for Integer.
-
Then it says print(2-1). It does that on this line right here,
-
prints 1, and then it prints the type of this whole thing
-
right over here. So instead of printing the text,
-
it prints its type. And its type is String.
-
The next thing I want to introduce you to
-
as we experiment with programs is
-
the idea of a Variable. Because we are going to want
-
to store these things in different places.
-
We will learn in future videos that in Python
-
it's more like we will have labels for these things,
-
and the labels can change.
-
Let's write a completely different program
-
using variables. What's cool about Python, even
-
some people doesn't like it, is that you can put
-
any type of data in any type of variable.
-
So you can say a=3+5, then we can say
-
b=a*a-a-1
-
note: * means "times", it is used for multiplication.
-
and then you can say c=a*b (c equals a times b)
-
I will put some space here to make it a little cleaner.
-
Finally lets print c. 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 and then
-
we can go back to see if it did the right thing.
-
I'm going to save the program, and run it.
-
We got 440 for c. Let's see if that makes sense.
-
3+5 is 8. So the label "a" will refer to 8.
-
Any place in the program, until we redefine "a",
-
any time you use "a" it will know it means 8.
-
So when you go down over here to define "b" it says
-
a*a. It uses order of operations, so it does
-
multiplications first. a*a is 64.
-
64 - a is 64 - 8, which is 56. Minus 1 is 55.
-
So "b" is 55. And "c" is going to be a * b,
-
which is 8 times 55, indeed 440. It all worked out.
-
Maybe you want to see what happens when
-
you get different "a"s. You can try that out.
-
You can just make "a" equal to -6
-
and run our program to see what happens.
-
We get -246. And you can verify it by yourself.
-
You go line by line, and have these variables refer to
-
what they are defined to be referring to,
-
and see if you get this response right here.
-
Now, if programs were just a bunch of commands
-
and you just always went straight through,
-
you wouldn't be able to do really interesting things.
-
To do really interesting things you are going to
-
start seeing things like Conditionals and Loops.
-
Let's see a conditional. I will leave the first lines unchanged.
-
And type "if (a<0):". In that case, we will print(c)
-
then type "else:", and in this case we will print (c-a).
-
So this is interesting. You might already have a gut
-
for what's going to happen here. But let's save it.
-
It's amazing how much you can get done just
-
with 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 are not going just 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 knew to only execute
-
"print(c)" if "a" was less than 0 is because
-
print(c) is indented. The indent is part of this clause.
-
The way it knows that there are new clauses for him
-
is because of this colon right here.
-
If "a" is not less than 0, then it executes this "else" clause.
-
If you want to do something else after this,
-
regardless of whether "a" is less than 0 or not,
-
You can get rid of the clause by getting rid of
-
the indentation. So now we can just print a string.
-
Actually, let's add some more stuff to this clause.
-
Let's print here "a<0".
-
Notice: this is not going to be evaluated.
-
We have this inside of a string, so it is just going to
-
print that thing. Over here we will say
-
print("a is not less than 0")
-
This is an interesting program. Let's just run it now.
-
Let's hope it runs. I save it. Now let's run the program.
-
And it printed "a<0", so it shows we are inside of this clause.
-
Then it printed "c", which is -246.
-
It does not execute this, because this would only be
-
executed if a was not less than 0.
-
But then it breaks out of this clause and prints
-
"we are done with the program" no matter what.
-
Let's now change "a" to see if we can get this other
-
clause to run. Let's make "a" greater than 0.
-
Let's make "a" equal to 9 and run the program.
-
So there. "a" is 9. It checks "is a less than 0?",
-
well, "a" is not less than 0, so it's not going to
-
execute this, it's going to go to the else clause.
-
So it's going to print "a is not less than 0"
-
which it did over here, then it printed c-a
-
which is 630. 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".