-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4ec42f
commit d70fe20
Showing
1 changed file
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,370 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"name": "Eval built-in-function.ipynb", | ||
"provenance": [], | ||
"include_colab_link": true | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "view-in-github", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"<a href=\"https://colab.research.google.com/github/Tanu-N-Prabhu/Python/blob/master/Eval_built_in_function.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "rE6vs_jAn9H6", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"# Python eval() built-in-function" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "PJHFoWR4n_4o", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"## Let us understand the eval() built-in-function in python." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "CR8mY4jaoCjU", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "az8KaKFJoDj9", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"This would be a short article about eval function in python, wherein I would be explaining to you about eval function, its syntax, and few questions that are often asked in interviews so that you clearly understand it and answer those questions in ease." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "CTnzSLrJoFgo", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"## Let's get started:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "_iVy9pA4oHLh", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"## 1. What is eval () in python and what is its syntax?\n", | ||
"\n", | ||
"**Answer:** eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression. In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.\n", | ||
"\n", | ||
"**Syntax**\n", | ||
"\n", | ||
"The syntax of the eval function is as shown below:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "b6TFc_myoOSN", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"\n", | ||
"\n", | ||
"```\n", | ||
"eval(expression, [globals[, locals]])\n", | ||
"```\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "syrOLWNJoRJ4", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"**Arguments or Parameters**\n", | ||
"\n", | ||
"The arguments or parameters of eval function are strings, also optionally global and locals can be used as an argument inside eval function, but the globals must be represented as a dictionary and the locals as a mapped object.\n", | ||
"\n", | ||
"**Return Value**\n", | ||
"\n", | ||
"The return value would be the result of the evaluated expression. Often the return type would be an integer.\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"---\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "z3LgGWZ-oYrv", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"## 2. Where is the eval function mostly used?\n", | ||
"\n", | ||
"Eval function is mostly used in situations or applications which need to evaluate mathematical expressions. Also if the user wants to evaluate the string into code then can use eval function, because eval function evaluates the string expression and returns the integer as a result.\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"---\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "uADIAS_8oed5", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"##3. What is the difference between the input() and eval()?\n", | ||
"\n", | ||
"Now you all know the input() takes the user input, but when the user enters an integer as an input the input function returns a string, but in the case of eval it will evaluate the returned value from a string to an integer. I know most of you are confused, let me clear your confusion by giving an example:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "ePlJ1v1KohiQ", | ||
"colab_type": "code", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 69 | ||
}, | ||
"outputId": "4f6dd51b-2cb5-42cd-d834-182a09637882" | ||
}, | ||
"source": [ | ||
"input = input(\"Enter any number of your choice:\")\n", | ||
"print(input)\n", | ||
"print(type(input))" | ||
], | ||
"execution_count": 1, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"Enter any number of your choice:10 + 10 \n", | ||
"10 + 10 \n", | ||
"<class 'str'>\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "eGOA17n5okje", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"See like I said I entered an integer 10+ 10 where I was expecting a result of 20 (10 + 10) but the input method returned a string of the same input entered." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "sHQth8-bom9G", | ||
"colab_type": "code", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 69 | ||
}, | ||
"outputId": "a104b544-0fdc-4db0-b9a1-176b106d577d" | ||
}, | ||
"source": [ | ||
"eval = eval(input(\"Enter any number of your choice\"))\n", | ||
"print(eval)\n", | ||
"print(type(eval))" | ||
], | ||
"execution_count": 1, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"Enter any number of your choice10 + 10\n", | ||
"20\n", | ||
"<class 'int'>\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "gmOsxUaRowOd", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"In the case of eval, it returned the evaluated expression **20** in the form of an integer given the string as input. **10 + 10 is an expression that returns 20 as a result**.\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"---\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "fFQE_uyEoz7O", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"## 4. Can we perform mathematical operations using eval function, give an example?\n", | ||
"\n", | ||
"Yes, we can perform mathematical operations using eval function as seen below:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "zNjXvaqPpCa_", | ||
"colab_type": "code", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 69 | ||
}, | ||
"outputId": "27f96774-647e-4f21-9bd1-2cea450b4a32" | ||
}, | ||
"source": [ | ||
"evaluate = input(\"Enter what operation x has to perform:\")\n", | ||
"print(evaluate)\n", | ||
"print(type(evaluate))" | ||
], | ||
"execution_count": 2, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"Enter what operation x has to perform:x + x + 100 - 35 + 5 * 80 \n", | ||
"x + x + 100 - 35 + 5 * 80 \n", | ||
"<class 'str'>\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "YEH4g-GhpKkp", | ||
"colab_type": "code", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 35 | ||
}, | ||
"outputId": "64a514f5-07ee-4f60-ca87-6bdd409947f4" | ||
}, | ||
"source": [ | ||
"x = 10\n", | ||
"print(type(x))" | ||
], | ||
"execution_count": 3, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"<class 'int'>\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"metadata": { | ||
"id": "RSvajVhVpMVs", | ||
"colab_type": "code", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 52 | ||
}, | ||
"outputId": "b44dcd5c-589e-4a7c-e1c3-b218cf0b72cc" | ||
}, | ||
"source": [ | ||
"expression = eval(evaluate)\n", | ||
"print(expression)\n", | ||
"print(type(expression))" | ||
], | ||
"execution_count": 4, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"text": [ | ||
"485\n", | ||
"<class 'int'>\n" | ||
], | ||
"name": "stdout" | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "AdIWvjezpOHB", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"See like I said if you give the input as a string and eval function evaluates the expression and returns the result as an integer.\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"---\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "RjQudtaJpPHw", | ||
"colab_type": "text" | ||
}, | ||
"source": [ | ||
"This is all you need to know to get you started with eval function in python, now you know all the answers for the above questions. The same questions might not be asked all the time, 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 shown below:\n", | ||
"\n", | ||
"[Python Eval Function](https://docs.python.org/3/library/functions.html?source=post_page-----601f87db191----------------------#eval)\n", | ||
"\n", | ||
"Thank you guys, this is the end of the article like I said this is a small article. If you guys have some doubts or if you are stuck with something, please let me know in the comment sections below, I will definitely answer all your questions. Alright time to say goodbye, have a wonderful day. Also to read my article on Towards Data Science click [here](https://towardsdatascience.com/python-eval-built-in-function-601f87db191).\n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
"---\n", | ||
"\n" | ||
] | ||
} | ||
] | ||
} |