python eval() function

python eval() function

Introduction

Python eval is a built in python function useful for calculations. This function evaluates in the process of finding the output.

Python eval is the widely used function in mathematical operations. It returns the output based on the expression provided. The eval function provides an easy way to return output. It is not necessary to write many lines of code while it can be done in few lines. While writing a code for calculation we often write many line of code but eval function let you to do in two lines. You must enter numbers with expressions.

The syntax of this function is:

eval(expression, globals=None, locals=None)

It is the universal function that can be useful in every situation.

Some python eval() examples

For mathematical operations

n = input("Enter an expression: ")
print("answer: ", eval(n))

python eval
divide
sub
mul


Finding the letter from a word

print(eval('"Coding"[3]'))
string