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