{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Day 11 Sets and Functions in Python

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recap\n", "\n", "\n", "- Dictionary\n", "- Dict Methods\n", "\n", "### Today Objectives\n", "\n", "- Set\n", "- Set Methods\n", "- Functions in Python\n", " - Built-in Funtions -> print(), input(), sum(), min(), max(), type(), \n", " id(), len(), range(), sorted(), reversed()\n", " - User Defined Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sets in Python\n", "\n", "It is used for storing non-homogenous group of unique data on python\n", "\n", "\n", "### Properties\n", "\n", "- `{}` for storing the data in comma seperated\n", "- It is mutable data type\n", "- It is an unordered\n", "- we can't accessing the data from set using indexing\n", "- It is iterable\n", "- It doesn't allow duplicated data" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "s1 = set()\n", "\n", "print(type(s1))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "s1 = {2,1,5,6,'apssdc','Python', 2.55, 56, 0.26, 0.10}\n", "\n", "print(type(s1))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.26, 1, 2, 2.55, 0.1, 5, 6, 'apssdc', 'Python', 56}\n" ] } ], "source": [ "print(s1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 4}\n" ] } ], "source": [ "s2 = {1,2,3,4,4,3,2,1}\n", "\n", "print(s2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "st = \"\"\"Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Wikipedia\n", "Developer: Python Software Foundation\n", "Stable release: 3.9.5 / 3 May 2021; 19 days ago\n", "Preview release: 3.10.0b1 / 3 May 2021; 19 days ago\n", "Typing discipline: Duck, dynamic, strong typing; gradual (since 3.5, but ignored in CPython)\n", "First appeared: February 1991; 30 years ago\n", "Paradigm: Multi-paradigm: object-oriented, procedural (imperative), functional, structured, reflective\"\"\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'P': 6, 'y': 15, 't': 28, 'h': 10, 'o': 25, 'n': 29, ' ': 67, 'i': 39, 's': 20, 'a': 38, 'e': 48, 'r': 29, 'p': 17, 'd': 19, 'g': 18, '-': 4, 'l': 17, 'v': 5, 'u': 13, 'm': 7, '.': 7, \"'\": 1, 'z': 1, 'c': 11, 'b': 7, 'w': 3, 'f': 5, 'W': 1, 'k': 2, '\\n': 6, 'D': 2, ':': 7, 'S': 2, 'F': 3, '3': 6, '9': 5, '5': 2, '/': 2, 'M': 3, '2': 4, '0': 5, '1': 8, ';': 4, 'T': 1, ',': 7, '(': 2, 'C': 1, ')': 2, 'j': 1}\n" ] } ], "source": [ "c = {}\n", "\n", "for char in st:\n", " # print(char, end = ' ')\n", " c[char] = st.count(char)\n", "\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "565\n" ] } ], "source": [ "print(len(st))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "49\n" ] } ], "source": [ "print(len(c))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'b', '/', 'P', 'p', '2', 'c', 'j', 'i', ';', \"'\", 'n', 'm', 'M', ')', 'C', 'D', 'F', ':', '9', 'l', '\\n', 'v', '.', 'y', 's', 'g', 'a', 'o', 'w', 'r', 'e', 'S', 'f', ' ', 'u', '5', '0', 'T', '(', 'W', 't', 'd', '3', 'h', ',', '-', 'z', 'k', '1'}\n" ] } ], "source": [ "s3 = set(st)\n", "\n", "print(s3)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "49\n" ] } ], "source": [ "print(len(s3))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'b': 7, '/': 2, 'P': 6, 'p': 17, '2': 4, 'c': 11, 'j': 1, 'i': 39, ';': 4, \"'\": 1, 'n': 29, 'm': 7, 'M': 3, ')': 2, 'C': 1, 'D': 2, 'F': 3, ':': 7, '9': 5, 'l': 17, '\\n': 6, 'v': 5, '.': 7, 'y': 15, 's': 20, 'g': 18, 'a': 38, 'o': 25, 'w': 3, 'r': 29, 'e': 48, 'S': 2, 'f': 5, ' ': 67, 'u': 13, '5': 2, '0': 5, 'T': 1, '(': 2, 'W': 1, 't': 28, 'd': 19, '3': 6, 'h': 10, ',': 7, '-': 4, 'z': 1, 'k': 2, '1': 8}\n" ] } ], "source": [ "c = {}\n", "\n", "for char in set(st):\n", " # print(char, end = ' ')\n", " c[char] = st.count(char)\n", "\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Python', 'is', 'an', 'interpreted', 'high-level', 'general-purpose', 'programming', 'language.', \"Python's\", 'design', 'philosophy', 'emphasizes', 'code', 'readability', 'with', 'its', 'notable', 'use', 'of', 'significant', 'indentation.', 'Wikipedia', 'Developer:', 'Python', 'Software', 'Foundation', 'Stable', 'release:', '3.9.5', '/', '3', 'May', '2021;', '19', 'days', 'ago', 'Preview', 'release:', '3.10.0b1', '/', '3', 'May', '2021;', '19', 'days', 'ago', 'Typing', 'discipline:', 'Duck,', 'dynamic,', 'strong', 'typing;', 'gradual', '(since', '3.5,', 'but', 'ignored', 'in', 'CPython)', 'First', 'appeared:', 'February', '1991;', '30', 'years', 'ago', 'Paradigm:', 'Multi-paradigm:', 'object-oriented,', 'procedural', '(imperative),', 'functional,', 'structured,', 'reflective']\n" ] } ], "source": [ "sli = st.split()\n", "\n", "\n", "print(sli)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "74\n" ] } ], "source": [ "print(len(sli))" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'days', 'gradual', 'object-oriented,', '/', 'Software', 'in', 'Developer:', 'Paradigm:', 'significant', 'Preview', 'Python', 'Wikipedia', 'notable', '3.10.0b1', 'code', 'Duck,', 'functional,', 'but', '1991;', 'May', '30', 'indentation.', 'ago', 'high-level', 'structured,', 'general-purpose', '(since', '3.5,', 'use', 'CPython)', 'interpreted', 'Stable', 'February', '2021;', \"Python's\", 'its', 'Foundation', 'Typing', 'ignored', 'of', '(imperative),', 'dynamic,', 'typing;', 'language.', 'release:', 'an', 'Multi-paradigm:', 'design', 'emphasizes', 'reflective', '3', 'philosophy', 'discipline:', 'appeared:', 'with', 'is', 'strong', '19', 'years', 'procedural', 'readability', 'First', 'programming', '3.9.5'} 64\n" ] } ], "source": [ "ssli = set(sli)\n", "\n", "print(ssli, len(ssli))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['days', 'gradual', 'object-oriented,', '/', 'Software', 'in', 'Developer:', 'Paradigm:', 'significant', 'Preview', 'Python', 'Wikipedia', 'notable', '3.10.0b1', 'code', 'Duck,', 'functional,', 'but', '1991;', 'May', '30', 'indentation.', 'ago', 'high-level', 'structured,', 'general-purpose', '(since', '3.5,', 'use', 'CPython)', 'interpreted', 'Stable', 'February', '2021;', \"Python's\", 'its', 'Foundation', 'Typing', 'ignored', 'of', '(imperative),', 'dynamic,', 'typing;', 'language.', 'release:', 'an', 'Multi-paradigm:', 'design', 'emphasizes', 'reflective', '3', 'philosophy', 'discipline:', 'appeared:', 'with', 'is', 'strong', '19', 'years', 'procedural', 'readability', 'First', 'programming', '3.9.5']\n" ] } ], "source": [ "print(list(ssli))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('days', 'gradual', 'object-oriented,', '/', 'Software', 'in', 'Developer:', 'Paradigm:', 'significant', 'Preview', 'Python', 'Wikipedia', 'notable', '3.10.0b1', 'code', 'Duck,', 'functional,', 'but', '1991;', 'May', '30', 'indentation.', 'ago', 'high-level', 'structured,', 'general-purpose', '(since', '3.5,', 'use', 'CPython)', 'interpreted', 'Stable', 'February', '2021;', \"Python's\", 'its', 'Foundation', 'Typing', 'ignored', 'of', '(imperative),', 'dynamic,', 'typing;', 'language.', 'release:', 'an', 'Multi-paradigm:', 'design', 'emphasizes', 'reflective', '3', 'philosophy', 'discipline:', 'appeared:', 'with', 'is', 'strong', '19', 'years', 'procedural', 'readability', 'First', 'programming', '3.9.5')\n" ] } ], "source": [ "print(tuple(ssli))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "{'days', 'gradual', 'object-oriented,', '/', 'Software', 'in', 'Developer:', 'Paradigm:', 'significant', 'Preview', 'Python', 'Wikipedia', 'notable', '3.10.0b1', 'code', 'Duck,', 'functional,', 'but', '1991;', 'May', '30', 'indentation.', 'ago', 'high-level', 'structured,', 'general-purpose', '(since', '3.5,', 'use', 'CPython)', 'interpreted', 'Stable', 'February', '2021;', \"Python's\", 'its', 'Foundation', 'Typing', 'ignored', 'of', '(imperative),', 'dynamic,', 'typing;', 'language.', 'release:', 'an', 'Multi-paradigm:', 'design', 'emphasizes', 'reflective', '3', 'philosophy', 'discipline:', 'appeared:', 'with', 'is', 'strong', '19', 'years', 'procedural', 'readability', 'First', 'programming', '3.9.5'}\n", "{\n" ] } ], "source": [ "stli = str(ssli)\n", "\n", "print(type(stli))\n", "print(stli)\n", "print(stli[0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set Methods" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "se = set()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "set()\n" ] } ], "source": [ "print(se)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 2, 123}\n" ] } ], "source": [ "se.add(123)\n", "se.add(2)\n", "se.add(0.5)\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5656\n" ] } ], "source": [ "se.add(input())" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 2, 123, '5656'}\n" ] } ], "source": [ "print(se)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 2, 123, '5656'}\n" ] } ], "source": [ "se.add(0.5)\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unhashable type: 'list'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'" ] } ], "source": [ "se.add([1,2,3])\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 2, (1, 2, 3), '5656', 123}\n" ] } ], "source": [ "se.add((1,2,3))\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 1, 2, 3, 4, (1, 2, 3), '5656', 123}\n" ] } ], "source": [ "se.update({1,2,3,4,4})\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{0.5, 1, 2, 3, 4, 5, 6, 7, (1, 2, 3), '5656', 123}\n" ] } ], "source": [ "se.update([1,2,3,4,4,5,6,7])\n", "\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "{3, 4, 5, 6, 7, (1, 2, 3), '5656', 123}\n" ] } ], "source": [ "print(se.pop())\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{3, 4, 5, 6, 7, '5656', 123}\n" ] } ], "source": [ "se.remove((1,2,3))\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "(1, 2, 3)", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mKeyError\u001b[0m: (1, 2, 3)" ] } ], "source": [ "se.remove((1,2,3))\n", "\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{3, 4, 5, 6, 7, 123}\n" ] } ], "source": [ "se.discard('5656')\n", "\n", "\n", "print(se)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None {3, 4, 5, 6, 7, 123}\n" ] } ], "source": [ "x = se.discard('5656')\n", "\n", "print(x, se)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None {3, 4, 5, 6, 7}\n" ] } ], "source": [ "x = se.discard(123)\n", "\n", "print(x, se)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mse\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" ] } ], "source": [ "print(se[0])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{3, 4, 5, 6, 7}\n" ] } ], "source": [ "se2 = se.copy()\n", "\n", "\n", "print(se2)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "set()\n" ] }, { "ename": "NameError", "evalue": "name 'se2' is not defined", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mse2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mse2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mNameError\u001b[0m: name 'se2' is not defined" ] } ], "source": [ "se2.clear()\n", "\n", "print(se2)\n", "\n", "\n", "del se2\n", "\n", "print(se2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Math Sets\n", "\n", "\n", "- Union\n", "- Intersection" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 4, 5, 6, 7, 8, 9}\n" ] } ], "source": [ "s1 = {1,2,3,4,5,6}\n", "s2 = {4,5,6,7,8,9}\n", "\n", "\n", "print(s1.union(s2))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 4, 5, 6, 7, 8, 9}\n" ] } ], "source": [ "print(s1 | s2)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{4, 5, 6}\n", "{4, 5, 6}\n" ] } ], "source": [ "print(s1.intersection(s2))\n", "\n", "print(s1 & s2)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3}\n" ] } ], "source": [ "print(s1.difference(s2))" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3}\n" ] } ], "source": [ "print(s1-s2)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{8, 9, 7}\n" ] } ], "source": [ "print(s2 - s1)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 7, 8, 9}\n" ] } ], "source": [ "print(s1.symmetric_difference(s2))" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 7, 8, 9}\n" ] } ], "source": [ "print(s1 ^ s2)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "{1, 2, 3, 7, 8, 9}\n" ] } ], "source": [ "print(s1.symmetric_difference_update(s2))\n", "\n", "print(s1)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "{1, 2, 3}\n" ] } ], "source": [ "print(s1.difference_update(s2))\n", "\n", "print(s1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Functions in Python\n", " - Built-in Funtions -> print(), input(), sum(), min(), max(), type(), \n", " id(), len(), range(), sorted(), reversed()\n", " - User Defined Functions\n", " \n", " \n", " \n", "set of instruction given by the user to perform a task\n", "\n", "- Reusable code to perform single related action \n", "- it reduces the lines of code\n", "- Reduces time complexity, memory" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(abs(-5))" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n" ] } ], "source": [ "li = [1,2,3,4,5,6]\n", "li2 = [1,2,3,4,5,6,0]\n", "\n", "\n", "print(all(li), all(li2))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True False\n" ] } ], "source": [ "li = [1,2,3,4,5,6,'0']\n", "li2 = [1,2,3,4,5,6,0]\n", "\n", "\n", "print(all(li), all(li2))" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True True False\n" ] } ], "source": [ "li = [1,2,3,4,5]\n", "li2 = [0,0,0,0,0,0,0,1]\n", "li3 = [0,0,0,0,0]\n", "print(any(li), any(li2), any(li3))" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(all([1,2,3,'']))" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print(bool([1,2]))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class str in module builtins:\n", "\n", "class str(object)\n", " | str(object='') -> str\n", " | str(bytes_or_buffer[, encoding[, errors]]) -> str\n", " | \n", " | Create a new string object from the given object. If encoding or\n", " | errors is specified, then the object must expose a data buffer\n", " | that will be decoded using the given encoding and error handler.\n", " | Otherwise, returns the result of object.__str__() (if defined)\n", " | or repr(object).\n", " | encoding defaults to sys.getdefaultencoding().\n", " | errors defaults to 'strict'.\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __contains__(self, key, /)\n", " | Return key in self.\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __format__(self, format_spec, /)\n", " | Return a formatted version of the string as described by format_spec.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(self, key, /)\n", " | Return self[key].\n", " | \n", " | __getnewargs__(...)\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __hash__(self, /)\n", " | Return hash(self).\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self int\n", " | \n", " | Return the number of non-overlapping occurrences of substring sub in\n", " | string S[start:end]. Optional arguments start and end are\n", " | interpreted as in slice notation.\n", " | \n", " | encode(self, /, encoding='utf-8', errors='strict')\n", " | Encode the string using the codec registered for encoding.\n", " | \n", " | encoding\n", " | The encoding in which to encode the string.\n", " | errors\n", " | The error handling scheme to use for encoding errors.\n", " | The default is 'strict' meaning that encoding errors raise a\n", " | UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n", " | 'xmlcharrefreplace' as well as any other name registered with\n", " | codecs.register_error that can handle UnicodeEncodeErrors.\n", " | \n", " | endswith(...)\n", " | S.endswith(suffix[, start[, end]]) -> bool\n", " | \n", " | Return True if S ends with the specified suffix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | suffix can also be a tuple of strings to try.\n", " | \n", " | expandtabs(self, /, tabsize=8)\n", " | Return a copy where all tab characters are expanded using spaces.\n", " | \n", " | If tabsize is not given, a tab size of 8 characters is assumed.\n", " | \n", " | find(...)\n", " | S.find(sub[, start[, end]]) -> int\n", " | \n", " | Return the lowest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | format(...)\n", " | S.format(*args, **kwargs) -> str\n", " | \n", " | Return a formatted version of S, using substitutions from args and kwargs.\n", " | The substitutions are identified by braces ('{' and '}').\n", " | \n", " | format_map(...)\n", " | S.format_map(mapping) -> str\n", " | \n", " | Return a formatted version of S, using substitutions from mapping.\n", " | The substitutions are identified by braces ('{' and '}').\n", " | \n", " | index(...)\n", " | S.index(sub[, start[, end]]) -> int\n", " | \n", " | Return the lowest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Raises ValueError when the substring is not found.\n", " | \n", " | isalnum(self, /)\n", " | Return True if the string is an alpha-numeric string, False otherwise.\n", " | \n", " | A string is alpha-numeric if all characters in the string are alpha-numeric and\n", " | there is at least one character in the string.\n", " | \n", " | isalpha(self, /)\n", " | Return True if the string is an alphabetic string, False otherwise.\n", " | \n", " | A string is alphabetic if all characters in the string are alphabetic and there\n", " | is at least one character in the string.\n", " | \n", " | isascii(self, /)\n", " | Return True if all characters in the string are ASCII, False otherwise.\n", " | \n", " | ASCII characters have code points in the range U+0000-U+007F.\n", " | Empty string is ASCII too.\n", " | \n", " | isdecimal(self, /)\n", " | Return True if the string is a decimal string, False otherwise.\n", " | \n", " | A string is a decimal string if all characters in the string are decimal and\n", " | there is at least one character in the string.\n", " | \n", " | isdigit(self, /)\n", " | Return True if the string is a digit string, False otherwise.\n", " | \n", " | A string is a digit string if all characters in the string are digits and there\n", " | is at least one character in the string.\n", " | \n", " | isidentifier(self, /)\n", " | Return True if the string is a valid Python identifier, False otherwise.\n", " | \n", " | Call keyword.iskeyword(s) to test whether string s is a reserved identifier,\n", " | such as \"def\" or \"class\".\n", " | \n", " | islower(self, /)\n", " | Return True if the string is a lowercase string, False otherwise.\n", " | \n", " | A string is lowercase if all cased characters in the string are lowercase and\n", " | there is at least one cased character in the string.\n", " | \n", " | isnumeric(self, /)\n", " | Return True if the string is a numeric string, False otherwise.\n", " | \n", " | A string is numeric if all characters in the string are numeric and there is at\n", " | least one character in the string.\n", " | \n", " | isprintable(self, /)\n", " | Return True if the string is printable, False otherwise.\n", " | \n", " | A string is printable if all of its characters are considered printable in\n", " | repr() or if it is empty.\n", " | \n", " | isspace(self, /)\n", " | Return True if the string is a whitespace string, False otherwise.\n", " | \n", " | A string is whitespace if all characters in the string are whitespace and there\n", " | is at least one character in the string.\n", " | \n", " | istitle(self, /)\n", " | Return True if the string is a title-cased string, False otherwise.\n", " | \n", " | In a title-cased string, upper- and title-case characters may only\n", " | follow uncased characters and lowercase characters only cased ones.\n", " | \n", " | isupper(self, /)\n", " | Return True if the string is an uppercase string, False otherwise.\n", " | \n", " | A string is uppercase if all cased characters in the string are uppercase and\n", " | there is at least one cased character in the string.\n", " | \n", " | join(self, iterable, /)\n", " | Concatenate any number of strings.\n", " | \n", " | The string whose method is called is inserted in between each given string.\n", " | The result is returned as a new string.\n", " | \n", " | Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n", " | \n", " | ljust(self, width, fillchar=' ', /)\n", " | Return a left-justified string of length width.\n", " | \n", " | Padding is done using the specified fill character (default is a space).\n", " | \n", " | lower(self, /)\n", " | Return a copy of the string converted to lowercase.\n", " | \n", " | lstrip(self, chars=None, /)\n", " | Return a copy of the string with leading whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | partition(self, sep, /)\n", " | Partition the string into three parts using the given separator.\n", " | \n", " | This will search for the separator in the string. If the separator is found,\n", " | returns a 3-tuple containing the part before the separator, the separator\n", " | itself, and the part after it.\n", " | \n", " | If the separator is not found, returns a 3-tuple containing the original string\n", " | and two empty strings.\n", " | \n", " | replace(self, old, new, count=-1, /)\n", " | Return a copy with all occurrences of substring old replaced by new.\n", " | \n", " | count\n", " | Maximum number of occurrences to replace.\n", " | -1 (the default value) means replace all occurrences.\n", " | \n", " | If the optional argument count is given, only the first count occurrences are\n", " | replaced.\n", " | \n", " | rfind(...)\n", " | S.rfind(sub[, start[, end]]) -> int\n", " | \n", " | Return the highest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Return -1 on failure.\n", " | \n", " | rindex(...)\n", " | S.rindex(sub[, start[, end]]) -> int\n", " | \n", " | Return the highest index in S where substring sub is found,\n", " | such that sub is contained within S[start:end]. Optional\n", " | arguments start and end are interpreted as in slice notation.\n", " | \n", " | Raises ValueError when the substring is not found.\n", " | \n", " | rjust(self, width, fillchar=' ', /)\n", " | Return a right-justified string of length width.\n", " | \n", " | Padding is done using the specified fill character (default is a space).\n", " | \n", " | rpartition(self, sep, /)\n", " | Partition the string into three parts using the given separator.\n", " | \n", " | This will search for the separator in the string, starting at the end. If\n", " | the separator is found, returns a 3-tuple containing the part before the\n", " | separator, the separator itself, and the part after it.\n", " | \n", " | If the separator is not found, returns a 3-tuple containing two empty strings\n", " | and the original string.\n", " | \n", " | rsplit(self, /, sep=None, maxsplit=-1)\n", " | Return a list of the words in the string, using sep as the delimiter string.\n", " | \n", " | sep\n", " | The delimiter according which to split the string.\n", " | None (the default value) means split according to any whitespace,\n", " | and discard empty strings from the result.\n", " | maxsplit\n", " | Maximum number of splits to do.\n", " | -1 (the default value) means no limit.\n", " | \n", " | Splits are done starting at the end of the string and working to the front.\n", " | \n", " | rstrip(self, chars=None, /)\n", " | Return a copy of the string with trailing whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | split(self, /, sep=None, maxsplit=-1)\n", " | Return a list of the words in the string, using sep as the delimiter string.\n", " | \n", " | sep\n", " | The delimiter according which to split the string.\n", " | None (the default value) means split according to any whitespace,\n", " | and discard empty strings from the result.\n", " | maxsplit\n", " | Maximum number of splits to do.\n", " | -1 (the default value) means no limit.\n", " | \n", " | splitlines(self, /, keepends=False)\n", " | Return a list of the lines in the string, breaking at line boundaries.\n", " | \n", " | Line breaks are not included in the resulting list unless keepends is given and\n", " | true.\n", " | \n", " | startswith(...)\n", " | S.startswith(prefix[, start[, end]]) -> bool\n", " | \n", " | Return True if S starts with the specified prefix, False otherwise.\n", " | With optional start, test S beginning at that position.\n", " | With optional end, stop comparing S at that position.\n", " | prefix can also be a tuple of strings to try.\n", " | \n", " | strip(self, chars=None, /)\n", " | Return a copy of the string with leading and trailing whitespace removed.\n", " | \n", " | If chars is given and not None, remove characters in chars instead.\n", " | \n", " | swapcase(self, /)\n", " | Convert uppercase characters to lowercase and lowercase characters to uppercase.\n", " | \n", " | title(self, /)\n", " | Return a version of the string where each word is titlecased.\n", " | \n", " | More specifically, words start with uppercased characters and all remaining\n", " | cased characters have lower case.\n", " | \n", " | translate(self, table, /)\n", " | Replace each character in the string using the given translation table.\n", " | \n", " | table\n", " | Translation table, which must be a mapping of Unicode ordinals to\n", " | Unicode ordinals, strings, or None.\n", " | \n", " | The table must implement lookup/indexing via __getitem__, for instance a\n", " | dictionary or list. If this operation raises LookupError, the character is\n", " | left untouched. Characters mapped to None are deleted.\n", " | \n", " | upper(self, /)\n", " | Return a copy of the string converted to uppercase.\n", " | \n", " | zfill(self, width, /)\n", " | Pad a numeric string with zeros on the left, to fill a field of the given width.\n", " | \n", " | The string is never truncated.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods defined here:\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | maketrans(...)\n", " | Return a translation table usable for str.translate().\n", " | \n", " | If there is only one argument, it must be a dictionary mapping Unicode\n", " | ordinals (integers) or characters to Unicode ordinals, strings or None.\n", " | Character keys will be then converted to ordinals.\n", " | If there are two arguments, they must be strings of equal length, and\n", " | in the resulting dictionary, each character in x will be mapped to the\n", " | character at the same position in y. If there is a third argument, it\n", " | must be a string, whose characters will be mapped to None in the result.\n", "\n" ] } ], "source": [ "help(str)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__delitem__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__gt__',\n", " '__hash__',\n", " '__iadd__',\n", " '__imul__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__reversed__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__setitem__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'append',\n", " 'clear',\n", " 'copy',\n", " 'count',\n", " 'extend',\n", " 'index',\n", " 'insert',\n", " 'pop',\n", " 'remove',\n", " 'reverse',\n", " 'sort']" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(list)" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "str(object='') -> str\n", "str(bytes_or_buffer[, encoding[, errors]]) -> str\n", "\n", "Create a new string object from the given object. If encoding or\n", "errors is specified, then the object must expose a data buffer\n", "that will be decoded using the given encoding and error handler.\n", "Otherwise, returns the result of object.__str__() (if defined)\n", "or repr(object).\n", "encoding defaults to sys.getdefaultencoding().\n", "errors defaults to 'strict'.\n" ] } ], "source": [ "print(str.__doc__)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "S.count(sub[, start[, end]]) -> int\n", "\n", "Return the number of non-overlapping occurrences of substring sub in\n", "string S[start:end]. Optional arguments start and end are\n", "interpreted as in slice notation.\n" ] } ], "source": [ "print(str.count.__doc__)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "li = [1,2,3,4]\n", "li2 = [5,6,7,8]\n", "\n", "li3 = zip(li, li2)\n", "\n", "print(li3)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(1, 5), (2, 6), (3, 7), (4, 8)]\n" ] } ], "source": [ "li3 = list(li3)\n", "\n", "print(li3)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "print(enumerate(li))" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(0, 1), (1, 2), (2, 3), (3, 4)]\n" ] } ], "source": [ "en = list(enumerate(li))\n", "\n", "print(en)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### User-Defined functions\n", "\n", "#### Syntax\n", "\n", "\n", "```python\n", "\n", "def function_name(arg1, arg2, ....... argn): # arg are optional\n", " \"\"\"Document for function\"\"\" # it is optional\n", " Block of code\n", " \n", " return result # return is optional\n", "```\n", "\n", "\n", "### Types of Function\n", "\n", "1. Based on arguments\n", " 1. Positional argument/ required argument\n", " 2. keyword argument\n", " 3. default argument\n", " 4. Variable length keyword arguments\n", "2. Based on return and arguments\n", " 1. With arg with return\n", " 2. without arg with return\n", " 3. with arg without return\n", " 4. without arg without return\n", "3. Call by value\n", "4. Call by reference\n", "5. Recursive Functions" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "# with arg with return\n", "def addition(a, b):\n", " print(a)\n", " print(b)\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "20\n" ] }, { "data": { "text/plain": [ "35" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "addition(15, 20) # Function calling " ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "def greet():\n", " return \"Good Evening all\"" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Good Evening all'" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "greet()" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "# with arg without return\n", "def addition(a, b):\n", " print(a)\n", " print(b)\n", " print(a + b)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "53\n", "68\n" ] } ], "source": [ "addition(15,53)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "25\n", "36\n", "121\n" ] } ], "source": [ "def square(a):\n", " print(a ** 2)\n", " return a ** 2\n", "\n", "# (5 + 6)\n", "\n", "print(square(5) + square(6) + 2 * 5 * 6)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "def greet():\n", " print(\"Good Evening all\")" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Good Evening all\n" ] } ], "source": [ "greet()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Required arguments" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "def addition(a, b):\n", " \"\"\"This function takes two args and returns addition of two args\"\"\"\n", " print(a)\n", " print(b)\n", " print(a + b)" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'This function takes two args and returns addition of two args'" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "addition.__doc__" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "5\n", "10\n" ] } ], "source": [ "addition(5, 5)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "addition() missing 1 required positional argument: 'b'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0maddition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: addition() missing 1 required positional argument: 'b'" ] } ], "source": [ "addition(5)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "def addition(a, b):\n", " return a + b\n", " print(a + b)\n", " print(a, b)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "add = addition(5,5)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "print(add)" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "def addition(a, b):\n", " return a + b, b + a, a ** b\n", " print(a + b)\n", " print(a, b)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "add = addition(5,5)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(10, 10, 3125)\n" ] } ], "source": [ "print(add)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "def addition(a, b):\n", " print(a + b, b + a, a ** b)\n", " print(a + b)\n", " print(a, b)" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 10 3125\n", "10\n", "5 5\n" ] } ], "source": [ "add = addition(5,5)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "print(add)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "range expected 1 argument, got 0", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: range expected 1 argument, got 0" ] } ], "source": [ "print(range())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### default argument" ] }, { "cell_type": "code", "execution_count": 109, "metadata": {}, "outputs": [], "source": [ "def addition(a, b = 0):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 110, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "5\n" ] } ], "source": [ "print(addition(5,10))\n", "print(addition(5))" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "range(0, 5)\n" ] } ], "source": [ "print(range(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Keyword arguments" ] }, { "cell_type": "code", "execution_count": 112, "metadata": {}, "outputs": [], "source": [ "def addition(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": 115, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdef\n", "defabc\n" ] } ], "source": [ "print(addition(a = 'abc', b = 'def'))\n", "print(addition(b = 'abc', a = 'def'))" ] }, { "cell_type": "code", "execution_count": 116, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "abcdef\n" ] } ], "source": [ "print(addition('abc', 'def'))" ] }, { "cell_type": "code", "execution_count": 117, "metadata": { "scrolled": true }, "outputs": [ { "ename": "SyntaxError", "evalue": "keyword argument repeated (, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print(addition(b = 'abc', b = 'def'))\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m keyword argument repeated\n" ] } ], "source": [ "print(addition(b = 'abc', b = 'def'))" ] }, { "cell_type": "code", "execution_count": 118, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "defabccccc\n" ] } ], "source": [ "print(addition('def', b = 'abccccc'))" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print(addition(b = 'abccccc', 'abc'))\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "print(addition(b = 'abccccc', 'abc'))" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "addition() got multiple values for argument 'a'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maddition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'def'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'abccccc'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: addition() got multiple values for argument 'a'" ] } ], "source": [ "print(addition('def', a = 'abccccc'))" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "addition() got an unexpected keyword argument 'd'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maddition\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'abc'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'abc'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: addition() got an unexpected keyword argument 'd'" ] } ], "source": [ "print(addition(d = 'abc', a = 'abc'))" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 4 5 6\n", "['1', '2', '3', '4', '5', '6']\n" ] } ], "source": [ "li = input().split()\n", "\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', '2', '3', '4', '5', '6']\n", "['1', '2', '3', '4', '5', '6']\n" ] } ], "source": [ "li = input()\n", "\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'['" ] }, "execution_count": 124, "metadata": {}, "output_type": "execute_result" } ], "source": [ "li[0]" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123\n", "['1', '2', '3']\n" ] } ], "source": [ "li = input()\n", "\n", "\n", "print(li)" ] }, { "cell_type": "code", "execution_count": 127, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "456\n" ] } ], "source": [ "li = []\n", "for i in input():\n", " li.append(i)" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['4', '5', '6']\n" ] } ], "source": [ "print(li)" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "123\n", "[123, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122]\n" ] } ], "source": [ "n=int(input())\n", "li=[]\n", "li.append(n)\n", "for i in range(n):\n", " li.append(i)\n", " \n", "print(li)" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 55)" ] }, "execution_count": 129, "metadata": {}, "output_type": "execute_result" } ], "source": [ "range(55)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Day Outcomes\n", "\n", "- Sets and Set Methods in Python\n", "- Functions in Python" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.3" } }, "nbformat": 4, "nbformat_minor": 4 }