< Return to Video

Introduction to Programs Data Types and Variables

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