How does eval function work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How does eval function work?

Could someone explain why the 'eval' computes in this way? https://code.sololearn.com/cU6ion3RJGzL/?ref=app

12th Feb 2019, 8:24 AM
Vladimir {Tomsk}
Vladimir  {Tomsk} - avatar
2 Answers
+ 9
It calculates your mathematical expression within the PEMDAS rules, so in the following order: Parentheses, Exponentiation, Multiplication, Division, Addition and Substraction. 1**2**3 gets resolved to 1, as 1**2 equals 1 and then 1**3... still one :)
12th Feb 2019, 8:56 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
Vladimir, eval is very interesting function ))) In a few words: It executes its argument like it has printed in console. For example eval("1+2+3*4") returns 15 (imagine that you've typed >>>1+2+3*4 and pressed Enter). So print(eval("1+2+3*4")) outputs 15 in console.
12th Feb 2019, 9:10 AM
dimon2101
dimon2101 - avatar