Exec() VS Eval() Explained In Python Tutorial 2023

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to be showing you the difference between eval and exec and these are two built-in functions in Python that have a lot of potential and have a lot of power so it's good to know how to use them when the time comes let's start off with the eval function that stands for evaluate here we're going to create some code that's called the source of type string and here we're just going to write some code in string format so I'm going to write something such as the string of 10 plus 10 times 2 to show some functionality plus the string of hello so it's a harmless line of code now with the power of evaluate we can type in result of type string it's going to equal evaluate and we need to provide a source which is going to be our source then we can print the result and when you run this script you're going to get the result of the line of code that you inserted in here so what evaluate does is evaluate the code the code has to return something for it to work so you can still add a statement such as print as you know if you print something every function in Python returns none by default so if we run this we will get none back plus it's going to execute the print statement so you can also say print hello and it's going to execute the code inside that evaluate it and return whatever it evaluated to so that's what you use evaluate for and that's also why in Python it's probably the easiest language to create a calculator because let's take this for example we can say user inputs of type string is going to equal inputs and we'll just type in U and here we can type in print evaluate and we're going to evaluate the user input so if we run this we can type in something such as 10 plus 10 times 10 divided by three and if you tap on enter it's going to evaluate whatever we entered and it's going to give us the response so it's extremely powerful and as you saw right there to create a calculator in Python just took the evaluate built-in function to actually process all of that information so to sum up evaluate evaluate takes only a single expression and it gives you the return of it so it gives you the result of what that expression has next let's move on to execute which I just called exec so execute is a lot like evaluate except it's much more powerful and to demonstrate this I'm going to create some more source code and this time I'm going to use triple quotation marks to write the code that I want to enter now I'm not going to write this all out immediately because it does take a lot of time to format but this is the script I want to run so we have a print statement that just says this is the execute we're then assigning a value of 10 to X and 11 to y then we go in a for Loop all I'm trying to show you is that with this we can execute this source code and as soon as you run this it's going to run like a normal script it's going to take everything inside the text and run it so this is pretty cool because again with some user input you can type in source is equal to user inputs and we'll just type in you again so you and that's not what I meant to do at all it's equal to input of type string I really messed up that line of code but this is what I was trying to get to so with this you can now type in something such as print hello world and it's going to execute that code for you which is absolutely cool because because with this you can start creating cool programs that can take code from some random source and execute it but at the same time you do need to be careful because if you take random scripts and execute them with this function they can scrape information from your computer and it can be a huge security risk for you or your company if you run external code that you are not familiar with but let's go back to what I had earlier because I need to show something else and that is that when you try to get the results from execute it actually doesn't give you anything back it's not the same as evaluate it just executes the code so what it's going to give you back is none because every function that doesn't return anything in Python by default is going to give you back none but there is also a major side effect when you use execute in some situations especially if you use it in the global namespace here you're going to notice that we can now actually refer to X and Y or not really refer to them it's going to give us this error or it's going to tell us hey you did not Define X but as soon as we run this code here it's actually going to create X in our script so it's going to have X in the global namespace which means if we run this we can now print X even if we did not really Define it in the code or at least the code editor is not going to be aware that you defined it anywhere because it doesn't really check strings for variable declaration so that's something else to keep into account if you're running random code it might affect other code and you'll never know why so you always need to be very careful when you're executing code from a string or from an external Source because one it might be dangerous to your computer or to your workspace and two it might affect other code in your program and this is really hard to debug with any code editor because there's no reference to it and we can't really find out where we declared why especially if this gets really big so all I'm trying to say in the end is that if you had a variable that was called X and it had the string of hello in it and you expected to use x later you'll notice that it's not going to print hello it's going to print 10 and I mean that's a very simple example but if it gets more complex than that good luck trying to find that error but anyways I hope this gave you a really good understanding on the difference between evaluate and execute evaluate evaluate an expression while execute essentially just executes any code that you wanted to execute without returning the result but that doesn't mean you can't get a result from it you can explicitly write something such as result inside here equals blah and then we can just execute that and now we can print the result which is blah and of course we can print X but anyways with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 17,421
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: cqQf3lEoN08
Channel Id: undefined
Length: 6min 44sec (404 seconds)
Published: Mon May 22 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.