{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "My name is MSys\n" ] } ], "source": [ "def print_name(name):\n", " print(\"My name is\",name)\n", "\n", "print_name(\"MSys\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n", "[1, 2, 3, 400, 5]\n" ] } ], "source": [ "def change_element(arr, index, element):\n", " arr[index] = element\n", "\n", "\n", "arr = [1, 2, 3, 4, 5]\n", "print(arr)\n", "change_element(arr, 3, 400)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] }, { "data": { "text/plain": [ "'ABC XYZ'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String Formatting\n", "------------------------\n", "z = \"abc\"\n", "\n", ".format()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index 1 , val is 10\n", "index 1, val is 10\n", "index 10, val is 1\n" ] } ], "source": [ "i = 1\n", "v = 10\n", "print('index', i, \", val is\", v)\n", "print(\"index {}, val is {}\".format(i, v)) \n", "print(\"index {s1}, val is {s2}\".format(s1=v, s2= i)) " ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index is 1, val is 10\n" ] } ], "source": [ "i = 1\n", "v = 10\n", "\n", "print(f\"index is {i}, val is {v}\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index is 0 and number is 100\n" ] } ], "source": [ "arr = [10, 20, 30, 40, 50]\n", "\n", "print(\"index is 0 and number is 100\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, v in enumerate(arr):\n", " print(\"index is\", i, \"and value is\", v ** 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, v in enumerate(arr):\n", " print(\"index is {} and value is {}\".format(i, v**2))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "index is 0 and value is 100\n", "index is 1 and value is 400\n", "index is 2 and value is 900\n", "index is 3 and value is 1600\n", "index is 4 and value is 2500\n" ] } ], "source": [ "for i, v in enumerate(arr):\n", " print(f\"index is {i} and value is {v ** 2}\")" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.588\n", "4.59\n", "4.588 \n", "4.588 \n" ] } ], "source": [ "f = 4.5878995994\n", "\n", "print(f\"{f:.3f}\")\n", "print(\"{:.2f}\".format(f))\n", "\n", "res = f\"{f:.3f}\"\n", "print(res, type(res))\n", "res = float(res)\n", "\n", "print(res, type(res))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(4.5878995994)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is 10, b is 20, c is 30\n" ] } ], "source": [ "a = 10\n", "b = 20\n", "c = 30\n", "\n", "print(f\"a is {a}, b is {b}, c is {c}\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "d = {1:'abc', 2:'pqr', 3:\"xyz\"}" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "for key 1 val is ' abc '\n", "for key 2 val is ' pqr '\n", "for key 3 val is ' xyz '\n" ] } ], "source": [ "for key, val in d.items():\n", " print(\"for key\", key, \"val is\", \"'\", val, \"'\")" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "for key 1 val is 'abc'\n", "for key 2 val is 'pqr'\n", "for key 3 val is 'xyz'\n" ] } ], "source": [ "for key, val in d.items():\n", " print(f\"for key {key} val is '{val}'\")" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('abc', 'zxc')\n" ] } ], "source": [ "def func():\n", " return \"abc\", 'zxc'\n", "\n", "x= func()\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "62.800000000000004\n" ] } ], "source": [ "def pi():\n", " return 3.14\n", "\n", "r = 10\n", "print(2 * pi() * r)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6.28, 12.56, 18.84, 25.12, 31.400000000000002]\n" ] } ], "source": [ "def pi():\n", " return 3.14\n", "\n", "def per(r):\n", " return 2 * pi() * r\n", "\n", "res = []\n", "\n", "for i in range(5):\n", " r = int(input())\n", " res.append(per(r))\n", "\n", "print(res)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n" ] } ], "source": [ "# wap to create a function, it will return all even numbers as listtill n\n", "\n", "def even(n):\n", " arr = []\n", "\n", " for i in range(n):\n", " if i % 2 == 0:\n", " arr.append(i)\n", "\n", " return arr\n", "\n", "n = int(input())\n", "res = even(n)\n", "print(res)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# wap to to check the given number is armstrong or not, using function, if it is an armstrong no. ituen true, else false" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "# 153\n", "# ---> 1 ---> 3 ** 3 \n", "# ---> 2 ---> 5 ** 3\n", "# ---> 1 ---> 1 ** 3\n", "\n", "def is_armstrong(n):\n", " res = 0\n", " temp = n\n", "\n", " while temp > 0:\n", " r = temp % 10\n", " res += r ** 3\n", " temp //= 10\n", "\n", " return (n == res)\n", "\n", "\n", "n = 153\n", "print(is_armstrong(n))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "foo() takes from 0 to 3 positional arguments but 4 were given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32md:\\python\\function.ipynb Cell 23\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mn3 is \u001b[39m\u001b[39m{\u001b[39;00mn3\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 5\u001b[0m \u001b[39mreturn\u001b[39;00m n1 \u001b[39m+\u001b[39m n2\u001b[39m+\u001b[39m n3\n\u001b[1;32m----> 7\u001b[0m \u001b[39mprint\u001b[39m(foo(\u001b[39m1\u001b[39;49m, \u001b[39m2\u001b[39;49m, \u001b[39m4\u001b[39;49m, \u001b[39m5\u001b[39;49m))\n", "\u001b[1;31mTypeError\u001b[0m: foo() takes from 0 to 3 positional arguments but 4 were given" ] } ], "source": [ "def foo(n1=0, n2=0, n3=0):\n", " print(f\"n1 is {n1}\")\n", " print(f\"n2 is {n2}\")\n", " print(f\"n3 is {n3}\")\n", " return n1 + n2+ n3\n", "\n", "print(foo(1, 2, 4, 5))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "110\n" ] } ], "source": [ "\n", "def func_name():\n", " y = 10\n", " global x\n", " x = x + 100\n", " print(x)\n", " \n", "x = 10\n", "print(x)\n", "func_name()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 1\n", "50\n", "60\n", "1010\n", "50\n", "60\n" ] } ], "source": [ "a = 10\n", "\n", "def foo():\n", " # print(a, '2')\n", " a = 50\n", " print(a)\n", " a = a+10\n", " print(a)\n", "\n", "print(a, \"1\")\n", "foo()\n", "a = a + 1000\n", "print(a)\n", "foo()" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n", "300\n" ] } ], "source": [ "def foo():\n", " print(a)\n", "\n", "a = 100\n", "foo()\n", "a += 200\n", "foo()" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "def updation():\n", " # a = a + 1\n", " global a\n", " a = a + 1\n", "\n", "a = 1\n", "print(a)\n", "updation()\n", "print(a)\n", "updation()\n", "print(a)\n", "updation()\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "ename": "UnboundLocalError", "evalue": "local variable 'arr' referenced before assignment", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mUnboundLocalError\u001b[0m Traceback (most recent call last)", "\u001b[1;32md:\\python\\function.ipynb Cell 28\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m arr \u001b[39m=\u001b[39m arr \u001b[39m+\u001b[39m [\u001b[39m33\u001b[39m, \u001b[39m44\u001b[39m]\n\u001b[0;32m 6\u001b[0m \u001b[39mprint\u001b[39m(arr)\n\u001b[1;32m----> 8\u001b[0m foo()\n", "\u001b[1;32md:\\python\\function.ipynb Cell 28\u001b[0m in \u001b[0;36mfoo\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mfoo\u001b[39m():\n\u001b[1;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m(arr)\n\u001b[0;32m 5\u001b[0m arr \u001b[39m=\u001b[39m arr \u001b[39m+\u001b[39m [\u001b[39m33\u001b[39m, \u001b[39m44\u001b[39m]\n\u001b[0;32m 6\u001b[0m \u001b[39mprint\u001b[39m(arr)\n", "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'arr' referenced before assignment" ] } ], "source": [ "arr = [1, 2, 3]\n", "\n", "def foo():\n", " print(arr)\n", " arr = arr + [33, 44]\n", " print(arr)\n", "\n", "foo()" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "arr = arr + [200]" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4] 2271685909952\n", "[1, 2, 3, 4, 5] 2271685909952\n", "[1, 2, 3, 4, 5, 10, 20, 30] 2271686273344\n" ] }, { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 10, 20, 30]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4]\n", "print(a, id(a))\n", "a.append(5)\n", "print(a, id(a))\n", "a = a + [10, 20, 30]\n", "print(a, id(a))\n", "a" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 2271595790864\n", "20 2271595791184\n" ] } ], "source": [ "def foo():\n", " x = 10\n", " x = x + 10\n", " print(x, id(x))\n", "\n", "x = 10\n", "print(x, id(x))\n", "foo()" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2271687191040 {'a': 10, 'b': 20}\n", "2271687191040 {'a': 50, 'b': 20}\n" ] } ], "source": [ "d = {'a' : 10, 'b': 20}\n", "print(id(d), d)\n", "d['a'] = 50\n", "print(id(d), d)" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2271687289712\n", "2271687192752 MSzs\n" ] } ], "source": [ "s = 'MSys'\n", "print(id(s))\n", "s = s.replace('y', 'z')\n", "print(id(s), s)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2271685808496 MSys\n", "2271685808496 MSys\n", "2271687565744 MSzs\n", "2271687565744 MSzs\n" ] } ], "source": [ "s = 'MSys'\n", "print(id(s), s)\n", "\n", "def foo():\n", " global s\n", " print(id(s), s)\n", " s = s.replace('y', 'z')\n", " print(id(s), s)\n", "\n", "foo()\n", "print(id(s), s)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "MSys\n" ] } ], "source": [ "s = 'MSys'\n", "res = 10\n", "print(res)\n", "print(s)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "11\n", "12\n", "1\n", "2\n", "3\n" ] } ], "source": [ "def display():\n", " print(1)\n", " print(2)\n", " print(3)\n", "\n", "print(10)\n", "print(11)\n", "print(12)\n", "display()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "x = 10\n", "\n", "def func():\n", " print(x)\n", "\n", "func()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n", "12\n" ] } ], "source": [ "y = 11\n", "\n", "def adder():\n", " global y\n", " y = y + 1\n", "\n", "print(y)\n", "adder()\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "10\n", "20\n" ] } ], "source": [ "def mul():\n", " global a\n", " a = a * 2\n", "\n", "a = 5\n", "print(a)\n", "mul()\n", "print(a)\n", "mul()\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 20\n", "20 10\n" ] } ], "source": [ "a = 10\n", "b = 20\n", "\n", "def swap():\n", " global a, b\n", " a, b = b, a\n", "\n", "print(a, b)\n", "swap()\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 6\n", "6 5\n" ] } ], "source": [ "a = 5\n", "b = 6\n", "\n", "def swap(a, b):\n", " a, b = b, a\n", " return a, b\n", "\n", "print(a, b)\n", "a, b = swap(a, b)\n", "print(a, b)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "50\n", "10 20\n", "50\n" ] } ], "source": [ "def func():\n", " z = 20\n", " a = 10\n", " print(a, z)\n", "\n", "a = 50\n", "print(a)\n", "func()\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "progrma started\n", "outer executed\n", "100\n", "inner executed\n", "100\n", "20\n", "program executed\n" ] } ], "source": [ "def outer():\n", " print('outer executed')\n", " l = 10\n", " print(g)\n", "\n", " def inner():\n", " print('inner executed')\n", " z = 100\n", " nonlocal l\n", " l = l + 10\n", " print(g)\n", " print(l)\n", "\n", " inner()\n", "\n", "g = 100\n", "print('progrma started')\n", "outer()\n", "print(\"program executed\")" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "110\n", "30\n", "1000\n" ] } ], "source": [ "def outer():\n", " x = 10\n", " def inner():\n", " y = 30\n", " nonlocal x\n", " x = x + 100\n", " global z \n", " z = z * 10\n", " print(x)\n", " print(y)\n", " print(z)\n", "\n", " inner()\n", "\n", "z = 100\n", "outer()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "func() takes 2 positional arguments but 3 were given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32md:\\python\\function.ipynb Cell 45\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mfunc\u001b[39m(a, b):\n\u001b[0;32m 2\u001b[0m \u001b[39mprint\u001b[39m(a, b)\n\u001b[1;32m----> 4\u001b[0m func(\u001b[39m10\u001b[39;49m, \u001b[39m20\u001b[39;49m, \u001b[39m30\u001b[39;49m)\n", "\u001b[1;31mTypeError\u001b[0m: func() takes 2 positional arguments but 3 were given" ] } ], "source": [ "def func(a, b):\n", " print(a, b)\n", "\n", "func(10, 20, 30)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "adder() missing 2 required positional arguments: 'a' and 'b'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32md:\\python\\function.ipynb Cell 46\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[39mprint\u001b[39m(a, b)\n\u001b[0;32m 3\u001b[0m \u001b[39mreturn\u001b[39;00m a \u001b[39m+\u001b[39m b\n\u001b[1;32m----> 5\u001b[0m \u001b[39mprint\u001b[39m(adder())\n", "\u001b[1;31mTypeError\u001b[0m: adder() missing 2 required positional arguments: 'a' and 'b'" ] } ], "source": [ "def adder(a, b):\n", " print(a, b)\n", " return a + b\n", "\n", "print(adder())" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "None\n", "0\n" ] } ], "source": [ "def display(name= '', age = None, marks=0):\n", " print(name)\n", " print(age)\n", " print(marks)\n", "\n", "\n", "display()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: 0\n", "b: 0\n", "0\n" ] } ], "source": [ "def adder(a=0, b=0):\n", " print('a:', a)\n", " print('b:', b)\n", " return a + b\n", "\n", "print(adder(a=10, b= 20))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "200" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def mul(b, a= 1):\n", " return a * b\n", "\n", "mul(10, 20)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "slno: 1\n", "name: persion1\n", "age: 0\n", "subject: *\n", "\n", "slno: 2\n", "name: persion2\n", "age: 0\n", "subject: *\n", "\n", "slno: 3\n", "name: persion3\n", "age: 23\n", "subject: *\n", "\n", "slno: 4\n", "name: persion4\n", "age: 24\n", "subject: math\n", "\n", "slno: 5\n", "name: persion5\n", "age: 24\n", "subject: python\n" ] } ], "source": [ "def display(slno, name, age=0, sub='*'):\n", " print('slno:', slno)\n", " print('name:', name)\n", " print('age:', age)\n", " print('subject:', sub)\n", "\n", "display(1, 'persion1')\n", "print()\n", "display(slno=2, name='persion2')\n", "print()\n", "display(3, 'persion3', 23)\n", "print()\n", "display(4, 'persion4', 24, 'math')\n", "print()\n", "display(slno=5, name='persion5', age=24, sub='python')\n", "\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n", "[5, 4, 3, 2, 1]\n" ] } ], "source": [ "def rev_list(arr):\n", " temp = []\n", "\n", " for i in arr:\n", " temp = [i] + temp\n", " \n", " return temp\n", "\n", "arr = [1, 2, 3, 4, 5]\n", "print(arr)\n", "res = rev_list(arr)\n", "print(res)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 20, 30, 40, 50, 60]\n", "[60, 50, 40, 30, 20, 10]\n" ] } ], "source": [ "def rev_list(arr):\n", " s = 0\n", " e = len(arr) - 1\n", "\n", " while s < e:\n", " arr[s], arr[e] = arr[e], arr[s]\n", " s += 1\n", " e -= 1\n", "\n", "arr = [10, 20, 30, 40, 50, 60]\n", "print(arr)\n", "rev_list(arr)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "120\n" ] } ], "source": [ "def adder(a=0, b=0):\n", " print(a + b)\n", "\n", "n1 = 10\n", "n2 = 20\n", "adder(a = 50, b = 70)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98)\n" ] } ], "source": [ "def find_even(n):\n", " for i in range(n):\n", " if not i % 2:\n", " yield i\n", "\n", "\n", "def div_by_ten(n):\n", " res = []\n", "\n", " for i in find_even(n):\n", " if i % 10 == 0:\n", " res.append(i)\n", " return res\n", "\n", "\n", "# print(div_by_ten(100))\n", "\n", "x = find_even(100)\n", "print(type(x))\n", "print(x)\n", "print(tuple(x))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "def adder(a, b, c, d, e=0):\n", " res = 0\n", " # for i in l:\n", " # res += i\n", " return res\n", "\n", "print(adder(10, 20, 30, 40))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3)\n", "[1, 2, 3]\n" ] } ], "source": [ "a = (1, 2,3)\n", "print(a)\n", "a = [1, 2, 3]\n", "print(a)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, [])\n", "()\n" ] } ], "source": [ "def func(*args):\n", " print(args)\n", "\n", "func(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, [])\n", "func()" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4, 5, 6, 7) \n", "28\n" ] } ], "source": [ "def adder(*n):\n", " print(n, type(n))\n", " res=0\n", " \n", " for i in n:\n", " res=res+i\n", " print(res)\n", "adder(1, 2, 3, 4, 5, 6, 7)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'slno': 1, 'name': 'MSys', 'age': 13} \n", "{'slno': 2, 'name': 'abdc', 'age': 12, 'marks': 99} \n", "{'slno': 3, 'name': 'xyz'} \n", "{} \n" ] } ], "source": [ "def display(**kwargs):\n", " print(kwargs, type(kwargs))\n", "\n", "display(slno=1, name='MSys', age=13)\n", "display(slno=2, name='abdc', age=12, marks=99)\n", "display(slno=3, name='xyz')\n", "display()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3, 4) \n", "{'name': 'a', 'age': 44} \n", "() \n", "{} \n" ] } ], "source": [ "def func(*t, **d):\n", " print(t, type(t))\n", " print(d, type(d))\n", "\n", "func(1, 2, 3, 4, name='a', age=44)\n", "func()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "5\n", "50\n", "2.0\n", "\n", "18\n", "4\n", "77\n", "1.5714285714285714\n", "\n", "27\n", "9\n", "162\n", "2.0\n", "\n" ] } ], "source": [ "def calc(n1, n2):\n", " print(n1+n2)\n", " print(n1-n2)\n", " print(n1*n2)\n", " print(n1/n2)\n", " print()\n", "\n", "calc(10, 5)\n", "calc(11, 7)\n", "calc(18, 9)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "5\n", "50\n", "2.0\n", "\n", "18\n", "4\n", "77\n", "1.5714285714285714\n", "\n" ] } ], "source": [ "n1 = 10\n", "n2 = 5\n", "print(n1+n2)\n", "print(n1-n2)\n", "print(n1*n2)\n", "print(n1/n2)\n", "print()\n", "\n", "n1 = 11\n", "n2 = 7\n", "print(n1+n2)\n", "print(n1-n2)\n", "print(n1*n2)\n", "print(n1/n2)\n", "print()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n1, n2 = 10, 30\n", "\n", "for i in range(n1, n2+1):\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n1, n2 = 10, 30\n", "\n", "while n1 <= n2:\n", " print(n1)\n", " n1 += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display(n1, n2):\n", " if n1 > n2:\n", " return\n", " print(n1)\n", " display(n1+1, n2)\n", "\n", "n1, n2 = 10, 15\n", "display(n1, n2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def display(n1, n2):\n", " if n1 <= n2:\n", " print(n1)\n", " display(n1+1, n2)\n", "\n", "n1, n2 = 10, 30\n", "display(n1, n2)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "arr = [10, 'a', True, (20, 40), 10.88]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "i = 0\n", "\n", "while i < len(arr):\n", " print(arr[i])\n", " i += 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def iteration(arr, i):\n", " if i == len(arr):\n", " return\n", " print(arr[i])\n", " iteration(arr, i+1)\n", "\n", "iteration(arr, 0)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n", "a\n", "True\n", "(20, 40)\n", "10.88\n" ] } ], "source": [ "def iteration(arr):\n", " if len(arr) == 0:\n", " return\n", " print(arr[0])\n", " iteration(arr[1:])\n", "\n", "iteration(arr)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "4\n", "5\n" ] } ], "source": [ "def display(n1, n2):\n", " if n1 <= n2:\n", " print(n1)\n", " display(n1+1, n2)\n", "\n", "display(1, 5)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "3\n", "1\n", "3\n", "2\n", "3\n" ] } ], "source": [ "def display(n1, n2):\n", " if n1 <= n2:\n", " display(n1+1, n2)\n", " print(n1)\n", " display(n1+1, n2)\n", "\n", "display(1, 3)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3\n", "2\n", "3\n", "1\n", "3\n", "2\n", "3\n" ] } ], "source": [ "def display1(n1, n2):\n", " if n1 <= n2:\n", " print(n1)\n", " display2(n1+1, n2)\n", "\n", "def display2(n1, n2):\n", " if n1 <= n2:\n", " print(n1)\n", " display1(n1+1, n2)\n", "\n", "display(1, 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.6 64-bit", "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.6" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "cb746cae21d8eece361e3d69b5b88e7465dccc6b14f8d94d4bf000eb937e2588" } } }, "nbformat": 4, "nbformat_minor": 2 }