{ "cells": [ { "cell_type": "raw", "id": "a8b72a8b", "metadata": {}, "source": [ "variable_name = value" ] }, { "cell_type": "markdown", "id": "933c2997", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 6, "id": "79066031", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n" ] } ], "source": [ "a = 1\n", "b = 2\n", "\n", "print(a + b)" ] }, { "cell_type": "code", "execution_count": 12, "id": "06874740", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Muhammad Qasim\n", "\n", "2206130063856\n" ] } ], "source": [ "name = \"Muhammad Qasim\"\n", "\n", "print(name) \n", "print(type(name)) #class data type\n", "print(id(name)) # physical address of variable" ] }, { "cell_type": "code", "execution_count": 13, "id": "55526fbc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20\n", "\n", "2206049043280\n" ] } ], "source": [ "a = 20\n", "\n", "print(a)\n", "print(type(a))\n", "print(id(a))" ] }, { "cell_type": "code", "execution_count": 14, "id": "63dd915b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20.7\n", "\n", "2206129160432\n" ] } ], "source": [ "a = 20.7\n", "\n", "print(a)\n", "print(type(a))\n", "print(id(a))" ] }, { "cell_type": "code", "execution_count": 15, "id": "e15dea6b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n", "\n", "2206130118912\n" ] } ], "source": [ "a = [1,2,3] # list\n", "\n", "print(a)\n", "print(type(a))\n", "print(id(a))" ] }, { "cell_type": "code", "execution_count": 16, "id": "d9d37671", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 5}\n", "\n", "2206128549088\n" ] } ], "source": [ "a = {1,1,1,2,5,5,2} # unique collection\n", "\n", "print(a)\n", "print(type(a))\n", "print(id(a))" ] }, { "cell_type": "code", "execution_count": 17, "id": "1245d030", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'name': 'Qasim', 'education': 'MSDS'}\n", "\n", "2206129356096\n" ] } ], "source": [ "a = {\"name\":\"Qasim\",\n", " \"education\":\"MSDS\"}\n", "\n", "print(a)\n", "print(type(a))\n", "print(id(a))" ] }, { "cell_type": "code", "execution_count": 22, "id": "e99eee87", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 2206049042864\n", "7 2206050332592\n", "7.0 2206129172464\n" ] } ], "source": [ "a = 7 # dynamic data type language\n", "print(a, type(a), id(a))\n", "\n", "a = '7' # string / overwrite\n", "print(a, type(a), id(a))\n", "\n", "a = 7.0 # float\n", "print(a, type(a), id(a))" ] }, { "cell_type": "code", "execution_count": 24, "id": "b640d577", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "qasim = 1\n", "qasim = 2\n", "qasim = '2'\n", "print(qasim)" ] }, { "cell_type": "code", "execution_count": 25, "id": "36886b8b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "a = '1'\n", "b = '1'\n", "\n", "print(a + b)" ] }, { "cell_type": "code", "execution_count": 26, "id": "cfae71cc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "a = 1\n", "b = 1\n", "\n", "print(a + b)" ] }, { "cell_type": "code", "execution_count": 27, "id": "c40ffe2f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2206049042672\n", "2 2206049042704\n", "2 2206050178352\n" ] } ], "source": [ "qasim = 1\n", "print(qasim, type(qasim), id(qasim))\n", "\n", "qasim = 2\n", "print(qasim, type(qasim), id(qasim))\n", "\n", "qasim = '2'\n", "print(qasim, type(qasim), id(qasim))\n" ] }, { "cell_type": "code", "execution_count": 28, "id": "cde44dbb", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2206049042832\n", "2206049042832\n", "2206049042832\n" ] } ], "source": [ "x = 6 # shallow copy\n", "y = 6\n", "z = 6\n", "\n", "print(id(x))\n", "print(id(y))\n", "print(id(z))" ] }, { "cell_type": "code", "execution_count": 29, "id": "d8cc9845", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2206049042864\n", "2206050332592\n" ] } ], "source": [ "a = 7\n", "b = '7'\n", "\n", "print(id(a)) # deep copy\n", "print(id(b))" ] }, { "cell_type": "code", "execution_count": 30, "id": "7c88d42a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2206049042864\n", "2206049042896\n" ] } ], "source": [ "a = 7\n", "b = 8\n", "print(id(a)) # deep copy\n", "print(id(b))" ] }, { "cell_type": "markdown", "id": "c9d42cf3", "metadata": {}, "source": [ "# Operators" ] }, { "cell_type": "markdown", "id": "002f61c5", "metadata": {}, "source": [ "\n", "https://www.w3schools.com/python/python_operators.asp" ] }, { "cell_type": "markdown", "id": "402c50e8", "metadata": {}, "source": [ "# Python divides the operators in the following groups:\n", "\n", "* Arithmetic operators\n", "* Assignment operators\n", "* Comparison operators\n", "* Logical operators\n", "* Identity operators\n", "* Membership operators\n", "* Bitwise operators" ] }, { "cell_type": "markdown", "id": "2427d452", "metadata": {}, "source": [ "## Arithmetic operators" ] }, { "cell_type": "code", "execution_count": 32, "id": "97c80836", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OperatorNameExampleTry it
0+Additionx + yTry it »
1-Subtractionx - yTry it »
2*Multiplicationx * yTry it »
3/Divisionx / yTry it »
4%Modulusx % yTry it »
5**Exponentiationx ** yTry it »
6//Floor divisionx // yTry it »
\n", "
" ], "text/plain": [ " Operator Name Example Try it\n", "0 + Addition x + y Try it »\n", "1 - Subtraction x - y Try it »\n", "2 * Multiplication x * y Try it »\n", "3 / Division x / y Try it »\n", "4 % Modulus x % y Try it »\n", "5 ** Exponentiation x ** y Try it »\n", "6 // Floor division x // y Try it »" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "df = pd.read_html(\"https://www.w3schools.com/python/python_operators.asp\")\n", "df[0]" ] }, { "cell_type": "code", "execution_count": 36, "id": "b2083b1f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9 Addition\n", "5 Subtaction\n", "14 Multiplication\n", "3.5 Division\n" ] } ], "source": [ "print(2 + 7, \"Addition\")\n", "print(7 - 2, \"Subtaction\")\n", "print(7 * 2, \"Multiplication\")\n", "print(7 / 2, \"Division\")" ] }, { "cell_type": "markdown", "id": "fe48e7e3", "metadata": {}, "source": [ "# " ] }, { "cell_type": "code", "execution_count": 37, "id": "1141c904", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "print(11 % 3)" ] }, { "cell_type": "code", "execution_count": 40, "id": "2c28a391", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "8\n" ] } ], "source": [ "print(2 ** 2)\n", "print(2 ** 3)" ] }, { "cell_type": "code", "execution_count": 41, "id": "043fba83", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.5" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7/2" ] }, { "cell_type": "code", "execution_count": 42, "id": "fe04af2e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "7//2" ] }, { "cell_type": "markdown", "id": "f5b806f8", "metadata": {}, "source": [ "# Python Assignment Operators" ] }, { "cell_type": "code", "execution_count": 43, "id": "81437374", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OperatorExampleSame AsTry it
0=x = 5x = 5Try it »
1+=x += 3x = x + 3Try it »
2-=x -= 3x = x - 3Try it »
3*=x *= 3x = x * 3Try it »
4/=x /= 3x = x / 3Try it »
5%=x %= 3x = x % 3Try it »
6//=x //= 3x = x // 3Try it »
7**=x **= 3x = x ** 3Try it »
8&=x &= 3x = x & 3Try it »
9|=x |= 3x = x | 3Try it »
10^=x ^= 3x = x ^ 3Try it »
11>>=x >>= 3x = x >> 3Try it »
12<<=x <<= 3x = x << 3Try it »
\n", "
" ], "text/plain": [ " Operator Example Same As Try it\n", "0 = x = 5 x = 5 Try it »\n", "1 += x += 3 x = x + 3 Try it »\n", "2 -= x -= 3 x = x - 3 Try it »\n", "3 *= x *= 3 x = x * 3 Try it »\n", "4 /= x /= 3 x = x / 3 Try it »\n", "5 %= x %= 3 x = x % 3 Try it »\n", "6 //= x //= 3 x = x // 3 Try it »\n", "7 **= x **= 3 x = x ** 3 Try it »\n", "8 &= x &= 3 x = x & 3 Try it »\n", "9 |= x |= 3 x = x | 3 Try it »\n", "10 ^= x ^= 3 x = x ^ 3 Try it »\n", "11 >>= x >>= 3 x = x >> 3 Try it »\n", "12 <<= x <<= 3 x = x << 3 Try it »" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[1]" ] }, { "cell_type": "code", "execution_count": 44, "id": "f344bd27", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "a = 7\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 45, "id": "536de36c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "a += 2\n", "print(a)" ] }, { "cell_type": "markdown", "id": "d7568006", "metadata": {}, "source": [ "# Python Comparison Operators" ] }, { "cell_type": "code", "execution_count": 46, "id": "7c849bc2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OperatorNameExampleTry it
0==Equalx == yTry it »
1!=Not equalx != yTry it »
2>Greater thanx > yTry it »
3<Less thanx < yTry it »
4>=Greater than or equal tox >= yTry it »
5<=Less than or equal tox <= yTry it »
\n", "
" ], "text/plain": [ " Operator Name Example Try it\n", "0 == Equal x == y Try it »\n", "1 != Not equal x != y Try it »\n", "2 > Greater than x > y Try it »\n", "3 < Less than x < y Try it »\n", "4 >= Greater than or equal to x >= y Try it »\n", "5 <= Less than or equal to x <= y Try it »" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[2]" ] }, { "cell_type": "code", "execution_count": 47, "id": "8326030b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 7\n", "b = 7\n", "\n", "a == b\n", "\n", "#a == b\n", "#7 == 7\n", "#True" ] }, { "cell_type": "code", "execution_count": 48, "id": "624c6631", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 7\n", "b = 9\n", "\n", "a == b\n", "#a == b\n", "#7 == 7\n", "#False" ] }, { "cell_type": "code", "execution_count": 49, "id": "8c889090", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 7\n", "b = 9\n", "\n", "a > b\n", "#a == b\n", "#7 == 9\n", "#False" ] }, { "cell_type": "code", "execution_count": 50, "id": "5b777c8a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 7\n", "b = 9\n", "\n", "a < b\n", "#a == b\n", "#7 == 9\n", "#True" ] }, { "cell_type": "code", "execution_count": 54, "id": "24075c7f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 9\n", "b = 9\n", "\n", "a >= b\n", "#a == b\n", "#7 == 9\n", "#False" ] }, { "cell_type": "code", "execution_count": 55, "id": "c3b08e52", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 9\n", "b = 9\n", "\n", "a != b\n", "#a == b\n", "#7 == 9\n", "#False" ] }, { "cell_type": "code", "execution_count": 56, "id": "d8682fff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OperatorDescriptionExampleTry it
0andReturns True if both statements are truex < 5 and x < 10Try it »
1orReturns True if one of the statements is truex < 5 or x < 4Try it »
2notReverse the result, returns False if the resul...not(x < 5 and x < 10)Try it »
\n", "
" ], "text/plain": [ " Operator Description \\\n", "0 and Returns True if both statements are true \n", "1 or Returns True if one of the statements is true \n", "2 not Reverse the result, returns False if the resul... \n", "\n", " Example Try it \n", "0 x < 5 and x < 10 Try it » \n", "1 x < 5 or x < 4 Try it » \n", "2 not(x < 5 and x < 10) Try it » " ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[3]" ] }, { "cell_type": "code", "execution_count": 59, "id": "66cb6493", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n", "False\n" ] } ], "source": [ "# 1 2 3 4\n", "print(True and True and True and True)\n", "print(True and False and True and True)\n", "print(True and False and True and False)" ] }, { "cell_type": "code", "execution_count": 62, "id": "4e332c64", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "True\n" ] } ], "source": [ "# 1 2 3 4\n", "print(True or True or True or True)\n", "print(True or False or True or True)\n", "print(True or False or True or False)\n", "print(False or True or True or True)" ] }, { "cell_type": "code", "execution_count": null, "id": "e872ab12", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }