Python built-in function, eval().
Hi Folks! Before we get into the topic, I pray for your good health and well-being.
This would be a small article about eval() function, after going through this blog you will understand the usage of this function. I would explain this function with the examples so, that at the end of the blog you will be clear with its usage and syntax.
Let’s get started!!
What is eval() function?
In python, eval() is a build in function that is used to evaluate the “String” python expression and return the result as an integer.
The Syntax for eval() function
eval(expression, [globals[, locals]])NOTE-The argument and parameters in eval() function are “String” however, the value return would be the result of the evaluated expression that would be an integer.
USES- eval() function used to evaluate mathematical expression. The user can also use this function to evaluate string, as it’s the property of this function to convert the result into integer further examples will clearly demonstrate the property of this function.
To make it simple lets us look with an example and difference between input() and eval(), say we want to add 40+40 =80, let see
Get Umang Saluja’s stories in your inbox
Join Medium for free to get updates from this writer.
The use of input() function returned a string of the same input entered.
user= input('enter the values-')
print(user)
type(user)Output:-
enter the values-40+40str # we can see that datatype is string
The use of eval() function that convert string input to result integer.
user= eval(input('enter the values-'))
print(user)
type(user)Output:-
enter the values-40+40
80int # we can see that data type is integer.
If you’re wandering to solve mathematical operations using eval() function, yes, you can perform that function for mathematical operation. Let’s look at an example for mathematical operation using eval().
ex= eval(input('input the mathematical expression- ')
print(ex)
print(type(ex)))Output:-input the mathematical expression- 5000*2
100000
<class 'int'>
I can assure you this function going to help you when you practice EDA however, the point is to know the concepts better, then you can answer any questions. If you want to spend some time reading through some material regarding the eval() function, I recommend you guys to read the documentation of eval ()function link shown below:
I know that it’s very crisp blog, however my point was to make you aware about the eval() function properties, alright time to pen down!!
If you like this article, make sure to clap until your hand start paining :) Take care, stay healthy, stay safe and keep learning.








