< Return to Video

Introduction to Programs Data Types and Variables

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