Eval function ...

suppose someone give you a task to evaluate a simple expression like 2+3 , 2*5, 3-4 , etc using python code . then you have to write the code for each arithmetic operator separately and if the expression contains more than 1 arithmetic operator and brackets also then using BODMAS rule also makes your code lengthy and complex also.

In this situation eval function comes into role .


eval() : 

eval function is a predefined python function which full-name is evaluate and this function is used to evaluate any arithmetic expression which may contains more than 1 arithmetic operator .
So instead of writing a long code for arithmetic expression evaluation you can directly use eval( ) function .


How to use eval( ) in our code :

Syntax for eval function ..    

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

  1. expression: this string is parsed and evaluated as a Python expression
  2. globals (optional): a dictionary to specify the available global methods and variables.
  3. locals (optional): another dictionary to specify the available local methods and variables.
 The last two arguments are optional and in this Blog i am only explaining about the first argument which is expression .
The expression parameter denotes your arithmetic expression and the arithmetic expression always be of string type any other type creates an error 
If you run the following code in your python editor . then you can see that expression type is of string and the answer is of integer type and the eval function return the correct answer of your expression into answer variable .
So basically eval( ) function takes a string expression and returns the evaluate answer of integer type. 


 IS eval( ) is good for you code .

According to me the answer is YES , because eval function is created only for solving arithmetic expression and using eval( ) makes your code more short and less complex .





This blog is contributed by Yogesh singh

Admin of Instagram page dynamic_codeing

  Output  

A laptop (also laptop computer), often called a notebook, is a small, portable personal computer (PC) with a "clamshell" form factor, typically having a thin LCD or LED computer screen mounted on the inside of the upper lid of the clamshell and an alphanumeric keyboard on the inside of the lower lid. The clamshell is opened up to use the computer. Laptops are folded shut for transportation, and thus are suitable for mobile use. Its name comes from lap, as it was deemed to be placed on a person's lap when being used. Although originally there was a distinction between laptops and notebooks (the former being bigger and heavier than the latter), as of 2014, there is often no longer any difference. Today, laptops are commonly used in a variety of settings, such as at work, in education, for playing games, web browsing, for personal multimedia, and general home computer use.......