-
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 Python 2.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 they occur.
-
So let's 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, let's 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 it to the print function
-
which comes with Python.
-
Maybe I will write it like this: print(3+7)
-
Let's 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 one: 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 this development environment,
-
this IDE or Integrated Development Environment, 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 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 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,
-
and then it printed "this is a chunk of text"
-
Now one thing I want to introduce you to, fairly early on,
-
it's the idea of data types.
-
So even when you saw this example,
-
you might have the gut feeling that
-
look, there is something kind of different
-
about a 3 or 2 or 1 or 7 and this chunk of text.
-
This is a number. I can just kind of 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 of this chunk of text.
-
The type of this chunk of text.
-
And let's save it. I just type CTRL+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. It starts at the inner parenthesis.
-
3+7 is 10. Then it tries to take the type of 10,
-
which is 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 a String.
-
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 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 see, or we can put them different types of labels.
-
So let's write a completely different program using variables.
-
What's cool about Python
-
some people don'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
-
Then you can have something like...
-
I will put some space here to make it a little bit cleaner.
-
c = a*b
-
Then we can say, let's print c.
-
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 if it did the right thing.
-
So I'm going to save the program,
-
and now I'm going to run it.
-
We got 440 for c. Let's see if that makes sense.
-
So 3+5 is 8. 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: a is 8. a is referring to 8.
-
So when you go down over here to define "b"
-
it'll say OK, a*a. It uses order of operations.
-
So order of operations, you do your multiplications first.
-
Especially when you're comparing against a subtraction.
-
So a*a that's going to be 64.
-
Then we have 64 - a is 64 - 8, which is 56. Minus 1 is 55.
-
So "b" is 55. And "c" is going to be a...which is 8.... times 55...
-
And 8 times 55 is indeed 440.
-
So it all worked out.
-
So maybe you want to see what happens
-
when you get different "a"s.
-
You can try that out.
-
you can just change what happens here for different a's.
-
So maybe we'll have a is equal to ...
-
Let's make it equal to -6
-
and now let's 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 over here.
-
Now, if programs were just a bunch of commands
-
and you just always go straight through,
-
you wouldn't be able to do really interesting things.
-
So to do really interesting things you are going to
-
start seeing things like Conditionals and Loops.
-
And Conditionals and Loops are something like
-
Let's do it like this
-
So...if.... I will leave that stuff over there.
-
And we'll say "if (a<0):". Maybe we will print(c)
-
If or "else:", print ... or otherwise ...we'll print (c-a).
-
So this is interesting. You might already have a gut
-
for what's going to happen here. Let's save it.
-
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 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 this line,
-
if "a" was less than 0 is it's indented here.
-
And the indent is part of this clause.
-
The way it knows that there are new clauses for him
-
is this colon right over here.
-
And the way to know what to execute
-
If none of these happens
-
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 just take it out of the clause by getting rid of the indentation.
-
So now we can just print "we are done with the program".
-
Actually, let's add some other stuff in this clause.
-
Let's print here "a<0".
-
So 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.
-
It printed this. 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".