< Return to Video

Introduction to Programs Data Types and Variables

  • 0:00 - 0:04
    In this video I want to introduce you
  • 0:04 - 0:06
    to what a computer program is.
  • 0:06 - 0:11
    I highly recommend you follow along
  • 0:11 - 0:13
    because the real way to learn computer science
  • 0:13 - 0:15
    is to really fiddle with things yourself.
  • 0:15 - 0:19
    This is a Python environment. I'm going to be doing a lot
  • 0:19 - 0:22
    of the programming in a language called Python.
  • 0:22 - 0:28
    This environment is called PyScripter. It's free,
  • 0:28 - 0:32
    it's an open-source piece of software and I believe
  • 0:32 - 0:35
    I'm using Python 2.6 or 2.7. As long as you are using
  • 0:35 - 0:39
    Python 2 your examples will be the same as mine,
  • 0:39 - 0:42
    they will work the same way. But If your using Python 3 you are going to have
  • 0:42 - 0:44
    to use slightly different variations every now and then
  • 0:44 - 0:46
    to make it work properly. I'll try to make notes for
  • 0:46 - 0:50
    those when they occur. So lets just start writing
  • 0:50 - 0:53
    a computer program. What's cool about this is, we can
  • 0:53 - 0:55
    write our computer program right here
  • 0:55 - 0:57
    and really we are just editing text in a file. That's all it is.
  • 0:57 - 1:02
    It's a set of instructions. The computer will start
  • 1:02 - 1:05
    at the top of this file and go down
  • 1:05 - 1:08
    reading the instructions. Although you will later see
  • 1:08 - 1:10
    that there are ways to tell the computer to jump
  • 1:10 - 1:13
    around and loop around within the instructions, so that
  • 1:13 - 1:15
    it can do things over and over again or skip parts of the program.
  • 1:15 - 1:18
    With that said, lets write a simple program
  • 1:18 - 1:21
    and while we do this we will expose ourselves to some of the
  • 1:21 - 1:24
    core concepts that exist within a computer program.
  • 1:24 - 1:27
    Let me write a very very simple computer program.
  • 1:27 - 1:32
    One very simple computer program
  • 1:32 - 1:34
    would literally just be an expression.
  • 1:34 - 1:42
    So let me just write 'print 3+7', so it's literally
  • 1:42 - 1:45
    just going to take 3+7 and print it. It's going to pass
  • 1:45 - 1:48
    3+7 to the print function which comes with Python.
  • 1:48 - 1:52
    I will write it like this: print(3+7)
  • 1:52 - 1:55
    Lets save this file. So there's literally only one command
  • Not Synced
    here on the top line that says print 3+7.
  • Not Synced
    Actually, let's add another command,
  • Not Synced
    just so you can see that it's going to go top down.
  • Not Synced
    let me add another line: print(2-1)
  • Not Synced
    and lets do: print("this is a chunk of text")
  • Not Synced
    Now let's see what this computer program is going to do.
  • Not Synced
    Let me save it. I saved it as the file "testarea.py".
  • Not Synced
    The .py extension signifies it is a Python file.
  • Not Synced
    Now let me run the program. What's nice about these
  • Not Synced
    IDE or Integrated Development Environments is that
  • Not Synced
    you can type and run the program in the same place.
  • Not Synced
    It also color-codes your text, so you can see what's a
  • Not Synced
    function, what's not a function, the different data-types...
  • Not Synced
    Let's run the program to see what happens.
  • Not Synced
    So there we go, we ran it!
  • Not Synced
    So it printed 10, then it printed 1, then it printed
  • Not Synced
    "this is a chunk of text"
  • Not Synced
    So it did exactly what we told it to do.
  • Not Synced
    And it did it in the order.
  • Not Synced
    It started up here, it evaluated 3+7 as equal to 10
  • Not Synced
    and it printed it, it printed 10 here.
  • Not Synced
    Then it printed 2-1, finally it printed "this is a chunk of text"
  • Not Synced
    Now one thing I want to introduce you to,
  • Not Synced
    fairly early, it's the idea of data types.
  • Not Synced
    So maybe when you saw this example,
  • Not Synced
    you had the gut feeling that there is something different
  • Not Synced
    between the 3, 7, 2 or 1 and this chunk of text.
  • Not Synced
    This is a number. I can add numbers. They represent
  • Not Synced
    some type of quantity. While this down here is representing
  • Not Synced
    a chunk of text. And your intuition would be right.
  • Not Synced
    These are different data types. The 3, 7, 1... they are
  • Not Synced
    numerical literals. They are integers.
  • Not Synced
    And this down here is actually a String, which you hear
  • Not Synced
    often in computer science, referring to a string of characters.
  • Not Synced
    In Python we can actually ask what are the types
  • Not Synced
    of these things. So you can pass them to the function "type"
  • Not Synced
    so now it should print the type of 3+7, not just 10.
  • Not Synced
    Let's try that. I'll just print 2-1 to show you the difference.
  • Not Synced
    Then I'll print the type of this chunk of text.
  • Not Synced
    Let's save it. I press CTRL+S, which is the shortcut for save.
  • Not Synced
    Then I try to run this program. So there you go.
  • Not Synced
    It evaluated this statement. It starts at the inner parenthesis.
  • Not Synced
    3+7 is 10. Then it tries to take the type of 10, which is int
  • Not Synced
    a type int, then it prints that type int.
  • Not Synced
    You see it right here. It says type 'int'.
  • Not Synced
    int is short for Integer.
  • Not Synced
    Then it says print(2-1). It does that on this line right here,
  • Not Synced
    prints 1, and then it prints the type of this whole thing
  • Not Synced
    right over here. So instead of printing the text,
  • Not Synced
    it prints its type. And its type is String.
  • Not Synced
    The next thing I want to introduce to you
  • Not Synced
    as we experiment with programs is
  • Not Synced
    the idea of a Variable. Because we are going to want
  • Not Synced
    to store these things in different places.
  • Not Synced
    We will learn in future videos that in Python
  • Not Synced
    it's more like we will have labels for these things,
  • Not Synced
    and the labels can change.
  • Not Synced
    Let's write a completely different program
  • Not Synced
    using variables. What's cool about Python, even
  • Not Synced
    some people doesn't like it, is that you can put
  • Not Synced
    any type of data in any type of variable.
  • Not Synced
    So you can say a=3+5, then we can say
  • Not Synced
    b=a*a-a-1
  • Not Synced
    and then you can say c=a*b
  • Not Synced
    I will put some space here to make it a little cleaner.
  • Not Synced
    Finally lets print c. If you want you can go ahead
  • Not Synced
    and try to figure out what c is going to look like
  • Not Synced
    or we can just run this program.
  • Not Synced
    So let's run the program and then
  • Not Synced
    we can go back to see if it did the right thing.
  • Not Synced
    I'm going to save the program, and run it.
  • Not Synced
    We got 440 for c. Let's see if that makes sense.
  • Not Synced
    3+5 is 8. So the label "a" will refer to 8.
  • Not Synced
    Any place in the program, until we redefine "a",
  • Not Synced
    any time you use "a" it will know it means 8.
  • Not Synced
    So when you go down over here to define "b" it says
  • Not Synced
    a*a. It uses order of operations, so it does
  • Not Synced
    multiplications first. a*a is 64.
  • Not Synced
    64 - a is 64 - 8, which is 56. Minus 1 is 55.
  • Not Synced
    So "b" is 55. And "c" is going to be a * b,
  • Not Synced
    which is 8 * 55, indeed 440. It all worked out.
  • Not Synced
    Maybe you want to see what happens when
  • Not Synced
    you get different "a"s. You can try that out.
  • Not Synced
    You can just make "a" equal to -6
  • Not Synced
    and run our program to see what happens.
  • Not Synced
    We get -246. And you can verify it by yourself.
  • Not Synced
    You go line by line, and have these variables refer to
  • Not Synced
    what they are defined to be referring to,
  • Not Synced
    and see if you get this response right here.
  • Not Synced
    Now, if programs were just a bunch of commands
  • Not Synced
    and you just always went straight through,
  • Not Synced
    you wouldn't be able to do really interesting things.
  • Not Synced
    To do really interesting things you are going to
  • Not Synced
    start seeing things like Conditionals and Loops.
  • Not Synced
    Let's see a conditional. I will leave the first lines unchanged.
  • Not Synced
    And type "if (a<0):". In that case, we will print(c)
  • Not Synced
    then type "else", and in this case we will print (c-a).
  • Not Synced
    So this is interesting. You might already have a gut
  • Not Synced
    for what's going to happen here. But let's save it.
  • Not Synced
    It's amazing how much you can get done just
  • Not Synced
    with these conditionals. So this is saying
  • Not Synced
    if "a" is less than 0, do this, otherwise
  • Not Synced
    if "a" is not less than 0, do this over here.
  • Not Synced
    So notice we are not going just straight down.
  • Not Synced
    Depending on whether "a" is less than 0 or not,
  • Not Synced
    it's going to either execute this line,
  • Not Synced
    or it's going to execute this line.
  • Not Synced
    And the way that Python knew to only execute
  • Not Synced
    "print(c)" if "a" was less than 0 is because
  • Not Synced
    print(c) is indented. The indent is part of this clause.
  • Not Synced
    The way it knows that there are new clauses for him
  • Not Synced
    is because of this colon right here.
  • Not Synced
    If "a" is not less than 0, then it executes this "else" clause.
  • Not Synced
    If you want to do something else after this,
  • Not Synced
    regardless of whether "a" is less than 0 or not,
  • Not Synced
    You can get rid of the clause by getting rid of
  • Not Synced
    the indentation. So now we can just print a string.
  • Not Synced
    Actually, let's add some more stuff to this clause.
  • Not Synced
    Let's print here "a<0".
  • Not Synced
    Notice: this is not going to be evaluated.
  • Not Synced
    We have this inside of a string, so it is just going to
  • Not Synced
    print that thing. Over here we will say
  • Not Synced
    print("a is not less than 0")
  • Not Synced
    This is an interesting program. Let's just run it now.
  • Not Synced
    Let's hope it runs. I save it. Now let's run the program.
  • Not Synced
    And it printed "a<0", so it shows we are inside of this clause.
  • Not Synced
    Then it printed "c", which is -246.
  • Not Synced
    It does not execute this, because this would only be
  • Not Synced
    executed if a was not less than 0.
  • Not Synced
    But then it breaks out of this clause and prints
  • Not Synced
    "we are done with the program" no matter what.
  • Not Synced
    Let's now change "a" to see if we can get this other
  • Not Synced
    clause to run. Let's make "a" greater than 0.
  • Not Synced
    Let's make "a" equal to 9 and run the program.
  • Not Synced
    So there. "a" is 9. It checks "is a less than 0?",
  • Not Synced
    well, "a" is not less than 0, so it's not going to
  • Not Synced
    execute this, it's going to go to the else clause.
  • Not Synced
    So it's going to print "a is not less than 0"
  • Not Synced
    which it did over here, then it printed c-a
  • Not Synced
    which is 630. It breaks out of that clause,
  • Not Synced
    and regardless of whether "a" is less than 0 or not,
  • Not Synced
    it prints "we are done with the program".
Title:
Introduction to Programs Data Types and Variables
Description:

Writing a basic program. Basics of data types, variables and conditional statements

more » « less
Video Language:
English
Duration:
11:28

English subtitles

Revisions Compare revisions