Lecture4 Add2Numbers

00:58:41
https://www.youtube.com/watch?v=Lh_BYRivF9U

Summary

TLDRCe programme Python interactif permet à l'utilisateur d'entrer deux nombres, puis les additionne et affiche le résultat. Il utilise des fonctions comme print pour afficher des messages et input pour obtenir des entrées utilisateur. Les entrées sont d'abord traitées comme des chaînes de caractères et doivent être converties en entiers pour effectuer l'addition. Le programme illustre également la gestion des erreurs lorsque l'utilisateur entre des valeurs non valides.

Takeaways

  • 🖥️ Ce programme additionne deux nombres.
  • 💬 Utilise la fonction input pour obtenir des entrées utilisateur.
  • 🔄 Les entrées doivent être converties en entiers pour l'addition.
  • ⚠️ Les erreurs de valeur se produisent avec des entrées non numériques.
  • 📦 Les variables stockent des valeurs dans le programme.
  • ➕ Les chaînes de caractères peuvent être concaténées avec +.
  • 🔍 Les types de données définissent la nature des valeurs.
  • 📊 Les erreurs sont gérées avec des messages d'erreur clairs.
  • 📖 Les commentaires aident à comprendre le code.
  • 🎉 Vous avez appris à créer un programme interactif en Python.

Timeline

  • 00:00:00 - 00:05:00

    Introduction d'un programme Python interactif qui additionne deux nombres.

  • 00:05:00 - 00:10:00

    Le programme commence par une déclaration d'impression qui informe l'utilisateur de la fonction du programme.

  • 00:10:00 - 00:15:00

    Utilisation de la fonction d'entrée pour demander à l'utilisateur d'entrer le premier nombre, qui est stocké dans une variable appelée num1.

  • 00:15:00 - 00:20:00

    Le texte saisi par l'utilisateur est converti en entier pour être utilisé dans des calculs.

  • 00:20:00 - 00:25:00

    Le même processus est répété pour le deuxième nombre, stocké dans la variable num2.

  • 00:25:00 - 00:30:00

    Les deux nombres sont additionnés et le résultat est stocké dans une nouvelle variable appelée total.

  • 00:30:00 - 00:35:00

    Une déclaration d'impression affiche le total, en convertissant le nombre en chaîne de caractères pour l'affichage.

  • 00:35:00 - 00:40:00

    Explication détaillée des fonctions print et input, y compris leur syntaxe et leur fonctionnement.

  • 00:40:00 - 00:45:00

    Introduction aux variables, leur création, leur assignation et leur portée dans le programme.

  • 00:45:00 - 00:50:00

    Discussion sur les types de données en Python, y compris les entiers, les flottants, les chaînes et les booléens.

  • 00:50:00 - 00:58:41

    Conclusion avec une démonstration du programme en ligne, y compris la gestion des erreurs lors de l'entrée de données non valides.

Show more

Mind Map

Video Q&A

  • Quel est le but de ce programme ?

    Le programme additionne deux nombres fournis par l'utilisateur.

  • Comment le programme obtient-il les entrées de l'utilisateur ?

    Il utilise la fonction input pour demander à l'utilisateur d'entrer des nombres.

  • Que se passe-t-il si l'utilisateur entre une valeur non numérique ?

    Le programme génère une erreur de valeur car il ne peut pas convertir la chaîne en entier.

  • Comment les variables sont-elles utilisées dans le programme ?

    Les variables stockent les valeurs entrées par l'utilisateur et les résultats des calculs.

  • Pourquoi devons-nous convertir les entrées en entiers ?

    Les entrées sont initialement des chaînes de caractères et doivent être converties pour effectuer des opérations mathématiques.

  • Qu'est-ce qu'une erreur de valeur ?

    C'est une erreur qui se produit lorsque le programme essaie de convertir une chaîne non valide en entier.

  • Comment le programme affiche-t-il le résultat ?

    Il utilise la fonction print pour afficher le total sous forme de chaîne.

  • Qu'est-ce qu'une variable en Python ?

    Une variable est un espace de stockage nommé qui contient une valeur.

  • Comment les chaînes de caractères sont-elles concaténées dans le programme ?

    Les chaînes sont concaténées à l'aide de l'opérateur +.

  • Qu'est-ce qu'un type en Python ?

    Un type définit la nature des données qu'une variable peut contenir, comme int pour les entiers ou str pour les chaînes.

View more video summaries

Get instant access to free YouTube video summaries powered by AI!
Subtitles
en
Auto Scroll:
  • 00:00:00
    so let's consider another program now
  • 00:00:03
    that's a little bit more involved now
  • 00:00:04
    that we've taken our initial steps into
  • 00:00:06
    python so what's this program going to
  • 00:00:08
    do so I'm going to take you through step
  • 00:00:10
    by step because this program is a little
  • 00:00:11
    bit more interactive it's actually going
  • 00:00:13
    to get get input from the user so when
  • 00:00:16
    this program starts off it's going to
  • 00:00:18
    start executing as main as we saw before
  • 00:00:19
    and I've taken out sort of the top
  • 00:00:21
    comments and stuff like that just to
  • 00:00:23
    focus on the code there's a print
  • 00:00:25
    statement what this print statement is
  • 00:00:26
    going to do and what I'm going to
  • 00:00:27
    represent down here is down here is the
  • 00:00:29
    terminal so I'm going to things that get
  • 00:00:31
    printed to the terminal I'm just going
  • 00:00:32
    to show them in this box down here so
  • 00:00:35
    first line comes along and it does this
  • 00:00:37
    print statement it says this program
  • 00:00:38
    adds two numbers and as you know now
  • 00:00:40
    with the print statement what's going to
  • 00:00:42
    get printed on the terminal is all the
  • 00:00:44
    text in between the double quotes the
  • 00:00:46
    double quotes aren't actually printed
  • 00:00:47
    but this program adds two numbers in the
  • 00:00:49
    period is printed to the terminal now
  • 00:00:52
    what happens next what happens next is
  • 00:00:54
    we have the statement called input and
  • 00:00:56
    what input does is this the way to ask
  • 00:00:58
    the user of the program and the user
  • 00:01:01
    being whoever's sitting there at the
  • 00:01:02
    terminal to put some some data or some
  • 00:01:05
    information into the program and so what
  • 00:01:07
    input does is it comes along and it will
  • 00:01:10
    print out on the terminal all the text
  • 00:01:12
    that's between the two quotes So enter a
  • 00:01:14
    first number as you can see is what gets
  • 00:01:16
    printed here out on the terminal and
  • 00:01:17
    then it sort of sits there like the
  • 00:01:19
    cursor is just waiting there for you to
  • 00:01:21
    type something and so when you type
  • 00:01:23
    something and hit enter whatever you
  • 00:01:25
    type in and hit enter that text is going
  • 00:01:27
    to get stored inside a box with the name
  • 00:01:31
    num1 and a little preview for you that
  • 00:01:34
    num1 is what we have actually call a
  • 00:01:36
    variable basically it's just a place
  • 00:01:38
    where we can store information inside
  • 00:01:40
    the computer so let's say for example
  • 00:01:42
    the user Types on the screen the number
  • 00:01:44
    nine and hits enter right so that's why
  • 00:01:46
    I put it in sort of blue font there and
  • 00:01:49
    it's italics because that's what the
  • 00:01:50
    user input would be they would type the
  • 00:01:52
    number nine input
  • 00:01:53
    that text for the number nine you can
  • 00:01:56
    see I've put it in double quotes to
  • 00:01:57
    indicate that it's text is actually
  • 00:01:59
    python takes that as the result of input
  • 00:02:02
    and says where do I want to store that I
  • 00:02:04
    want to store that into this thing
  • 00:02:05
    called num1 so creates a box called num1
  • 00:02:08
    and puts that text 9 into it now if we
  • 00:02:11
    want to do something with that text nine
  • 00:02:13
    like we want to treat it as the number
  • 00:02:15
    nine we need to convert it to number
  • 00:02:17
    nine because right now it's just a piece
  • 00:02:19
    of text
  • 00:02:20
    and so what this next line does it says
  • 00:02:23
    take that piece of text whatever is in
  • 00:02:25
    num1 what's in that box and convert it
  • 00:02:28
    to a number in this case we want to
  • 00:02:30
    convert it to an INT which is short for
  • 00:02:32
    integer we want to convert it to an
  • 00:02:34
    integer number so what it does it says
  • 00:02:36
    okay take that piece of text nine right
  • 00:02:38
    if I back up you can see that was the
  • 00:02:39
    piece of text nine that was this num
  • 00:02:41
    here
  • 00:02:42
    take that piece of text 9 and convert it
  • 00:02:45
    to the integer number equivalent which
  • 00:02:48
    is the number nine and what are you
  • 00:02:50
    going to do with it I'm going to put
  • 00:02:51
    that back into the box for num1 so it
  • 00:02:54
    replaces whatever was in that box before
  • 00:02:56
    with the value 9 which is now the number
  • 00:02:58
    nine notice the double quotes are gone
  • 00:03:00
    okay now we're going to go through that
  • 00:03:02
    same process again to get another number
  • 00:03:04
    and put it in a different location so we
  • 00:03:06
    have another input statements it says
  • 00:03:08
    enter second number that gets printed on
  • 00:03:10
    our terminal and the cursor is sort of
  • 00:03:12
    waiting here for the user to enter
  • 00:03:13
    something let's say the user now types
  • 00:03:15
    in a one and a seven and hits enter so
  • 00:03:17
    that's the number 17 that 17 is a piece
  • 00:03:21
    of text is what comes back from input
  • 00:03:23
    and is what's stored inside num2 so
  • 00:03:26
    python says I will create a new box
  • 00:03:28
    called num2 and put that text 17 into it
  • 00:03:31
    and then just like you saw before we
  • 00:03:34
    want to take that whatever contents are
  • 00:03:36
    in num2 which is the text 17 and convert
  • 00:03:39
    it to a number which is an integer and
  • 00:03:41
    so what we want to do is convert that on
  • 00:03:44
    text 17 to the value 17 and then what do
  • 00:03:49
    we want to do with it we want to store
  • 00:03:50
    it back into the box for num2 so we want
  • 00:03:53
    to take the text 17 convert it to the
  • 00:03:56
    number 17 and then store that 17 back in
  • 00:03:59
    this box and that's what that line does
  • 00:04:01
    notice I have a 17 here without the
  • 00:04:03
    double quotes now
  • 00:04:05
    okay now the final thing I want to do is
  • 00:04:07
    one almost final thing I want to do is I
  • 00:04:09
    want to say I want to add num1 and num2
  • 00:04:12
    together and then store the result
  • 00:04:14
    inside a new box called total so what
  • 00:04:17
    this does is it says okay when I have an
  • 00:04:19
    equal sign here that's actually known as
  • 00:04:21
    an assignment and we'll go through this
  • 00:04:23
    program I'll sort of deconstruct all the
  • 00:04:25
    parts for it and we'll go through the
  • 00:04:26
    program again in even more detail so
  • 00:04:28
    then you know what we're referring to
  • 00:04:29
    when referring to all these pieces
  • 00:04:32
    um in a little bit more sort of python
  • 00:04:34
    speak so to speak what this is doing is
  • 00:04:36
    it goes and looks up num1 which is nine
  • 00:04:38
    it looks up num2 which is 17. it adds
  • 00:04:41
    them together which gets you the value
  • 00:04:43
    26 and it puts that 26 in a box called
  • 00:04:47
    total so when all that happens what we
  • 00:04:50
    get is a box call Total with the value
  • 00:04:52
    26 in it okay and the last thing we're
  • 00:04:56
    going to do is we want to print that
  • 00:04:58
    total onto the screen and so we're going
  • 00:05:00
    to have another print statement here but
  • 00:05:01
    this looks kind of funky what's going on
  • 00:05:03
    with this print statement it says the
  • 00:05:05
    total is and then we want to print out
  • 00:05:08
    some textual version of this total
  • 00:05:10
    that's what it does it says look up this
  • 00:05:11
    total 26 convert a textual version of it
  • 00:05:15
    which is actually in Python knows known
  • 00:05:18
    as a string we'll talk about that more
  • 00:05:20
    in just a moment but that's why we have
  • 00:05:22
    this Str here it says treat this 26 as a
  • 00:05:26
    piece of text that would be sort of 26
  • 00:05:29
    inside quotes and then add a period to
  • 00:05:32
    the end and so what's really going on
  • 00:05:34
    here is it says okay the total is
  • 00:05:36
    concatenates on with this plus sign the
  • 00:05:39
    26 and then concatenates on a little
  • 00:05:41
    period at the end and prints that all on
  • 00:05:44
    the screen so when we execute that print
  • 00:05:46
    line what we get is the total is notice
  • 00:05:49
    I get that 26 here which is coming from
  • 00:05:51
    this box and being converted to text and
  • 00:05:54
    and then the period at the end
  • 00:05:56
    so what we've now done is constructed
  • 00:05:58
    perhaps the most expensive calculator
  • 00:06:01
    that can only add two numbers in the
  • 00:06:04
    world because we're using a computer to
  • 00:06:06
    just add two numbers so you're like okay
  • 00:06:08
    that's kind of interesting Maron but you
  • 00:06:10
    kind of went through this fast and there
  • 00:06:11
    was all these little pieces in there can
  • 00:06:13
    you deconstruct what's really going on
  • 00:06:15
    so I can sort of see in more general
  • 00:06:17
    terms what's actually happening and what
  • 00:06:19
    are these boxes all about well the boxes
  • 00:06:21
    are really variables so let's jump into
  • 00:06:23
    that and we can look at each statement
  • 00:06:25
    piece by piece in more detail so let's
  • 00:06:28
    look at the first statement print what
  • 00:06:30
    is print doing well print is actually a
  • 00:06:33
    function and what the print function
  • 00:06:34
    does is it says print out all the text
  • 00:06:37
    in between the two quotes that you give
  • 00:06:39
    me okay and so in a little bit more
  • 00:06:42
    detail is basically print is a command
  • 00:06:44
    or a function that prints text to the
  • 00:06:46
    terminal the text is specified so we
  • 00:06:48
    have an opening paren and a closing
  • 00:06:50
    paren and then inside quotes as we
  • 00:06:52
    specify the text
  • 00:06:54
    the text that's printed out is all the
  • 00:06:57
    text in between the double quotes So the
  • 00:06:59
    double quotes are not actually printed
  • 00:07:00
    as you saw in the terminal but all the
  • 00:07:02
    text in between them is and it turns out
  • 00:07:04
    you can also use single quotes So if you
  • 00:07:06
    want to have a version of print where
  • 00:07:08
    you said something like print open
  • 00:07:10
    parentheses and then single quote or you
  • 00:07:12
    could think of that as the apostrophe
  • 00:07:14
    character the same thing and then hello
  • 00:07:16
    single quote as long as they're matching
  • 00:07:19
    either single quotes or double quotes
  • 00:07:20
    that will also work too so you can
  • 00:07:22
    either use single quotes or double
  • 00:07:24
    quotes and so one of the natural
  • 00:07:26
    question that comes up is okay why do I
  • 00:07:29
    choose one versus the other well sort of
  • 00:07:31
    by convention a lot of python
  • 00:07:33
    programmers use single quotes some like
  • 00:07:35
    to use double quotes but there is one
  • 00:07:37
    time when you're constrained to use one
  • 00:07:39
    versus the other and that's when you
  • 00:07:41
    actually want to use the other kind of
  • 00:07:43
    character in the middle of the text you
  • 00:07:45
    want to print so let me show you an
  • 00:07:47
    example let's for example con why want
  • 00:07:50
    to write some text on the screen that
  • 00:07:52
    contains a single quote or you could
  • 00:07:53
    think of as an aposture character
  • 00:07:55
    they're the same thing like we have the
  • 00:07:57
    word didn't here which has an apostrophe
  • 00:07:59
    in it if we want to do that then we
  • 00:08:01
    would need to encapsulate all of the
  • 00:08:03
    text inside double quotes because we
  • 00:08:06
    actually want the single quote character
  • 00:08:07
    to be printed to the terminal and so
  • 00:08:10
    this would print on the screen no you
  • 00:08:12
    didn't yes I did but it would print no
  • 00:08:16
    you didn't and the single quote
  • 00:08:18
    character would actually get printed
  • 00:08:19
    because it would know that the text here
  • 00:08:21
    was actually started and ended or what
  • 00:08:23
    we refer to as delimited by double
  • 00:08:26
    quotes the flip side can also be true
  • 00:08:29
    let's say you wanted to print something
  • 00:08:30
    on the screen that had double quotes in
  • 00:08:32
    it like a quotation then you would use
  • 00:08:35
    single quotes to indicate the beginning
  • 00:08:37
    and ending of the text so if we wanted
  • 00:08:39
    to print out the text say hi Carol where
  • 00:08:41
    high was in double quotes we would have
  • 00:08:44
    the beginning and ending indicated with
  • 00:08:46
    single quotes and so this would print
  • 00:08:48
    out on the screen say hi Carol and the
  • 00:08:50
    double quotes get printed but the single
  • 00:08:52
    quotes that start the text and end the
  • 00:08:54
    text don't get printed and you might say
  • 00:08:56
    what about that special case or I want
  • 00:08:58
    to have both double quotes and single
  • 00:08:59
    quotes well I'll give you some
  • 00:09:00
    references for that if you really want
  • 00:09:02
    to do it but it's pretty rare so it's
  • 00:09:03
    not worth spending a bunch of class time
  • 00:09:05
    on it but that's kind of the the two
  • 00:09:07
    cases where you're forced to use either
  • 00:09:08
    double quotes or single quotes otherwise
  • 00:09:10
    if you're just printing text that
  • 00:09:11
    doesn't contain double or single quotes
  • 00:09:12
    you can use whichever one you want we
  • 00:09:15
    just say be consistent like either
  • 00:09:16
    consistently use double quotes unless
  • 00:09:18
    you're forced to use single quotes or
  • 00:09:20
    consistently use single quotes unless
  • 00:09:22
    you're forced to use double quotes Okay
  • 00:09:24
    so that choice is yours but just be
  • 00:09:26
    consistent
  • 00:09:27
    okay so that's our friend the print
  • 00:09:29
    function it just prints stuff on the
  • 00:09:31
    screen and now you got seen a little bit
  • 00:09:33
    more detail how about our friend the
  • 00:09:35
    input function well the way the input
  • 00:09:37
    function works is basically the setup
  • 00:09:40
    for input is it's a command that gets
  • 00:09:42
    some text input from the user that's the
  • 00:09:45
    way to think about it what it's going to
  • 00:09:46
    do is on the terminal it's going to
  • 00:09:48
    print out whatever text is inside these
  • 00:09:51
    quotes and you're like hey could I use
  • 00:09:53
    single quotes here yep so just like the
  • 00:09:55
    same rule we had for print if you wanted
  • 00:09:57
    both of these things to be single quote
  • 00:10:00
    characters that would be fine too that
  • 00:10:02
    would also work and the same rules with
  • 00:10:04
    like using single quotes and printing
  • 00:10:06
    double quotes characters on the inside
  • 00:10:07
    or using double quotes and printing
  • 00:10:09
    single quote characters on the inside
  • 00:10:10
    that all works the same here but what
  • 00:10:13
    input does is it not only prints out the
  • 00:10:15
    text that is inside that particular
  • 00:10:18
    those particular quotes but what it will
  • 00:10:21
    then do is after it prints that text it
  • 00:10:24
    will sit there and wait for the user to
  • 00:10:26
    type something in and and after the user
  • 00:10:29
    types something in and hits enter that's
  • 00:10:31
    how the user indicates that they've
  • 00:10:32
    finished the text that they're typing
  • 00:10:34
    that text that the user has written
  • 00:10:38
    basically comes back and is put into a
  • 00:10:41
    variable so there's num1 thing here is a
  • 00:10:45
    variable and all a variable is is it's
  • 00:10:47
    basically some box inside the computer's
  • 00:10:50
    memory that has a name associated with
  • 00:10:53
    it in this case the name is num1 and
  • 00:10:55
    store some information and so the
  • 00:10:57
    information that's going to get stored
  • 00:10:59
    here is whatever the user typed in after
  • 00:11:01
    we ask them for num1 okay
  • 00:11:04
    and the important thing to keep in mind
  • 00:11:06
    as you saw with the program we just did
  • 00:11:08
    to add two numbers is that that input is
  • 00:11:10
    considered text even if the user entered
  • 00:11:13
    a number so if you want to take that
  • 00:11:16
    text and treat as a number you have to
  • 00:11:17
    change it to a number and we'll talk
  • 00:11:19
    about that in more detail in just a
  • 00:11:21
    second but that's the idea the input is
  • 00:11:22
    considered text okay and we'll talk more
  • 00:11:25
    about the input function as we continue
  • 00:11:27
    along in the class but I just really
  • 00:11:30
    want you to get a sense for the basics
  • 00:11:31
    of this is how you get information from
  • 00:11:34
    the user to create an interactive
  • 00:11:35
    program at least one form of creating an
  • 00:11:37
    interactive program okay so you're like
  • 00:11:40
    hey Maron you've been talking about this
  • 00:11:42
    variable thing and we called it num1
  • 00:11:44
    over here can you tell me a little bit
  • 00:11:46
    more about variables like what are they
  • 00:11:47
    really and how do they work sure and lo
  • 00:11:50
    and behold there just happens to be
  • 00:11:52
    another slide called what is a variable
  • 00:11:53
    and now I feel compelled to answer the
  • 00:11:55
    question what is a variable so let's
  • 00:11:57
    talk about that
  • 00:11:58
    a variable at a high level that we
  • 00:12:01
    talked about is just a place to store
  • 00:12:03
    information in a program if you're
  • 00:12:05
    familiar with the notion of a variable
  • 00:12:06
    from algebra like the notion of having
  • 00:12:08
    the variable X that represents some
  • 00:12:10
    number it's kind of the same idea right
  • 00:12:12
    you could think of that X as being a box
  • 00:12:14
    that really has some number in it that's
  • 00:12:16
    explicitly the way we think about it in
  • 00:12:18
    Python okay so every variable has a name
  • 00:12:22
    associated with it and it has some value
  • 00:12:24
    and the way we think about that is we
  • 00:12:27
    can create a new variable by assigning a
  • 00:12:30
    value to it and so the way we do the
  • 00:12:32
    assignment is with the equals okay and
  • 00:12:35
    so the idea here is the right hand side
  • 00:12:38
    is the value that we want to assign the
  • 00:12:41
    left hand side is the name of the
  • 00:12:43
    variable and the first time we actually
  • 00:12:46
    specify a particular name we are
  • 00:12:48
    creating that variable or sometimes what
  • 00:12:50
    people say is we're declaring the
  • 00:12:51
    variable it's like kind of like you're
  • 00:12:52
    standing on the Mountaintop and you're
  • 00:12:54
    like X you're declaring the variable
  • 00:12:57
    yeah that's just computer speak it
  • 00:12:58
    really means we're sort of creating the
  • 00:13:00
    variable and so what this does is it
  • 00:13:02
    creates a box for us the name of that
  • 00:13:04
    box is X so that's how I refer to the
  • 00:13:07
    contents of the box and what I'm going
  • 00:13:09
    to do is I'm going to take this 10 over
  • 00:13:11
    here this value that's on the right hand
  • 00:13:15
    side of the equal statement and put it
  • 00:13:18
    into whichever variable is named over
  • 00:13:20
    here on the left hand side and if it's
  • 00:13:22
    the first time I've named that variable
  • 00:13:23
    I actually create the box for it and so
  • 00:13:26
    what this does it takes the value 10
  • 00:13:27
    puts it in a box with the name X okay
  • 00:13:30
    and so I can also change the value so if
  • 00:13:34
    later on in my program I have another
  • 00:13:36
    line like x equals 5 what it does well X
  • 00:13:39
    already exists at this point it's
  • 00:13:41
    already been created but what it does is
  • 00:13:42
    it says okay what is on the right hand
  • 00:13:44
    side here it's the value 5 what I'm
  • 00:13:47
    going to do is put that into X so it
  • 00:13:49
    says that box already exists what I'm
  • 00:13:51
    going to do is now replace the value
  • 00:13:52
    here with A5 or can I make that a little
  • 00:13:55
    cleaner with the slides please yes I can
  • 00:13:57
    make it a little cleaner with the slides
  • 00:13:58
    so let me
  • 00:14:00
    erase what I'd written here and what I
  • 00:14:02
    get is the value 5 because that's what's
  • 00:14:04
    really the new value in my box now
  • 00:14:06
    notice the 10 no longer exists there for
  • 00:14:08
    x x now that box has the value 5
  • 00:14:11
    associated with it okay so the
  • 00:14:15
    interesting thing about thinking about
  • 00:14:16
    this is that we can on this right hand
  • 00:14:18
    side when we're doing something we can
  • 00:14:20
    not only just have a value we could
  • 00:14:22
    actually have a mathematical expression
  • 00:14:23
    so we could say something like x equals
  • 00:14:26
    five five plus seven and what goes on
  • 00:14:28
    here is the python comes along and says
  • 00:14:30
    let me evaluate what's on the right hand
  • 00:14:32
    side of the equal first so it says okay
  • 00:14:36
    what's five plus seven well that's the
  • 00:14:37
    value 12 so it figures out that that's a
  • 00:14:40
    12. that 12 is was now going to get
  • 00:14:42
    assigned to the Box named X so can we
  • 00:14:46
    assign the 12 to the X please ding and
  • 00:14:49
    you can see the 5 is now gone there's
  • 00:14:51
    now a 12 associated with the name X and
  • 00:14:55
    that's what's going on here basically is
  • 00:14:57
    we'll talk more about expressions in the
  • 00:15:00
    class on Wednesday okay
  • 00:15:01
    but the idea here is what we want to do
  • 00:15:03
    is we can create variables and put
  • 00:15:05
    values in them and that's a way of being
  • 00:15:07
    able to keep track of information so
  • 00:15:09
    that we can use it later in our program
  • 00:15:11
    or manipulate the information as we go
  • 00:15:13
    along
  • 00:15:15
    okay so let's talk a little bit more
  • 00:15:17
    about variable assignments it seems
  • 00:15:19
    straightforward but it has a few nuances
  • 00:15:21
    that are important to keep in mind so
  • 00:15:23
    the notion of using an equal sign in a
  • 00:15:26
    Python program is what we refer to as
  • 00:15:28
    assigning to a variable what assigning
  • 00:15:31
    means is you're just putting a value
  • 00:15:32
    into the box for the variable and so the
  • 00:15:35
    very first time that you assign to a
  • 00:15:36
    variable what you're actually doing is
  • 00:15:39
    creating that variable as we kind of
  • 00:15:40
    talked about so when we say like x
  • 00:15:42
    equals 10 for the first time we refer to
  • 00:15:44
    X is what actually creates the variable
  • 00:15:46
    but subsequent assignments give the
  • 00:15:48
    variable a new value so when we refer to
  • 00:15:51
    that X again or we refer to some
  • 00:15:52
    variable
  • 00:15:53
    subsequent times what we're doing is
  • 00:15:55
    just associating a new value with that
  • 00:15:57
    variable okay
  • 00:16:00
    so something to keep in mind and this is
  • 00:16:02
    super important because when people see
  • 00:16:04
    this equal sign they think of like
  • 00:16:05
    equality in mathematics and the
  • 00:16:08
    important thing is the equal sign is not
  • 00:16:10
    the same as a quality in mathematics the
  • 00:16:13
    equal sign in Python is actually what's
  • 00:16:15
    known as an assignment statement what
  • 00:16:17
    you're doing is assigning a value to a
  • 00:16:20
    variable you're not creating equality in
  • 00:16:22
    sort of a deep mathematical sense
  • 00:16:24
    wouldn't it be nice if we could create
  • 00:16:25
    equality in the world just by using an
  • 00:16:27
    equal sign well we can hope for that but
  • 00:16:29
    that's not the way things work in Python
  • 00:16:31
    okay so the way the assignment actually
  • 00:16:34
    works right when we use the SQL sign in
  • 00:16:37
    Python is what we do as we sort of saw
  • 00:16:39
    before is we first evaluate the right
  • 00:16:42
    hand side of the equal statement and
  • 00:16:44
    then we assign that value whatever value
  • 00:16:47
    we got after we evaluated the expression
  • 00:16:49
    on the right hand side of the equal to
  • 00:16:51
    the variable on the left hand side of
  • 00:16:53
    the equal statement okay so let me show
  • 00:16:55
    you an example of that
  • 00:16:57
    consider this code so when we say
  • 00:16:59
    something like total equals five you're
  • 00:17:01
    like oh well if I follow the steps
  • 00:17:04
    around told me this is the first time
  • 00:17:05
    I'm referring to total so what it does
  • 00:17:07
    is it's going to
  • 00:17:09
    evaluate the right hand side first
  • 00:17:11
    there's really no expression there to
  • 00:17:12
    evaluate it's just the value 5 and it's
  • 00:17:14
    going to assign that to a box called
  • 00:17:16
    total well I don't have that box yet
  • 00:17:18
    because this is the first time I'm doing
  • 00:17:20
    the assignments so we'll create a box
  • 00:17:22
    called total and it will put a five in
  • 00:17:25
    it okay now what happens if in my
  • 00:17:28
    program I see the statement total equals
  • 00:17:30
    total plus one
  • 00:17:32
    what does that mean right a
  • 00:17:34
    mathematician looks at that statement
  • 00:17:35
    and they go what
  • 00:17:37
    they might even do that again what that
  • 00:17:40
    doesn't make any sense how can total be
  • 00:17:42
    equal to Total plus one and the
  • 00:17:45
    important thing to keep in mind is that
  • 00:17:46
    this equals is not equality the equals
  • 00:17:50
    is an assignment statement so what
  • 00:17:52
    happens in an assignment we evaluate the
  • 00:17:55
    right hand side first how do we evaluate
  • 00:17:58
    the right hand side we come in here and
  • 00:17:59
    say this expression is total plus one so
  • 00:18:02
    what is total plus one well first I need
  • 00:18:04
    to look up total what's total I go over
  • 00:18:06
    to the box for total and I say oh the
  • 00:18:08
    value associated with total right now is
  • 00:18:10
    currently five so this is a five then I
  • 00:18:13
    add one to it because that's the
  • 00:18:14
    expression so what I get is 6.
  • 00:18:17
    what do I do with that six I take that 6
  • 00:18:20
    right and I assign it to the left hand
  • 00:18:23
    side what's the left hand side it's
  • 00:18:25
    total so it says take that 6 and put it
  • 00:18:28
    into total so it comes along here and
  • 00:18:30
    says oh I had this five before well I'm
  • 00:18:33
    getting rid of the five I'm putting in
  • 00:18:34
    the value 6 because that's what you told
  • 00:18:37
    me to assign there so what this line is
  • 00:18:39
    actually doing is this line is adding
  • 00:18:43
    one to Total that's all this line does
  • 00:18:45
    but notice it doesn't make any sense as
  • 00:18:47
    mathematical quality it does make sense
  • 00:18:50
    as an assignment statement which is to
  • 00:18:53
    evaluate the right hand side figure out
  • 00:18:55
    what the value is and assign it to the
  • 00:18:57
    left hand side even if you're
  • 00:18:58
    overwriting the old value that's
  • 00:19:00
    perfectly fine it's actually pretty
  • 00:19:01
    common to do statements like this in
  • 00:19:03
    Python okay now something you need to
  • 00:19:06
    consider this is kind of the additional
  • 00:19:08
    stuff I showed you at the bottom of the
  • 00:19:09
    slide
  • 00:19:10
    variables are only visible in the
  • 00:19:13
    functions in which they are created okay
  • 00:19:16
    that's what's known as the scope of the
  • 00:19:18
    variable it is not a mouthwash in the
  • 00:19:20
    case of python what scope says is where
  • 00:19:22
    does a variable live a variable lives
  • 00:19:25
    inside the function in which it was
  • 00:19:27
    created okay and so we'll talk more
  • 00:19:31
    about that in the next class and
  • 00:19:32
    actually future classes it's a pretty
  • 00:19:34
    important concept but that's the thing
  • 00:19:36
    to think about if you create a variable
  • 00:19:38
    inside of main it is only visible inside
  • 00:19:41
    main you can't refer to that same
  • 00:19:43
    variable from another function until you
  • 00:19:46
    learn some more that we'll get to in a
  • 00:19:47
    few days but for the time being when you
  • 00:19:50
    declare a variable that variable you can
  • 00:19:52
    only use inside that function okay and
  • 00:19:55
    we'll get more about that in the next
  • 00:19:57
    few classes but we're taking the steps
  • 00:19:59
    to sort of make them digestible as we go
  • 00:20:01
    along
  • 00:20:02
    okay so what about variable names like
  • 00:20:05
    back here right I had total and before I
  • 00:20:07
    had num1 and num2 like where do these
  • 00:20:10
    names come from well the thing about
  • 00:20:12
    variable names is you get to choose what
  • 00:20:15
    the variable names are they are your
  • 00:20:16
    choice but let me give you some of the
  • 00:20:18
    the guidelines from python of what the
  • 00:20:21
    variable name must include and what it
  • 00:20:23
    is nice to include okay so what of what
  • 00:20:26
    a variable name when you create a
  • 00:20:28
    variable name in Python what must it
  • 00:20:30
    have well the variable name must start
  • 00:20:32
    with either a letter or an underscore
  • 00:20:34
    character okay so there's the only
  • 00:20:36
    things in terms of the first character
  • 00:20:38
    of the name that it has to start with
  • 00:20:40
    after that
  • 00:20:42
    it can only contain digits or letters
  • 00:20:45
    digits and underscores notice I can't
  • 00:20:47
    start with a digit like the number two
  • 00:20:49
    right it has to start with a letter or
  • 00:20:50
    underscore but then it can contain
  • 00:20:51
    letters digits or underscores but it
  • 00:20:54
    can't contain other punctuation like I
  • 00:20:56
    can't contain dashes or exclamation
  • 00:20:58
    points or periods or something like that
  • 00:20:59
    those are not part of a variable valid
  • 00:21:02
    name for a variable
  • 00:21:04
    and final thing and this is a minor
  • 00:21:06
    thing but it's important to keep in mind
  • 00:21:08
    it can't be one of the built-in commands
  • 00:21:10
    in Python so like back in the day you
  • 00:21:12
    learned about for loops and while Loops
  • 00:21:14
    right those were actually python we'll
  • 00:21:16
    talk more about those in a few days but
  • 00:21:18
    for example the word for is actually a
  • 00:21:21
    built-in command in Python so you can't
  • 00:21:23
    have a variable called four because
  • 00:21:25
    python will get confused between that
  • 00:21:27
    and the notion of a for Loop okay
  • 00:21:29
    there's only about 200 or so of those
  • 00:21:31
    words in Python so it's not very likely
  • 00:21:33
    that she's randomly happened to hit on
  • 00:21:35
    one but just so you know
  • 00:21:37
    okay so that's what it must have
  • 00:21:39
    but the other thing you want to keep in
  • 00:21:42
    mind is that variable names are also
  • 00:21:44
    case sensitive so hello with a capital H
  • 00:21:47
    is not the same variable name as hello
  • 00:21:50
    with a lowercase H so variables are case
  • 00:21:53
    sensitive the same word but with
  • 00:21:55
    different casing for the letters will
  • 00:21:57
    actually be treated as different
  • 00:21:58
    variables so important point to keep in
  • 00:22:00
    mind keep your cases straight
  • 00:22:02
    those are the things the variables must
  • 00:22:04
    have there's also what we think of as
  • 00:22:05
    variables should have so python the
  • 00:22:08
    language doesn't enforce this but to do
  • 00:22:10
    good programming style this is what we
  • 00:22:13
    sort of would encourage you to do is to
  • 00:22:16
    have variable names be descriptive of
  • 00:22:18
    the value they are referring to so if
  • 00:22:20
    you have a variable called X X is not a
  • 00:22:23
    very descriptive name it's only a good
  • 00:22:25
    name if I say it's a referring to an x
  • 00:22:27
    coordinate inside like an x y coordinate
  • 00:22:30
    or something if you're referring to some
  • 00:22:31
    value in a grid like say where Carol was
  • 00:22:33
    right but otherwise if you're like
  • 00:22:36
    having a writing a program that keeps
  • 00:22:38
    track of a bank balance keeping track of
  • 00:22:40
    the balance with a variable X isn't
  • 00:22:42
    meaningful because when someone reads
  • 00:22:44
    that they're like what is this x thing
  • 00:22:45
    you might actually want that variable to
  • 00:22:47
    be something like balance or Bank
  • 00:22:50
    underscore balance to make it clear what
  • 00:22:52
    values actually being stored in the Box
  • 00:22:54
    for that variable and the other thing
  • 00:22:57
    you want to keep in mind is you're in
  • 00:22:58
    terms of the naming convention how we
  • 00:23:00
    write these things we want to write the
  • 00:23:02
    variable name in snake case
  • 00:23:04
    so just like you saw with the names for
  • 00:23:06
    functions the names for variables should
  • 00:23:08
    also be snake case which is separate
  • 00:23:10
    words that are joined by underscores
  • 00:23:12
    right so if I had some variable that
  • 00:23:13
    kept track say of the number of students
  • 00:23:15
    who are encode in place which is about
  • 00:23:16
    12 000 I might keep track of that in a
  • 00:23:19
    variable called num underscore students
  • 00:23:22
    okay so descriptive names is what we
  • 00:23:24
    want to have now to get a little bit
  • 00:23:26
    under the hood of what's actually going
  • 00:23:27
    on with variables in Python so you can
  • 00:23:29
    think about them a little bit more
  • 00:23:30
    really they're kind of like suitcases
  • 00:23:33
    like what suitcases their suitcases
  • 00:23:35
    inside my computer yeah well kind of
  • 00:23:37
    yeah so let's think about what it means
  • 00:23:39
    to kind of think about the python
  • 00:23:40
    suitcases let's say I had a variable X
  • 00:23:43
    that I had gave a value 12. okay so to
  • 00:23:47
    dig a little bit deeper for those of you
  • 00:23:48
    who are really interested when you store
  • 00:23:50
    information in a variable it really
  • 00:23:52
    becomes what we refer to as a python
  • 00:23:54
    object so underneath the hood what
  • 00:23:56
    python is kind of thinking about is I
  • 00:23:58
    have some object that I keep track of
  • 00:24:00
    this value 12.
  • 00:24:02
    okay and objects come in different sizes
  • 00:24:04
    and types because you kind of think of
  • 00:24:06
    object to suitcases that store different
  • 00:24:08
    kinds of things some of them may store
  • 00:24:10
    number some of them may store text and
  • 00:24:12
    so the size of how big that thing is and
  • 00:24:15
    what kind of information it contains can
  • 00:24:17
    be different for different kinds of
  • 00:24:18
    variables okay and the way to think
  • 00:24:21
    about this python object it is just the
  • 00:24:23
    suitcase that it's stored inside your
  • 00:24:25
    computer's memory so somewhere inside
  • 00:24:26
    your computer it has to keep track of
  • 00:24:28
    all the stuff while your Python program
  • 00:24:29
    is running okay and so the way I think
  • 00:24:32
    about is a little suitcase and if you
  • 00:24:33
    think about like the ram the memory
  • 00:24:35
    inside your computer that's where the
  • 00:24:37
    suitcases are getting stored
  • 00:24:38
    and each of these objects that is
  • 00:24:40
    storing some information takes up
  • 00:24:42
    different amounts of Ram or different
  • 00:24:43
    like bytes in your computer depending on
  • 00:24:46
    how you think about it but you don't
  • 00:24:48
    have to really worry about it you have
  • 00:24:50
    space for Millions potentially depending
  • 00:24:52
    on how much memory you have potentially
  • 00:24:53
    billions of these suitcases inside
  • 00:24:56
    python okay so really for our intents
  • 00:24:59
    and purposes the amount of memory you're
  • 00:25:01
    using is is going to be far less than
  • 00:25:04
    than the computer can actually handle
  • 00:25:06
    but that's the idea
  • 00:25:08
    now think about the suitcase in a little
  • 00:25:10
    bit more detail the way we want to think
  • 00:25:12
    about this luggage is the luggage has
  • 00:25:14
    tags associated with it and the tags
  • 00:25:16
    basically just specify the name for that
  • 00:25:18
    piece of luggage so the thinking about
  • 00:25:21
    having something like a variable num
  • 00:25:23
    students that I create with the value
  • 00:25:25
    700 what's really going on is that this
  • 00:25:29
    value over here that's my 700 is what's
  • 00:25:31
    going to get stored inside the suitcase
  • 00:25:33
    and then the suitcase is going to have
  • 00:25:35
    some tag associated with it which is the
  • 00:25:38
    name of the variable so that's how we
  • 00:25:40
    can keep track of it so here's my
  • 00:25:42
    suitcase that has the 700 in it and how
  • 00:25:44
    do I know how to refer to that suitcase
  • 00:25:46
    well it has a luggage tag that's numb
  • 00:25:48
    students that's the name of the variable
  • 00:25:51
    okay
  • 00:25:52
    and the thing that's interesting there
  • 00:25:54
    is if I were to have say I say my number
  • 00:25:56
    of students is 700 this might be for
  • 00:25:58
    example for one of the classes I teach
  • 00:26:00
    at Stanford might have 700 students and
  • 00:26:02
    then I see how many of the students
  • 00:26:04
    actually showed up in class that day and
  • 00:26:06
    I might want to keep track of that with
  • 00:26:07
    a variable called num in class and that
  • 00:26:10
    might be 550. so 550 students came today
  • 00:26:14
    so I want to assign the value 550 to num
  • 00:26:16
    in class and so what I get is a new
  • 00:26:18
    piece of luggage that has the value 550
  • 00:26:22
    in it and the way I refer to that piece
  • 00:26:24
    of luggage or that value is with its tag
  • 00:26:26
    that is numb in class which is just the
  • 00:26:29
    name for that variable or the tag for
  • 00:26:31
    that suitcase and so if I were to create
  • 00:26:33
    some new suitcase I want to see how many
  • 00:26:35
    students were absent in class that day
  • 00:26:37
    you're like Maron you actually have apps
  • 00:26:39
    and students occasionally yeah it'd be
  • 00:26:42
    great if you could show up to all the
  • 00:26:43
    lectures so what I do is I say I have
  • 00:26:45
    some expression here which is take the
  • 00:26:47
    number of students and subtract off the
  • 00:26:49
    number in the class so it says okay how
  • 00:26:51
    do I get number of students students
  • 00:26:52
    well I look up that luggage tag and look
  • 00:26:55
    at the value associated with it which is
  • 00:26:56
    the 700. so that would be 700 I want to
  • 00:27:00
    subtract from that so that's just the
  • 00:27:01
    minus sign like an arithmetic
  • 00:27:03
    the number of students in class so it
  • 00:27:06
    says okay here's my name go look up that
  • 00:27:08
    luggage tag that's the value 550. so
  • 00:27:11
    from 700 it's going to subtract off 550
  • 00:27:14
    and get the value 150 that's the result
  • 00:27:17
    what am I going to do with that 150 I'm
  • 00:27:20
    going to store it inside num absence so
  • 00:27:22
    it says oh num absent that's the first
  • 00:27:24
    time I've seen that name so I'm going to
  • 00:27:26
    create a new piece of luggage that has
  • 00:27:28
    that value 150 put into it and I'm going
  • 00:27:30
    to tag that piece of luggage with the
  • 00:27:33
    name of the variable num absent okay so
  • 00:27:36
    that's kind of what's going on
  • 00:27:37
    underneath the hood and the fun thing
  • 00:27:39
    about all of this
  • 00:27:40
    the thing you want to keep in mind I'm
  • 00:27:42
    just going to erase this to create a
  • 00:27:44
    little bit more space on the slide is
  • 00:27:46
    that when it comes down to it python
  • 00:27:48
    actually handles the baggage for you
  • 00:27:50
    that's the way I like to think about it
  • 00:27:51
    you know life is full of all kinds of
  • 00:27:53
    baggage that we run into at least in the
  • 00:27:55
    case of python the baggage that is the
  • 00:27:57
    variables is all handled for you so it's
  • 00:27:59
    the one that's like creating the
  • 00:28:00
    suitcases and tagging them for you and
  • 00:28:02
    going and looking them up when you need
  • 00:28:03
    it it's kind of nice I wish I had
  • 00:28:05
    something that did that like in my
  • 00:28:06
    everyday life as opposed to just python
  • 00:28:08
    but at least we have it for python
  • 00:28:11
    all right so if we think about these
  • 00:28:14
    suitcases we can also keep track of
  • 00:28:16
    something called a type and a type just
  • 00:28:18
    keeps track of what sort of information
  • 00:28:20
    is being stored in the suitcase okay so
  • 00:28:24
    that's all the type does is each
  • 00:28:25
    suitcase knows what type of information
  • 00:28:27
    it contains and so I'll show you the
  • 00:28:30
    difference at least the sub sampling of
  • 00:28:33
    some of the different kinds of or types
  • 00:28:34
    of information in Python and we'll sort
  • 00:28:37
    of see more as the class goes on so when
  • 00:28:39
    I coming back to my example with num
  • 00:28:41
    students equals 700
  • 00:28:43
    the 700 python actually knows is
  • 00:28:46
    actually an integer value it's a number
  • 00:28:48
    that happens to be an integer in this
  • 00:28:50
    case it has no decimal portion so what
  • 00:28:53
    it does it says I create the suitcase
  • 00:28:54
    with the value 700 I tag it with the
  • 00:28:56
    name of the variable num students and I
  • 00:28:59
    also keep track of what is the type of
  • 00:29:01
    that variable in this case the type is
  • 00:29:04
    int and that is short for integer so it
  • 00:29:07
    knows that what's stored in the suitcase
  • 00:29:09
    is a numerical value which is an integer
  • 00:29:11
    and the way python names that is as an
  • 00:29:14
    INT okay
  • 00:29:15
    similarly if I think about the value
  • 00:29:18
    that are stored in so this as I
  • 00:29:20
    mentioned is the value stored in a
  • 00:29:21
    suitcase that's an integer or an intent
  • 00:29:23
    python
  • 00:29:24
    but the suitcase keeps track of the
  • 00:29:26
    value that's stored there and I could
  • 00:29:28
    think about well what happens if I
  • 00:29:30
    wanted to store a real value so in a
  • 00:29:32
    similar sense I want to create a
  • 00:29:34
    suitcase that has a value associated
  • 00:29:36
    with it but it's going to have a
  • 00:29:37
    different type so let's say I wanted to
  • 00:29:39
    have a real value how do I do that let's
  • 00:29:42
    say I said the number of students with
  • 00:29:43
    700.0
  • 00:29:45
    and the interesting thing here is by
  • 00:29:48
    having the decimal point here that's
  • 00:29:50
    what tells python this is no longer an
  • 00:29:53
    integer this is a real value and you
  • 00:29:55
    might say but it's point zero meron
  • 00:29:57
    doesn't that still make it an integer no
  • 00:29:59
    integers don't have decimal values even
  • 00:30:02
    if it's a point zero so when python sees
  • 00:30:04
    the decimal it knows that this thing is
  • 00:30:07
    actually a real value so it's the
  • 00:30:09
    decimal point that is what's telling
  • 00:30:10
    python it's a real value so the value
  • 00:30:13
    here is 700.0 and I want to store that
  • 00:30:15
    in a variable called num students so
  • 00:30:18
    what does it do
  • 00:30:19
    and says okay I'm going to have a
  • 00:30:21
    suitcase that has a 700 and the type for
  • 00:30:25
    the suitcase that's stored in there is
  • 00:30:27
    kind of funky I'm going to call it a
  • 00:30:28
    float because it turns out that in
  • 00:30:31
    Python the way it refers to the type for
  • 00:30:33
    a real numeric value is a float and that
  • 00:30:37
    name actually comes from the IEEE which
  • 00:30:40
    is the institute for electronics and
  • 00:30:42
    electrical engineers that came up with a
  • 00:30:44
    standard a few years back called or many
  • 00:30:46
    years back called The Floating Point
  • 00:30:48
    standard and it is a standard for
  • 00:30:50
    keeping track of real values or decimal
  • 00:30:53
    numbers inside a computer and so since
  • 00:30:56
    then programmers have been referring to
  • 00:30:58
    real values as floats because they are
  • 00:31:01
    represented using this floating Point
  • 00:31:03
    standard so that's why floats actually
  • 00:31:05
    the way to think about it is this means
  • 00:31:07
    a real number in Python okay and so what
  • 00:31:12
    we get is a suitcase that stores 700.0
  • 00:31:14
    it knows that its type is float which is
  • 00:31:16
    a real value and it is tagged with the
  • 00:31:19
    tag students okay you're like so what
  • 00:31:22
    happened to the old one well this old
  • 00:31:23
    one kind of went away if we now specify
  • 00:31:26
    this
  • 00:31:27
    okay and so there are different kinds of
  • 00:31:30
    types in Python so let me tell you about
  • 00:31:32
    some of the different types there are
  • 00:31:34
    integer values so these are known as INT
  • 00:31:37
    in python as we just kind of talked
  • 00:31:38
    about and the way it knows it's an
  • 00:31:40
    integer is there's no decimal point but
  • 00:31:42
    you can have positive or negative values
  • 00:31:44
    or zeros so if I say x equals 10 or I
  • 00:31:46
    say y equals negative 2 python knows
  • 00:31:49
    those are integer values because they're
  • 00:31:51
    numeric but they have no decimal point
  • 00:31:53
    so how does python figure out real
  • 00:31:55
    numbers real numbers the type in Python
  • 00:31:58
    is called a float as we just talked
  • 00:32:01
    about it has a decimal point and again
  • 00:32:03
    these can be negative values because
  • 00:32:04
    real values can be negative so x equals
  • 00:32:06
    5.0 or y equals negative 3.7 python
  • 00:32:10
    knows those are Floats or real values
  • 00:32:13
    because they have a decimal point even
  • 00:32:14
    if the decimal point is a zero it's
  • 00:32:16
    still considered a floating Point number
  • 00:32:18
    because it has a decimal so it's
  • 00:32:20
    considered a real value
  • 00:32:22
    okay there's also something we refer to
  • 00:32:24
    as strings and a string is just a piece
  • 00:32:28
    of text it kind of comes from the day of
  • 00:32:29
    thinking about a bunch of characters
  • 00:32:30
    that are strung together in a string and
  • 00:32:33
    so that's why a string is how we were
  • 00:32:36
    the name is how we refer to text
  • 00:32:37
    characters but the type in Python is
  • 00:32:40
    string and basically you've seen strings
  • 00:32:42
    before we just didn't really call them
  • 00:32:44
    strings much before
  • 00:32:46
    um they are between either single quotes
  • 00:32:47
    or double quotes So if I say x equals
  • 00:32:50
    hello inside double quotes what that is
  • 00:32:52
    is X is the box that contains the string
  • 00:32:55
    hello which is really the characters
  • 00:32:58
    h-e-l-l-o or I could say y equals the
  • 00:33:02
    string 10 in which case Y is a box that
  • 00:33:04
    contains not the number 10 but the piece
  • 00:33:08
    of text one zero and that is different
  • 00:33:11
    so that's important to keep in mind for
  • 00:33:13
    example the textual piece of information
  • 00:33:15
    10 is not the same as the number or
  • 00:33:19
    integer value 10. important to keep in
  • 00:33:22
    mind and you short it might have been
  • 00:33:23
    thinking that from our program before to
  • 00:33:25
    add two numbers when we had to convert
  • 00:33:27
    the text to a number we'll revisit that
  • 00:33:29
    but now you understand what's going on
  • 00:33:31
    is
  • 00:33:32
    the text 10 is different than the actual
  • 00:33:35
    value 10 the numeric value 10. okay so
  • 00:33:39
    let me clean up this slide a little bit
  • 00:33:41
    just so you can now read this text down
  • 00:33:43
    here
  • 00:33:44
    and so as I mentioned with 10 just to
  • 00:33:47
    put it on the slide the string five for
  • 00:33:49
    example is not the same as the integer
  • 00:33:50
    five there is also a particular notion
  • 00:33:53
    that we'll get to in a little while
  • 00:33:54
    it'll probably be a week or so before we
  • 00:33:56
    get there called the Bool or a Boolean
  • 00:33:58
    which actually is named after the uh
  • 00:34:01
    mathematician or logician George boole
  • 00:34:04
    that comes from his name and basically
  • 00:34:06
    Boolean value is just a logical value
  • 00:34:08
    it's either true or false with a capital
  • 00:34:11
    T or a capital f okay so I can actually
  • 00:34:13
    have a variable
  • 00:34:15
    that has the value true assigned to it
  • 00:34:17
    or has the value false assigned to it
  • 00:34:19
    and that's those are the only two values
  • 00:34:21
    that a Boolean can the type Boolean
  • 00:34:23
    consorts and store and that's how it
  • 00:34:25
    knows it's a Boolean because it's true
  • 00:34:27
    or false okay and we'll talk more about
  • 00:34:29
    strings and Bulls in a few days for
  • 00:34:31
    right now we're just going to focus on
  • 00:34:32
    the numeric types ins and floats okay
  • 00:34:35
    now natural question that oftentimes
  • 00:34:38
    comes up when we talk about integers and
  • 00:34:40
    we talk about floating Point numbers if
  • 00:34:42
    someone says why do we have both of
  • 00:34:44
    these like I thought all integers are
  • 00:34:46
    actually real values so why don't we
  • 00:34:48
    just have real values huh I'm Aaron huh
  • 00:34:51
    well there's a reason and it's not just
  • 00:34:53
    because I wasn't the one who designed
  • 00:34:55
    python this has actually been going on
  • 00:34:56
    for years with lots of programming
  • 00:34:58
    languages and what it comes down to is
  • 00:35:00
    the difference between how much versus
  • 00:35:02
    how many so let's talk a little bit
  • 00:35:04
    about that so you could ask me hey Maron
  • 00:35:07
    how much do you weigh go ahead and ask
  • 00:35:09
    right now while you're sitting at your
  • 00:35:10
    computer hey Maron how much do you weigh
  • 00:35:13
    yeah that's kind of a personal question
  • 00:35:15
    but I'll answer it anyway so if I were
  • 00:35:18
    to tell you I weigh 163 pounds you would
  • 00:35:21
    say okay if I were to tell you I weigh
  • 00:35:26
    163.523 pounds you might say okay that
  • 00:35:30
    still makes sense it's a little weird
  • 00:35:31
    that you might know your weight to like
  • 00:35:33
    three decimal points but it still makes
  • 00:35:34
    sense
  • 00:35:35
    and that's the idea is that it's a real
  • 00:35:38
    valued answer the notion of how much
  • 00:35:41
    and the thing about real values is
  • 00:35:42
    there's no next value what does it mean
  • 00:35:45
    what's the next real value after 1.0 is
  • 00:35:49
    it 2.0 no because we have 1.1 that would
  • 00:35:52
    be in between so as an x value after 1.0
  • 00:35:54
    1.1 no because we have 1.01 we have
  • 00:35:59
    1.0001 we have 1.00 I could keep going
  • 00:36:04
    one
  • 00:36:05
    but you get the notion right there's no
  • 00:36:08
    next value for real numbers and so we
  • 00:36:10
    can't count with real numbers the way we
  • 00:36:12
    count with integers
  • 00:36:14
    so that's the way to think about this is
  • 00:36:16
    that things where we think about how
  • 00:36:18
    much are floating Point numbers their
  • 00:36:21
    real values sometimes we might have a
  • 00:36:23
    zero after the decimal point that's fine
  • 00:36:25
    sometimes we might have more digits but
  • 00:36:27
    really there is also no next number okay
  • 00:36:30
    now contrast that with the question of
  • 00:36:32
    how many children do you have yes that's
  • 00:36:35
    one of my children super cute that was a
  • 00:36:38
    few years ago
  • 00:36:39
    um but you could ask how many children
  • 00:36:41
    do you have and I would say two now if
  • 00:36:44
    you asked how many children do you have
  • 00:36:45
    and I said 1.7
  • 00:36:48
    that might creep you out a little bit
  • 00:36:50
    you're like what does that mean you have
  • 00:36:52
    1.7 children like is his wife pregnant
  • 00:36:54
    like what's going on what does that mean
  • 00:36:56
    it doesn't make sense to the question of
  • 00:36:58
    how many do you have to answer with a
  • 00:37:01
    real value right the idea here is that
  • 00:37:04
    the answer is an integer and there's a
  • 00:37:06
    well-defined next number what's the
  • 00:37:08
    value after one it's two what's the
  • 00:37:10
    value of two is three so if we had two
  • 00:37:12
    kids right now then we added one we'd
  • 00:37:15
    have a third child then a fourth child
  • 00:37:16
    then a fifth child then we would go
  • 00:37:18
    insane we're not having any more
  • 00:37:20
    children that's how I'm born right now
  • 00:37:21
    but the point to think about is that
  • 00:37:24
    there is a well-defined next number so
  • 00:37:26
    we can count and oftentimes in our
  • 00:37:28
    programs we want to count and so that's
  • 00:37:31
    why this notion of having an integer as
  • 00:37:33
    a separate type is very important in
  • 00:37:35
    programming even though we still have
  • 00:37:37
    real values okay so that's the
  • 00:37:39
    difference between them is think about
  • 00:37:41
    that question of how much versus how
  • 00:37:43
    many when you're answering the question
  • 00:37:44
    of how much or how many how many is an
  • 00:37:48
    integer about okay
  • 00:37:50
    so now after we've gone through all this
  • 00:37:53
    stuff we can come back to the program
  • 00:37:55
    that you saw at the beginning and now
  • 00:37:57
    you can go through this with tact and
  • 00:37:59
    Verve because you're like yeah I know
  • 00:38:01
    all the pieces and I know what's going
  • 00:38:02
    on so let's go through it with some new
  • 00:38:04
    found confidence around programming so
  • 00:38:07
    here's our program right now we can go
  • 00:38:09
    through all the pieces of the program
  • 00:38:10
    and see what it's doing first line comes
  • 00:38:12
    along is our print statement it is
  • 00:38:14
    printing and you now know it is printing
  • 00:38:16
    a string to the screen right so what
  • 00:38:18
    it's displaying is something with type A
  • 00:38:20
    String which is just inside these quote
  • 00:38:22
    characters and that's what it prints on
  • 00:38:24
    the terminal here okay
  • 00:38:27
    so then when we come along and we do the
  • 00:38:29
    input statement the input statement
  • 00:38:31
    prints some text which you know is a
  • 00:38:33
    string on the screen it writes out enter
  • 00:38:36
    first number and is waiting there for
  • 00:38:37
    the user input when the user enters
  • 00:38:39
    their input what we get back is their
  • 00:38:41
    input as a string we get it back as a
  • 00:38:44
    piece of text and so we get back that
  • 00:38:47
    input we assign it to num1 so num1 gets
  • 00:38:51
    created as a box with the name or the
  • 00:38:53
    tag
  • 00:38:54
    one and what's inside that box is the
  • 00:38:57
    text of the value 9. now we know that
  • 00:39:00
    the text of the value 9 is not the same
  • 00:39:02
    as the number nine what we actually want
  • 00:39:05
    to do is say hey you know what from that
  • 00:39:07
    text I want you to convert that text to
  • 00:39:09
    the actual integer equivalent that's
  • 00:39:11
    what this line does so it says take
  • 00:39:13
    what's in Num which is the string nine
  • 00:39:16
    convert it to an ins and so it looks
  • 00:39:18
    like we're calling a function called int
  • 00:39:20
    on this which in some sense is kind of
  • 00:39:22
    what's going on although we would refer
  • 00:39:24
    to this is what's called a cast which
  • 00:39:26
    we're changing the type of this thing
  • 00:39:29
    we would say take that and convert it to
  • 00:39:32
    the integer 9 and then what you're going
  • 00:39:35
    to do is you're going to assign that
  • 00:39:36
    back to num1 so it says okay now I've
  • 00:39:39
    taken this string nine I converted it to
  • 00:39:42
    the integer nine and that's what I'm now
  • 00:39:43
    going to put back into the box for num1
  • 00:39:45
    so that's what happens after we do that
  • 00:39:48
    line so the input command gives you back
  • 00:39:50
    a string even if the user types in a
  • 00:39:52
    number and so what we need to do is we
  • 00:39:55
    need to convert that string to an
  • 00:39:57
    integer and so when we do this line over
  • 00:40:00
    here it converts that string to an
  • 00:40:03
    integer and that's why we have the 9 as
  • 00:40:05
    an integer now notice it's not inside
  • 00:40:06
    quotes inside the box for num1 and now
  • 00:40:10
    we can go through that process for num2
  • 00:40:11
    so we call
  • 00:40:13
    if we were to call num2 we would now get
  • 00:40:16
    a new piece of text for num2 but what I
  • 00:40:19
    want to do is rather than continuing
  • 00:40:21
    with the rest of the program at this
  • 00:40:22
    point I want to go and kind of look at
  • 00:40:24
    the luggage that's underneath okay or
  • 00:40:27
    the variable so show me the luggage so
  • 00:40:30
    when I call the input statements right
  • 00:40:32
    and it's saying enter first number what
  • 00:40:35
    I get from the luggage point of view I
  • 00:40:36
    just showed you is the Box Point of View
  • 00:40:38
    but what I get is from the luggage point
  • 00:40:39
    of view is I get the text 9 which is of
  • 00:40:42
    type string because it's a piece of text
  • 00:40:44
    that is tagged with the tag or the name
  • 00:40:47
    num1 that's the name of our variables
  • 00:40:49
    okay then when I do this line what's
  • 00:40:52
    happening is it says go look up num1
  • 00:40:54
    this is the string nine convert it to
  • 00:40:58
    the integer equivalence for whatever
  • 00:41:00
    that string is saying that would be the
  • 00:41:02
    value 9 and then what I want you to do
  • 00:41:05
    is I want to have that 9 put into a
  • 00:41:07
    suitcase with the tag num1 so what it
  • 00:41:10
    does it says okay create a new suitcase
  • 00:41:12
    it creates a new suitcase that has the
  • 00:41:14
    value 9 in it which is now of type
  • 00:41:16
    integer so that's this 9 over here and
  • 00:41:19
    what it's going to do is say tag the
  • 00:41:21
    suitcase with the tag num1 so that
  • 00:41:24
    suitcase gets tagged
  • 00:41:26
    with the tag for num1 okay and what
  • 00:41:30
    happens is you might say but num1 was
  • 00:41:33
    this other lug piece of luggage over
  • 00:41:34
    here not anymore what it basically does
  • 00:41:36
    it says this tag no longer refers to
  • 00:41:39
    this thing over here this tag num1 is
  • 00:41:42
    now referring to this new suitcase that
  • 00:41:44
    has a 9 and an integer okay and the way
  • 00:41:48
    you could conceptually think about that
  • 00:41:50
    that's easier to kind of think about is
  • 00:41:51
    I have a box called num1 and the
  • 00:41:55
    contents of that box are now changing
  • 00:41:57
    from the string nine to the numeric
  • 00:41:59
    value nod that's kind of conceptually
  • 00:42:01
    the easier way to think about it but if
  • 00:42:03
    you care about the little details the
  • 00:42:05
    way to think about the details
  • 00:42:06
    underneath the hood is I've actually
  • 00:42:08
    created a new piece of luggage and sort
  • 00:42:10
    of moved the tag to now be referring to
  • 00:42:12
    that piece of luggage okay
  • 00:42:15
    so coming back to our program here
  • 00:42:17
    that's what happened in that line from
  • 00:42:20
    underneath the hood when you actually
  • 00:42:21
    saw the luggage now I can continue
  • 00:42:23
    executing the rest of this program so I
  • 00:42:25
    come along and say enter the second
  • 00:42:27
    number as you know the input statement
  • 00:42:29
    is going to get a piece of text from the
  • 00:42:30
    user if the user types 17 and hits enter
  • 00:42:33
    I get back the text 17 and so on the
  • 00:42:36
    next line I'm going to take that text 17
  • 00:42:38
    convert it to its integer equivalent
  • 00:42:40
    which is the number 17 and then assign
  • 00:42:43
    that back to num2 so that will turn num2
  • 00:42:46
    here into the integer value 17. okay and
  • 00:42:50
    you can think about the luggage
  • 00:42:52
    underneath the hood like we just saw in
  • 00:42:53
    the last slide but if it's conceptually
  • 00:42:55
    easier to think of it as just a box with
  • 00:42:57
    a name that we put a value in that works
  • 00:42:59
    just fine okay and then on our next line
  • 00:43:02
    you can say hey we have an assignment
  • 00:43:04
    statement for total so it's going to
  • 00:43:06
    evaluate the right hand side and assign
  • 00:43:09
    that to the left hand side yep that's
  • 00:43:11
    what it's doing that's actually what all
  • 00:43:12
    of these equal statements or assignments
  • 00:43:14
    have done it's just that in previous
  • 00:43:16
    cases the way we needed to evaluate the
  • 00:43:18
    right hand side was for example to do
  • 00:43:20
    the input statements or to convert a
  • 00:43:22
    value to its integer form here we have a
  • 00:43:24
    mathematical expression to convert num1
  • 00:43:26
    or to add num1 and num2 so looks up num1
  • 00:43:29
    which is nine looks up num2 which is the
  • 00:43:32
    17 computes 9 plus 17 which gives us the
  • 00:43:35
    value 26 and is going to put that into a
  • 00:43:38
    box named total and so that's what
  • 00:43:40
    happens when we execute that line
  • 00:43:41
    there's our box total with the value 26
  • 00:43:44
    in it so final line for what we're going
  • 00:43:46
    to do
  • 00:43:47
    what does this final line going to do
  • 00:43:49
    now we can actually tease this whole
  • 00:43:51
    thing apart so before I show you what
  • 00:43:53
    this is going to print on the screen
  • 00:43:55
    actually let me show you what's going to
  • 00:43:56
    print on the screen and then I'll tell
  • 00:43:57
    you how we got that it's going to print
  • 00:43:59
    out the total is the value 26 and then a
  • 00:44:03
    period so it's going to print out
  • 00:44:05
    the total is 26 in Period on the screen
  • 00:44:08
    but what do these plus signs mean here
  • 00:44:11
    and what is this string thing here so
  • 00:44:13
    now we can sort of understand those
  • 00:44:15
    better by deconstructing this a little
  • 00:44:18
    bit so what's going on with the print
  • 00:44:19
    statement what's going on with the print
  • 00:44:21
    statement is what we're doing is we're
  • 00:44:24
    trying to create and add strings
  • 00:44:27
    together so we say the total is that's
  • 00:44:29
    one string we say total which is the
  • 00:44:31
    value 26. I want the string aversion of
  • 00:44:34
    that which is the text 26 and then I
  • 00:44:38
    want to add to that a period which is
  • 00:44:40
    just a textual period so to understand
  • 00:44:43
    what's going on with these plus signs
  • 00:44:45
    these plus signs in Python are actually
  • 00:44:48
    what's known as concatenation they sort
  • 00:44:50
    of concatenate or the way you can think
  • 00:44:52
    of is they just sort of put strings
  • 00:44:54
    together so let me show you an example
  • 00:44:55
    of that
  • 00:44:57
    let's say I had a string whose name was
  • 00:44:59
    Stir one or string one that was just the
  • 00:45:01
    string high and then I had string two
  • 00:45:04
    which was just the space so there's
  • 00:45:05
    actually a space character in here
  • 00:45:07
    between the two double quotes then I had
  • 00:45:09
    some other string that was there and I
  • 00:45:11
    said I want to have string four which is
  • 00:45:13
    string one plus string two plus string
  • 00:45:15
    three you're like you're adding strings
  • 00:45:17
    what does that mean well adding strings
  • 00:45:19
    means you're concatenating together the
  • 00:45:21
    text so what I actually get here is a
  • 00:45:23
    box for string four and what that box
  • 00:45:25
    has in it is it has the string High then
  • 00:45:29
    a space for string two and then there
  • 00:45:32
    for string three I've concatenated these
  • 00:45:35
    three strings together the original
  • 00:45:37
    string string one string two and string
  • 00:45:39
    three are not changed I've created a new
  • 00:45:41
    string string four which is essentially
  • 00:45:43
    a copy of string one concatenated to the
  • 00:45:47
    space concatenated with a copy of string
  • 00:45:49
    two and it's a new string okay
  • 00:45:52
    so these plus signs really what they do
  • 00:45:55
    when you apply plus signs to Strings
  • 00:45:57
    they're sort of concatenating strings
  • 00:45:58
    together and that's what's going on up
  • 00:46:00
    here I'm concatenating the total is to
  • 00:46:03
    26 to period and then I'm printing that
  • 00:46:06
    whole thing out
  • 00:46:07
    okay and so total is an integer so we
  • 00:46:11
    need to create a string version of it to
  • 00:46:13
    be able to concatenate it into Strings
  • 00:46:14
    because a string doesn't know how to
  • 00:46:16
    concatenate an integer onto it it says I
  • 00:46:18
    know how to put strings together so if I
  • 00:46:20
    want to have this thing that was an
  • 00:46:21
    integer and I want to put it into the
  • 00:46:23
    middle of a string I use Stir which is
  • 00:46:26
    my short for string but we actually just
  • 00:46:28
    say Str in Python and then the thing I
  • 00:46:31
    want to convert to a string and what it
  • 00:46:33
    gives me back is the string or textual
  • 00:46:35
    equivalent of that so that would be the
  • 00:46:37
    value 26 the way you could think about
  • 00:46:39
    it is inside quotes because it's a piece
  • 00:46:41
    of text okay
  • 00:46:43
    and so that string version of total is a
  • 00:46:46
    new value that's concatenated in to
  • 00:46:48
    produce the final string that's printed
  • 00:46:50
    which is
  • 00:46:51
    the total is 26 period
  • 00:46:57
    okay so that's thing you want to keep in
  • 00:46:59
    mind total itself is still just the
  • 00:47:02
    variable that has the value 26 in it so
  • 00:47:04
    total is not actually changed what it
  • 00:47:06
    does is just says here I want you to
  • 00:47:09
    create a string version of 26 and what
  • 00:47:11
    I'm going to do with that is append it
  • 00:47:12
    into this larger string but the original
  • 00:47:14
    Total is not changed okay that's an
  • 00:47:16
    important point to keep in mind
  • 00:47:18
    all right I didn't want the highlighter
  • 00:47:20
    I wanted the eraser
  • 00:47:22
    all right so the original value in total
  • 00:47:26
    is still an INT that's something you
  • 00:47:27
    want to keep in mind so let's come back
  • 00:47:29
    to our program and execute our final
  • 00:47:31
    line together so now we know what that
  • 00:47:33
    line is doing it's actually creating
  • 00:47:34
    this string the total is 26 and that
  • 00:47:37
    string is what's getting printed out by
  • 00:47:39
    the print command Okay so that's what's
  • 00:47:42
    going on there I want to give you one
  • 00:47:44
    more side note about the print statement
  • 00:47:46
    okay and the side note about the print
  • 00:47:49
    statements is you can actually print
  • 00:47:50
    numbers directly without converting them
  • 00:47:54
    to Strings okay and so let me show you
  • 00:47:57
    how that works so let's say we add a
  • 00:47:59
    main program and so inside main I have X
  • 00:48:01
    is 10 so X is a variable with the value
  • 00:48:03
    10 in it and you know the type of that
  • 00:48:05
    is going to be
  • 00:48:07
    int right this is going to be an integer
  • 00:48:09
    because it has no decimal point and I
  • 00:48:12
    have Y which is 3.5 and the type for y
  • 00:48:15
    is going to be
  • 00:48:17
    float because it's a real value right
  • 00:48:19
    because it has a decimal point I can
  • 00:48:22
    just say print X and print y so here I
  • 00:48:25
    don't need to convert them to Strings if
  • 00:48:26
    I'm just printing the x or I'm just
  • 00:48:28
    printing the Y I don't need to convert
  • 00:48:31
    them to Strings I can just print
  • 00:48:32
    directly and this will actually if I
  • 00:48:35
    look at the terminal
  • 00:48:36
    this line will print out the 10 this
  • 00:48:39
    line will point out to print out the 3.5
  • 00:48:41
    but if I want some other text on that
  • 00:48:43
    same line then I would say if I want to
  • 00:48:46
    say something like x equals and the
  • 00:48:47
    value of x I would could say print out x
  • 00:48:51
    equals and then the string version of X
  • 00:48:53
    which would be the string 10 concatenate
  • 00:48:57
    those together so that would be the
  • 00:48:58
    string x equals 10 and that's what gets
  • 00:49:01
    printed on my terminal okay
  • 00:49:04
    so that's the thing I can print out
  • 00:49:06
    directly without converting to a string
  • 00:49:08
    if I have numbers if I want to turns out
  • 00:49:11
    there's another way of doing this in
  • 00:49:13
    Python 2 because python has multiple
  • 00:49:15
    ways to do almost everything so let me
  • 00:49:17
    show you that because you'll constant
  • 00:49:19
    you'll you'll commonly see this other
  • 00:49:21
    version of doing prints in python as
  • 00:49:23
    well you can also print multiple items
  • 00:49:26
    of different types by separating them
  • 00:49:29
    with commas inside a print statement so
  • 00:49:32
    what do I mean by that
  • 00:49:33
    what I mean by that is that by default a
  • 00:49:36
    space is going to be printed between
  • 00:49:38
    each item that are separated by commas
  • 00:49:41
    and let me just show you an example so
  • 00:49:43
    what I can say is in my main program I
  • 00:49:45
    have X which is going to have value 4
  • 00:49:48
    and Y which is going to have value 0.2
  • 00:49:51
    if I want to print out X and Y I can
  • 00:49:55
    print X comma Y and so what I will get
  • 00:49:57
    on the screen on the terminal when I do
  • 00:49:59
    that is it will print out X which is the
  • 00:50:01
    4 then it will print out a space and
  • 00:50:04
    then it will print out y
  • 00:50:07
    and it will print out Y which is the 1.2
  • 00:50:09
    so notice I just have the different
  • 00:50:11
    variables separated by commas and with
  • 00:50:13
    those commas basically get treated as is
  • 00:50:16
    spaces when things are printed out I can
  • 00:50:18
    also into indisperse variables and text
  • 00:50:21
    with commas if I want to print stuff out
  • 00:50:23
    so if I say print x equals comma then X
  • 00:50:27
    and then and Y equals which is a string
  • 00:50:30
    comma Y what this is going to print is x
  • 00:50:34
    equals then a space then the value of x
  • 00:50:37
    notice I don't convert X to a string
  • 00:50:40
    here I just have the variable X it
  • 00:50:42
    prints out its actual value then I get
  • 00:50:44
    another space as a result of this comma
  • 00:50:46
    so I get another space here I get and Y
  • 00:50:49
    equals and then comma y I get another
  • 00:50:52
    space and then the value of y so in many
  • 00:50:56
    python programs this is actually a
  • 00:50:58
    common form to write them as you into
  • 00:51:00
    you separate out the text and the
  • 00:51:02
    variables you want to print with commas
  • 00:51:04
    and those commas just get turned into
  • 00:51:06
    spaces into the default case there are
  • 00:51:08
    actually things we'll look at later
  • 00:51:09
    where we can actually not have it print
  • 00:51:11
    this space is the separator but for
  • 00:51:13
    right now we don't want to worry about
  • 00:51:15
    that so it will just the way to think
  • 00:51:17
    about it is
  • 00:51:18
    I'm printing out multiple things this is
  • 00:51:21
    a string this is an INT this is a string
  • 00:51:25
    and this is a float and it's fine to
  • 00:51:27
    print out multiple things if they're
  • 00:51:29
    separated by commas and the commas just
  • 00:51:31
    get treated as spaces okay
  • 00:51:34
    so you got to see a bunch of stuff you
  • 00:51:36
    just wrote your first Python program we
  • 00:51:39
    learned a lot about variables we
  • 00:51:40
    deconstructed a program so you are on
  • 00:51:43
    top of the mountain of today's goals you
  • 00:51:45
    are just totally rocking out right now
  • 00:51:46
    you got your introduction to python you
  • 00:51:49
    understood your variables you are what
  • 00:51:51
    we would call a happy happy camper or at
  • 00:51:53
    least I would hope
  • 00:51:55
    so the final thing I'd like to do with
  • 00:51:57
    you now is to take this add to numbers
  • 00:52:00
    program and actually run it in the
  • 00:52:02
    online development environment so you
  • 00:52:04
    can see it running you can try some
  • 00:52:06
    things out and you can see how things
  • 00:52:07
    work so what I want to do now is share
  • 00:52:10
    my screen with you to go over to the
  • 00:52:12
    online development environment and
  • 00:52:14
    that's going to bring us right over to
  • 00:52:17
    here so hopefully you are now seeing the
  • 00:52:20
    code for add to numbers dot py inside of
  • 00:52:23
    our online development environment all
  • 00:52:25
    the stuff that looks familiar to you we
  • 00:52:27
    have the comments at the top we have our
  • 00:52:29
    main program and now you know all the
  • 00:52:31
    pieces of how the main program works so
  • 00:52:33
    we can go ahead and run this and see
  • 00:52:35
    what happens so as you know in our
  • 00:52:37
    online development environment we can
  • 00:52:39
    just hit the Run button and so if we hit
  • 00:52:41
    the Run button what we're going to see
  • 00:52:42
    is the program starts running it prints
  • 00:52:44
    out on the screen this program adds two
  • 00:52:46
    numbers and so that shows up in our
  • 00:52:48
    terminal down here and it asks for the
  • 00:52:50
    first number so let's say for the first
  • 00:52:52
    number I enter an eight and that number
  • 00:52:54
    is going going to get stored in the
  • 00:52:56
    variable num1 it's actually going to get
  • 00:52:58
    converted into an integer from the text
  • 00:53:02
    8 that will get converted to the integer
  • 00:53:04
    8 and store it in num1 and then the
  • 00:53:06
    program is going to ask us to enter the
  • 00:53:08
    second number so that second number
  • 00:53:09
    maybe I will say 13 and when I hit enter
  • 00:53:12
    that 13 is going to get stored in num2
  • 00:53:15
    it's then going to get converted to the
  • 00:53:17
    integer version of 13 and stored again
  • 00:53:20
    in the variable num2 we're going to
  • 00:53:23
    compute total as the total of num1 which
  • 00:53:26
    is the 8 and num2 which is the 13 so
  • 00:53:28
    that'll be 21 that will get stored into
  • 00:53:30
    total and then we'll print out the total
  • 00:53:33
    is the text version of total which is 21
  • 00:53:36
    and then a period at the end and that's
  • 00:53:39
    what gets printed out here in our online
  • 00:53:41
    development environment now this is a
  • 00:53:44
    really fun time and you might say this
  • 00:53:45
    is great I want to do it again go ahead
  • 00:53:48
    do it again do it as many times as you
  • 00:53:49
    want it's just a fun time so I'm going
  • 00:53:51
    to run the program again and when it
  • 00:53:53
    runs again I can try some different
  • 00:53:54
    numbers so I can say you know what I
  • 00:53:56
    want to do I want to add 7 and negative
  • 00:53:59
    2 because negative 2 is an integer so
  • 00:54:01
    this should work what do you think we're
  • 00:54:03
    going to get
  • 00:54:04
    yeah five because when we add seven and
  • 00:54:07
    negative two we get five you're like
  • 00:54:09
    okay that's pretty neat uh how many
  • 00:54:12
    times do I need to add two numbers man I
  • 00:54:14
    mean you know is it really that exciting
  • 00:54:16
    to keep adding two numbers and I say
  • 00:54:17
    yeah it's that exciting run it again run
  • 00:54:20
    it again like uh okay you told me to run
  • 00:54:22
    it again so I'm gonna go ahead and run
  • 00:54:23
    it again now when you run it again once
  • 00:54:25
    again it asks for two numbers and now
  • 00:54:29
    I hate to say it but evil Maron shows up
  • 00:54:31
    and you're like no no Maron don't be
  • 00:54:33
    evil yeah normally I'm not evil normally
  • 00:54:35
    I try to be a good person but sometimes
  • 00:54:37
    to actually show you some things about
  • 00:54:39
    what might go wrong evil Maron shows up
  • 00:54:42
    what does evil Maron gonna do it's a huh
  • 00:54:44
    you know what you asked me to enter the
  • 00:54:46
    first number yeah that's kind of
  • 00:54:48
    interesting you know I'll enter a five
  • 00:54:50
    for the first number you're like that
  • 00:54:52
    doesn't seem so evil yeah but you know
  • 00:54:54
    what I want to enter for the second
  • 00:54:55
    number I think I want to enter the
  • 00:54:57
    number A B C you're like
  • 00:55:00
    but ABC isn't a number what are you
  • 00:55:03
    doing like this is evil Marilyn so what
  • 00:55:05
    do you think happens when I enter ABC
  • 00:55:07
    well let's see I enter ABC and I hit
  • 00:55:09
    return
  • 00:55:10
    and I get an error and what this is
  • 00:55:13
    actually doing is it's python telling
  • 00:55:15
    you that you're trying to do something
  • 00:55:17
    that it doesn't understand but it's
  • 00:55:19
    telling you where things went wrong and
  • 00:55:21
    giving you a hint about what went wrong
  • 00:55:23
    so let's take a look at this error
  • 00:55:25
    message it says line 14 is the place
  • 00:55:28
    where this error happens so here's line
  • 00:55:30
    14 up here that's why we have the line
  • 00:55:32
    numbers to make it easier to follow
  • 00:55:34
    basically this line had an error what's
  • 00:55:36
    the error something called the value
  • 00:55:38
    error that value error said invalid
  • 00:55:41
    literal for INT with base 10 and the
  • 00:55:44
    invalid literal is ABC now that sounds
  • 00:55:47
    kind of weird but really all that means
  • 00:55:49
    is you try to take this ABC which is
  • 00:55:52
    what was entered here is the second
  • 00:55:53
    number so what happened was when we
  • 00:55:55
    asked for the second number the user
  • 00:55:57
    entered ABC and that got stored in num2
  • 00:56:00
    that text ABC which was fine but then
  • 00:56:03
    when we went to line 14 we tried to say
  • 00:56:06
    give me an integer version of ABC and
  • 00:56:09
    this is where python has a problem it's
  • 00:56:10
    says ABC that's not a number that
  • 00:56:14
    doesn't have an integer version and so
  • 00:56:16
    because it's not a valid base 10 number
  • 00:56:19
    that's what this is saying it's an
  • 00:56:21
    invalid literal it's uh you're giving it
  • 00:56:23
    some literal thing which is ABC that
  • 00:56:25
    you're trying to turn into a base 10
  • 00:56:27
    integer and it's not a base 10 integer
  • 00:56:29
    so that's why it gives you the error and
  • 00:56:31
    it happened here on line 14. so the
  • 00:56:33
    program just stops don't worry that
  • 00:56:36
    doesn't cause any damage to your
  • 00:56:37
    computer it doesn't cause anything to
  • 00:56:39
    you know stop working properly it just
  • 00:56:41
    says this program has an error in it and
  • 00:56:44
    so what it actually does is it stops the
  • 00:56:46
    program we can just go ahead and run the
  • 00:56:48
    program again and if we run the program
  • 00:56:49
    again and we give it valid values like
  • 00:56:52
    the sixth and the Seven that's fine and
  • 00:56:54
    if we run it again you might say but but
  • 00:56:56
    what happens if I give it a number
  • 00:56:58
    that's it's not actually an integer like
  • 00:57:00
    I give it a real number like a float
  • 00:57:02
    um okay uh let's say I give it the
  • 00:57:05
    number 7.8 to begin with
  • 00:57:08
    I get another error why do I get narrow
  • 00:57:11
    this time the error is on line number
  • 00:57:13
    12. once again I get a value error and
  • 00:57:16
    it's telling me you're giving me an
  • 00:57:17
    invalid literal for an integer with base
  • 00:57:19
    10 when you gave me a 7.8 what's going
  • 00:57:22
    on here when we input the first number
  • 00:57:24
    what we entered was a 7.8 that got
  • 00:57:27
    stored here and not as num1 as the text
  • 00:57:31
    7.8 then on line 12 we tried to convert
  • 00:57:34
    that text 7.8 to an integer and the
  • 00:57:37
    program says 7.8 is an integer that's
  • 00:57:40
    not valid that's why we get this error
  • 00:57:42
    here on line 12 because it's invalid to
  • 00:57:45
    try to convert the literal piece of text
  • 00:57:48
    7.8 to a base 10 integer because it's
  • 00:57:51
    not and so what the online development
  • 00:57:54
    is basically the environment is telling
  • 00:57:57
    you is if something goes wrong and tries
  • 00:57:59
    to give you hints use these hints so you
  • 00:58:02
    can figure out how to debug your program
  • 00:58:04
    or get the errors out of it because it's
  • 00:58:06
    telling you where the error happened and
  • 00:58:08
    it's trying to give you some indication
  • 00:58:09
    of what that error was and hopefully
  • 00:58:11
    that will help you solve the problem so
  • 00:58:14
    with that said there is our add to
  • 00:58:15
    numbers program you've now seen it
  • 00:58:18
    running in the development environment
  • 00:58:19
    and you've also learned a little bit
  • 00:58:21
    about how to deal with errors if they
  • 00:58:23
    come up congratulations and we'll see
  • 00:58:25
    you next time once again super proud of
  • 00:58:28
    you for getting this far just think
  • 00:58:30
    about all the stuff that you've learned
  • 00:58:31
    in just a few days and you're beginning
  • 00:58:33
    to spread your programming Wings even
  • 00:58:36
    more so take care thanks very much we'll
  • 00:58:39
    see you next time
Tags
  • Python
  • programmation
  • variables
  • addition
  • entrées utilisateur
  • erreurs
  • conversion
  • chaînes de caractères
  • fonction print
  • fonction input