eval() — Python Technical Interview Questions & Answers
eval()
eval()
- eval() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- eval() — output: 5. eval runs a string as code — don’t use on user input.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix eval up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call eval() once and say what prints.
Code Examples for eval()
Example
# eval()
print(eval("2 + 3"))
eval() — output: 5. eval runs a string as code — don’t use on user input.
How to Explain eval() to an Interviewer
Explain eval() as if you are teaching a junior — definition, then one tiny script.
eval() is built-in — no import. One call + exact output.
What does the example print, and why?
eval() — output: 5
What mistake do freshers make with eval()?
Wrong type → TypeError.