Pythons eval() function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] let's consider this program here look at the first line it's a number is assigned input and then there's a user friendly string saying please enter the number now when this program runs that string will appear and it's requesting that the user enters a number now let's just say for argument's sake they enter 21 so if we come to this line it would appear that what's happening is this 21 that stored in number is being multiplied by 2 to give us 42 and that 42 is a scientific and of course then we print out what stored in double number so we should have 42 appearing on the screen or that's what it would appear to be the case just by looking at this program here so let's just see what actually happens so we can see that we have this user-friendly string appealing please enter a number the user then enters the 21 we come onto this line here that executes and then we execute this line and it will print what is actually been calculated by this program and we can see it certainly isn't 42 in fact it's 21 and 21 or if you want to think of it you could say well isn't this 2121 well in fact no that's not what it's actually representing because what we've done on this line here we've entered a string so number becomes a variable at of type of string and we've entered a 2 and a 1 in it and what this line is doing it's giving us 2 lots of that particular string and assigning it a double number and double number is in fact a string so what we end out printing here is the content of the string double number which is 2 1 2 1 let's look at it in a little bit more detail you see when this line executes and we enter the 21 we create a string now a string is just essentially a sequence of characters so we have the two was entered by the user and the one entered by the user consequently what we're actually doing on this line is taking the string to one and you can see I've put these two courtier to emphasize that it's a string and we're multiplying it by two now this operator we've seen earlier in the playlist does a multiplication but what it actually gives us in this particular case in this context it's asking for two lots of the string so it will give two one-to-one which are two lots of the string 2 1 consequently double number will look like this it will be 2 1 2 1 let's emphasize this point by altering the program slightly and you can see the alteration has occurred here what I've changed the to 2 or 3 if we now run the program again and this time I will enter 2 1 the same as I did last time what we're going to get out is 2 1 2 1 2 1 in other words we've got 3 lots of the string we actually entered so when the program ran and I entered the 2 1 it generated the string variable number into which were the two characters of that string the 2 and the 1 which really meant that I have this string 2 1 and I'm asking for 3 lots of it because this in this particular context is asking for 3 lots of the string so that doesn't act as a multiplication of numbers it's saying right I'll have 3 lots of this particular string please and consequently it gives this value here so we end up with the variable double number now being 2 1 2 1 2 1 I think I'd like to be a little bit more accurate with what I've said here here we can see that we are entering 2 1 and we have this operator here followed by the 3 now what I'm saying is we get 3 lots of the 2 1 to end up with 2 1 2 1 2 1 we don't end up with 3 string variables we just have this 2 1 repeated 3 times and we store it in this variable here which is a string variable so we end up with one string valuable cold double number from this particular process here so overall this program has actually been dealing with a string valuable number and a string valuable double number but the double number has been created by us joining together the contents of what's in number three times to emphasize that we're dealing with strings I've actually produced this program which has just added these two lines to the one we've just been looking at and if we have a look at this line what that's doing it's printing the length of the string variable number that's what this le n means here and if we come down we've got le N and in brackets we got double number so this will print the length of the string variable double number so let's run this program here we can see the user-friendly string appealing I actually typed to one and of course when I press ENTER the to one goes into the number variable as we can see here the two and the one goes into the string variable number now if I go to the next line which is double number is assigned number times three well in fact that is an operator which is asking me for three lots of the number I keep on using the words x which in a sense it is because you're timesing how many times you want this particular string repeating we're actually saying well let's take the 2 ne1 multiply it by three so it will give this particular number here consequently we have double number being this as we've already seen now of course we'll come on to this line and we'll print what that looks like and it's we can see it's here now this particular line will actually tell us the length of number and we can see it too and that's because if we look at the variable we've got one two characters in it in the string there's two characters so that's said to have a length of two we now go on to this line and that one as we can see print six now that's because if we have a look at the string variable double number it's got one two three four five six characters so we get the outputs of six so this again shows us that we've actually been dealing with strings here let's have a look at this program it's more or less the same as the first program we saw in this video but you can see I've had this additional word put in here int now what this is capable of doing is taking what is input using this here and converting it from the string that's input to an integer and to be consistent with what we've done earlier I'm going to enter 21 so when this program executes we will have this please enter the number appearing here because of this and of course I'm now going to enter 21 so this takes that 21 as a string this converts it to an integer and then assigns that integer to number so number here now is an integer variable not a string variable that's 2 and the 1 are now not characters in a string it is actually 21 the quantity 21 if you like a definite integer now what we're going to do we're going to execute 21 times 2 because in this context it is multiplying it is doing mathematics it's multiplying 21 by 2 and of course that gives 42 and the result is as we can see from this line assigned to double number so double number now has 42 so when we now go to here and print the content of double number we can see we get 42 let's have a look at the same program again and let's have a look at another run time well it asked me to please enter a number and what I'm going to do I'm going to enter 1.5 now 1.5 is clearly not an integer it's got a fractional bit to start with and the program has been designed to convert the input to an integer and of course as I just said it's not an integer so the program will actually crash this red text appears and a kinda gives you a hint as to what it is if you read it clearly the last line of read says invalid literal 4 into with base 10 and it puts 1.5 in quotes in other words is saying look I can't convert 1.5 to an integer the way to correct this if you wish to enter non integers ie real numbers as the code in mathematics is you would do this you would use the word float now when I run the program i enter 1.5 so what will now happen it will take the 1.5 x 2 as you can see from the second line in the program and it will then print out this and it prints it out as 3.0 now notice it prints 3.0 now the fact that is a point followed by a 0 it's definitely 3 but it's telling us that this particular output is a float because it's put a 0 there now 1.5 times 2 is 3 but it has made it a float because we have number being a float and I multiplied that float which contained 1.5 by 2 it generates 3 but it generates the 3 as a float not as an integer even though 3 is an integer in mathematics in Python it says no number was a float I've multiplied that by 2 I'm giving you the result of 3 but I'm actually going to store that in a variable of type float so in this particular program we can say that number and double number will both be of type float we now can get a look at the eval function which is what this particular video is really about so if we now have a look at the program we've just been using and we have in this position the word ends and in this position the word float and you can see I've replaced those by the word eval so when I run this program now let's have a go here we go please enter the number Allen to 9 now 9 is clearly an integer so what will happen on the second line it takes the 9 it multiplies by to get 18 and assigns that 18 to double number and then the program will print out double number and does it prints out 18 but look carefully at the 18 you can see there's nothing following the 8 there's no point there's no 0 and that's because what eval is done it says right you've entered 9 which is an integer so I'm going to convert you to an integer type and assign you to number so number becomes an integer variable the second line is taking number which is an integer variable containing 9 multiplies it by 2 guess the result of 9 and assigned that's a double number so double number becomes an integer type because it's receiving an integer and then of course we print double number and we get the 18 as you've seen if I now run the program again but on this occasion I'm going to enter 1.5 what the eval now does it says right while you're entering a float so I'm gonna convert you to a float so it assigns to number 1.5 and makes or Python makes number a float so the second line takes number which is now sterling 1.5 which is a float multiplies it by 2 and produces the result three now three in mathematics that we know is an integer but because we're now in Python and talking about types what will happen it will produce three in a float format a sign that's a double number and double number will now take up the type of a float and then when we print it we get 3.0 and you can clearly see the point 0 is there telling us that it is actually a float so what a vowel will do it will take in an integer convert it to an integer it'll take in a float and convert it to a float now when I say take in an integer it takes in the string of 9 converts that string to an integer it takes in the string of 1.5 and convert that string to a float let's have a look at the avowal function in a different context here you can see it says result is assign eval and in brackets there's a string and the string is one plus two plus three plus four plus five plus six now if we map that against this particular diagram which we've done in a few places in this particular playlist we can see that this string is used as the input of the function and the output is given two results let's write a small program to show what in fact is happening here if we look at the first line we can see if the room we've just been seeing result equals eval and in brackets we have the string and the last line is simply printing out the results ie what worked out on the first line and when we run it we can see we get 21 so what if val is de and it's taken in that string of the 1 + 2 + 3 + 4 + 5 / 6 and worked out that it's 21 assigned the 21 to results and of course print results are simply displayed the 21 as we can see here now here you can see there's a slight amendment to the program of both and we now have a string which is one point 0 plus 2 point 0 all the way up to + six point 0 now of course 1.0 and 2.0 float that's how we represent a float we put some fractional bits in and in this case there is no actual fraction we represent that by 0 but the point is what we now have is a float string here because I say that because when we run it what will actually be output is 21.0 and we can see the point 0 is telling us that this is actually a float let's now consider the following here we can see the program saying calculation is equal to input please enter your calculation the next line of same result is the evaluation of that calculation and the third line is saying the result of your entered calculation is and at the end of that it will actually put the result so when we actually run this particular program we get the user-friendly message saying please enter the calculation so the user enters 1 + 2 + 3 all the way up to + 6 as you can see and it says the result of your entered calculation is 21 so it displays the result without user-friendly message if I run it again with the same program and it says please enter the calculation and now I enter this which is two times and in brackets I've got one plus 3.5 now obviously the contents of those brackets is 1 plus 3.5 which is four point five four point five then multiplied by the two is nine but the thing is because I've got three point five in there which is a float the result is going to be a float so what I think is going to be output in a moment is nine point zero informing us that it's actually a float so let's have a look the results of your entered calculation is nine point zero so here we can see we've got a way of writing a small program where the user enters a string which would be a string of some kind of calculation and Python tells you what the answer is let's consider the second line of this particular program in isolation here it is result is a vowel and in brackets calculation now what we can see it this is a built-in function within Python this here is a string and we've just seen that the result can be of type integer or of float depending upon what we actually input as a string let's consider the following program the first line is simply sending a user-friendly message to the user asking them to type in a function for y now in mathematical terms a function is something like y equals two x plus four and that's the example I'm going to use in this particular program here when I run it in a moment if we look at the rest of the program we can see that we have a for loop and the for loop is dictated to by this range function here and what I mean by dictated to it means that you will go into the loop when X has the value of zero you will go into the loop again and the next time you go into the loop the value of x will be ten because this here it means step up in ten the next time you go into the loop X will be 20 because we're going up in steps of 10 then we won't go any further because this says go up to but not include and obviously 20 plus this 10 is 30 but we don't have X taking up that value now if you're not quite sure about what this range function does I strongly advise you you go back to look at the video earlier than the play list on the range function and of course every time we go into the loop we execute this line here let's now consider the execution of this program the first line to execute is this one here and we can see it puts this user-friendly prompt here now we need to enter an appropriate function and I'm going to enter 2x plus 4 now to do that I have to write it as you can see here 2 times X plus 4 once I press ENTER this will become a string that's stored here in calculation we now enter the 4 and on this entry X will be set to 0 so when we come here to do this calculation evaluate the string if we look here this will be 2 times X which is 0 then 2 times 0 is 0 and so that we add 4 so we get Y is equal to 4 so when we print out this will print our 4x equals then it'll print out what x equals which we know is 0 and then it'll print this string out and then of course it'll print out this here which we can see has just been calculated to be 4 so we get x equals 0 and y equals 4 of course we now go and execute this again but on this occasion X will be 10 because we're stepping up in 10 so when we evaluate the calculation here this X is going to be 10 and 2 times 10 is obviously 20 plus the 4 is 24 so we can see we get X being 10 y being 24 we then go into the loop again but on this occasion X will be 20 so when we do the avowal here of course we concede that this X's now 20 and 2 times 20 is obviously 40 we add the 4 to it and we get 40 4 so what this line will now print out is 4 x equals 20 y equals 44 and if you have a look at what we have here we have the points that we could plot on a graph and when you did that you would see that it was a straight line graph but you've seen another use of evaluates here you can see it's used actually inside a print statement which means that you can pass it a string and it will evaluate it and it returns that value that it's calculated to this position in the print and consequently will be output as you've seen here here and here every time you went round the fallible finally this tutorial has not had a deep look at the eval function there are other features and parameters that can be passed to this function this can wait until later in the playlist when you have gained more experience of Python just a word of warning the eval function can be dangerous if you do not know what you're doing so in the meantime just use if a simple example such as the one shown in this video if you cannot trust the source of the string enter to the eval function then it could for example on the certain conditions allow system calls that delete your directory check out the supporting website for these videos and also consider subscribing to the YouTube channel and get an automatic update every time I upload a new video also consider subscribing to the Google+ circle that relates to these videos
Info
Channel: John Philip Jones
Views: 22,659
Rating: 4.8054709 out of 5
Keywords: Eval, Pythons eval function, eval function, Python, Python 3, len function, int, float, Pythons eval() function, eval()
Id: Llk6uCXwkvE
Channel Id: undefined
Length: 21min 57sec (1317 seconds)
Published: Wed Jul 23 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.