diff --git a/Python/basics/is_==.ipynb b/Python/basics/is_==.ipynb new file mode 100644 index 0000000..4fcd251 --- /dev/null +++ b/Python/basics/is_==.ipynb @@ -0,0 +1,449 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "cf22a66a", + "metadata": {}, + "source": [ + "# Difference Between is and ==\n", + "\n", + "This is a simple tutorial of when to use `is` and `==` in Python\n", + "\n", + "\n", + "## Defination\n", + "\n", + "`==` is for *value equality*. It's used to know if two objects have the same value.\n", + "\n", + "`is` is for *reference equality*. It's used to know if two references refer (or point) to the same object, i.e if they're identical. Two objects are identical if they have the same memory address.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "06a98143", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "b7da2b13", + "metadata": {}, + "source": [ + "### Simplification\n", + "\n", + "`==` checks if value is equal or not and `is` checks if they are exact same object\n", + "\n", + "Let's understand using `id()`\n", + "\n", + "`id()` is a built-in function in Python. It accepts a single parameter and is used to return the identity of an object." + ] + }, + { + "cell_type": "markdown", + "id": "78e51ae7", + "metadata": {}, + "source": [ + "### Float" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "49671a6a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885460600320\n", + "1885460600008\n", + "140722084511264\n", + "\n", + "For Float:\n", + "True\n", + "False\n", + "\n", + "For Float and Integer:\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "a = 1.0\n", + "b = 1.0\n", + "c = 1\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "print(id(c))\n", + "\n", + "print(\"\\nFor Float:\")\n", + "print(a==b)\n", + "print(a is b)\n", + "\n", + "print(\"\\nFor Float and Integer:\")\n", + "print(a==c)\n", + "print(a is c)" + ] + }, + { + "cell_type": "markdown", + "id": "bd2fb887", + "metadata": {}, + "source": [ + "### List" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "4e0b5224", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885462242952\n", + "1885462242824\n", + "--------\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "## Check ID\n", + "\n", + "a = [1,2,3,4]\n", + "b = [1,2,3,4]\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "607b206d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885461966664\n", + "1885461966664\n", + "--------\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = [1,2,3,4]\n", + "b = a\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "24d16047", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[34, 2, 3, 4]\n", + "--------\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "b[0] = 34\n", + "print(a)\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "066d353d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885461966664\n", + "1885461989512\n", + "--------\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "c = a[:]\n", + "print(id(a))\n", + "print(id(c))\n", + "print(\"--------\")\n", + "print(a==c)\n", + "print(a is c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de8bdf55", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "72b03fb9", + "metadata": {}, + "source": [ + "### Integer" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "cbfe5bb9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140722084511552\n", + "140722084511552\n", + "--------\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "## Check ID\n", + "\n", + "a = 10\n", + "b = 10\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "09c166a6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "140722084511200\n", + "140722084511200\n", + "--------\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = -1\n", + "b = -1\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0e1c98fa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885461868176\n", + "1885461868208\n", + "--------\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "a = 257\n", + "b = 257\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "eac0b7ba", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885461867568\n", + "1885461867920\n", + "--------\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "a = -6\n", + "b = -6\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)" + ] + }, + { + "cell_type": "markdown", + "id": "4545beeb", + "metadata": {}, + "source": [ + "### Integer Explanation\n", + "\n", + "The current implementation keeps an array of integer objects for all integers between `-5` and `256`. When you create an int in that range you actually just get back a reference to the existing object." + ] + }, + { + "cell_type": "markdown", + "id": "946141d8", + "metadata": {}, + "source": [ + "### String" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "88abdb91", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1885462270320\n", + "1885462270800\n", + "--------\n", + "True\n", + "False\n", + "--------\n", + "True\n", + "True\n" + ] + } + ], + "source": [ + "a = \"Subcribe @HarshMittalYoutube\"\n", + "b = \"Subcribe @HarshMittalYoutube\"\n", + "c = a\n", + "\n", + "print(id(a))\n", + "print(id(b))\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==b)\n", + "print(a is b)\n", + "\n", + "print(\"--------\")\n", + "\n", + "print(a==c)\n", + "print(a is c)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cb39a8d2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.1" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}