diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.ipynb b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.ipynb new file mode 100644 index 0000000000..ee71e66239 --- /dev/null +++ b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.ipynb @@ -0,0 +1,295 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

DICTIONARIES

\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d1={}\n", + "type(d1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Adding elements into dictionary

\n", + "

Elements can be added to dictionary as a key, value pairs in the following way:

\n", + " dictionary[key]=value " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'A': 10, 'B': 20, 'C': 30}\n" + ] + } + ], + "source": [ + "d1['A']=10\n", + "d1['B']=20\n", + "d1['C']=30\n", + "print(d1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Changing values in dictionary

\n", + "

Dictionaries are mutable, meaning the values referenced by dictionary keys can be changed. This is illustrated in the following example...

" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Dictionaries before changing value of 'C' {'A': 10, 'B': 20, 'C': 1000}\n", + "Dictionaries after changing value of 'C' {'A': 10, 'B': 20, 'C': 1000}\n" + ] + } + ], + "source": [ + "print(\"Dictionaries before changing value of 'C'\",d1)\n", + "d1[\"C\"] = 1000\n", + "print(\"Dictionaries after changing value of 'C'\",d1)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

We can also update a dictionary with elements of another dictionary as follows..." + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'robot': 40, 'car': 50, 'A': 10, 'B': 20, 'C': 1000}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "toys = {\"robot\": 40, \"car\": 50}\n", + "toys.update(d1)\n", + "toys\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Accessing the keys and values seperately

\n", + "We can get the keys and values seperately using keys() and values() methods respectively. They can converted to a list by wrapping it inside the list() method" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values([40, 50, 20, 1000])" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "toys.values()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['robot', 'car', 'A', 'B', 'C'])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(toys.keys())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Deleting an element from Dictionary

\n", + "

To remove an key-value pair, we use pop(key)

" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'robot': 40, 'car': 50, 'B': 20, 'C': 1000}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "toys.pop(\"A\")\n", + "toys\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "More examples..." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'k1': [0, 1, 2, 3],\n", + " 'k2': (4, 5, 6, 7),\n", + " 'k3': (1, 2, 3, {'k4': [1, 2, 3, 'found you!', 4, 5]})}" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "maze = {\"k1\": list(range(4)), \"k2\": tuple(range(4,8)),\n", + "\"k3\": (1,2, 3, {\"k4\": [1,2,3, \"found you!\", 4, 5]})}\n", + "maze\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'found you!'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "maze[\"k3\"][3][\"k4\"][3]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LISTS_AND_RANGE.ipynb b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LISTS_AND_RANGE.ipynb new file mode 100644 index 0000000000..c74139f852 --- /dev/null +++ b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LISTS_AND_RANGE.ipynb @@ -0,0 +1,346 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

LIST and RANGE

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

LIST

\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['hello', 20, 30]\n" + ] + } + ], + "source": [ + "num= [10,20,30]\n", + "num[0]=\"hello\"\n", + "print(num)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

RANGE

\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 3, 5, 7, 9]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(range(1,10,2))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140]\n", + "\n", + "\n" + ] + } + ], + "source": [ + "numbers = list(range(10, 150, 10))\n", + "print(numbers)\n", + "print(type(numbers))\n", + "print(type([]))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", + "[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]\n" + ] + } + ], + "source": [ + "nested_list = [ \n", + " [1,2,3], \n", + " [4,5,6], \n", + " [7,8,9]\n", + " ]\n", + "nested_range = [\n", + " list(range(11)), \n", + " list(range(10, 21)),\n", + " list(range(20, 31))\n", + " ]\n", + "\n", + "print(nested_list)\n", + "print(nested_range)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 55]\n" + ] + } + ], + "source": [ + "basket1 = [1,2,3,4]\n", + "basket1.append(55)\n", + "print(basket1)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4]\n" + ] + } + ], + "source": [ + "basket1.pop()\n", + "print(basket1)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 100, 3, 4]\n" + ] + } + ], + "source": [ + "basket1.insert(2, 100)\n", + "print(basket1)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 100]\n" + ] + } + ], + "source": [ + "basket1.sort()\n", + "print(basket1)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "100\n" + ] + } + ], + "source": [ + "basket2 =[10, 20, 30, basket1]\n", + "print(basket2[3][4])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "34\n" + ] + } + ], + "source": [ + "basket3 = [\"apples\", \"pears\", \"berries\", \"bananas\"]\n", + "basket4 = [basket2, basket3]\n", + "print(basket4[0][2] + basket4[0][3][-2])" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "basket5 = [\"a\", \"a\", \"a\", 1, 1,1,1, 3, 3, 3,3, 3, 50, 50]\n", + "basket5.count(50)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n", + "[10, 20, 30, 40]\n", + "['car', 'bar', 'star']\n", + "['star', 'bar', 'car', False, 40, 20, 'd', 'b']\n", + "['star', 'bar', 'car', False]\n", + "[-1, -2, -3]\n" + ] + } + ], + "source": [ + "basket6 = [\"a\", \"b\", \"c\", \"d\", 10, 20, 30,40, True, False,\n", + " -1, \"car\", -2, \"bar\", -3, \"star\"]\n", + "\n", + "print(len(basket6))\n", + "print(basket6[4:8])\n", + "print(basket6[11::2])\n", + "print(basket6[15::-2])\n", + "print(basket6[15:8:-2])\n", + "print(basket6[10:15:2])" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 2, 3, 4, 'Chips']" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "basket1[-1] = \"Chips\"\n", + "basket1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.pdf b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/DICTIONARIES.pdf similarity index 100% rename from Basics of the Python/Basic of Python/2.TUPLES_LECTURE/DICTIONARIES.pdf rename to Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/DICTIONARIES.pdf diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LISTS_EXERCISE.pdf b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LISTS_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LISTS_EXERCISE.pdf rename to Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LISTS_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LIST_AND_RANGE.pdf b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LIST_AND_RANGE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LIST_AND_RANGE.pdf rename to Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LIST_AND_RANGE.pdf diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LIST_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LIST_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/2.TUPLES_LECTURE/LIST_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/LIST_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/TUPLES_LECTURE.pdf b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/TUPLES_LECTURE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/2.TUPLES_LECTURE/TUPLES_LECTURE.pdf rename to Basics of the Python/Basic of Python/2.TUPLES_LECTURE/PDF/TUPLES_LECTURE.pdf diff --git a/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/TUPLES.ipynb b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/TUPLES.ipynb new file mode 100644 index 0000000000..7aac3ae6aa --- /dev/null +++ b/Basics of the Python/Basic of Python/2.TUPLES_LECTURE/TUPLES.ipynb @@ -0,0 +1,379 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

TUPLE

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(()) #type() fn prints the datatype of the argument passed to it\n", + "# () is of datatype 'tuple'" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup1 = (40, 50, 60, 70)\n", + "tup1\n", + "type(tup1) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [1,2,3,4]\n", + "mylist\n", + "type(tuple(mylist)) # tuple(arg) converts arg to tuple datatype if possible" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[40, 50, 60, 70]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(tup1) # converts tuple into list" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "((10, 10, 10, 10, 2, 2, 2, 3), ('cat', 'dog', 'bird'), (40, 50, 60, 70))\n" + ] + } + ], + "source": [ + "tup2 = (\n", + " (10, 10, 10, 10,2,2, 2, 3),\n", + " (\"cat\", \"dog\", \"bird\"), \n", + " tup1\n", + " )\n", + "print(tup2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup2.count(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup2[0].count(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'bird'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup2[1].index(\"bird\")\n", + "tup2[1][2]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup3 = tuple(range(0,21, 2))\n", + "tup3\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 20, 30, 'hello']" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mylist = [10, 20, 30, 40]\n", + "mylist[3] = \"hello\"\n", + "mylist\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

Tuples are mutable, meaning its value cannot be changed

" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "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[0;32m 1\u001b[0m \u001b[1;31m# TUPLES ARE IMMUTABLE\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtup2\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"hello\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "# TUPLES ARE IMMUTABLE\n", + "tup2[2][3] = \"hello\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(1, 2, 3, 4, False, True, True)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup4 = 1,2,3,4, False, True, True\n", + "tup4\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "((1, 2, 3, 4, False, True, True), True, False, 100, 9.1245, 'string')" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup5 = tup4, True, False, 100, 9.1245, \"string\"\n", + "tup5\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(tup4)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tup4.index(0)\n", + "tup4.count(1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW.ipynb b/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW.ipynb new file mode 100644 index 0000000000..89a41cb919 --- /dev/null +++ b/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW.ipynb @@ -0,0 +1,199 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

CONTROL FLOW (IF, ELIF, ELSE)/h1>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

IF,ELIF,ELSE

\n", + "
    \n", + "
  • Adds logic to our code
  • \n", + "
  • The if statement performs a task if the condition is true.
  • \n", + "
  • The else statement performs a default when the if statement's condition is false.
  • \n", + "
  • Else statements cannot exist on their own.
  • \n", + "
  • The elif statement (short for else if) works in between if and else statements.
  • \n", + "
  • Elif only performs a task when true, and the if statement is false.\n", + "
  • \n", + "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

DEPENDS ON INDENTATION!

" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "a = 10; b = 25 ; c = 100; d = (\"cat\", \"dog\", \"bird\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "a is true!\n", + "b is true!\n", + "c is true!\n", + "d is false!\n" + ] + } + ], + "source": [ + "if a == 10:\n", + " print(\"a is true!\")\n", + " if b < 40:\n", + " print(\"b is true!\")\n", + " if c > 9:\n", + " print(\"c is true!\")\n", + " if \"zebra\" in d:\n", + " print(\"d is true!\")\n", + " else:\n", + " print(\"d is false!\")\n", + " else:\n", + " print(\"c is false!\")\n", + " else:\n", + " print(\"b is false!\")\n", + "else:\n", + " print(\"a is false!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Keep looking!\n" + ] + } + ], + "source": [ + "num = eval(input(\"Enter a number: \"))\n", + "if num >= 3:\n", + " print(\"Keep looking!\")\n", + "elif d[num] == \"cat\":\n", + " print(\"Found a {}\".format(d[num]))\n", + "elif d[num] == \"dog\":\n", + " print(\"Found a {}\".format(d[num]))\n", + "else:\n", + " print(\"Found a bird!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('cat', 'dog', 'bird')" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "d" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n", + " [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25, 26, 27, 28, 29, 30]]" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "total = [list(range(11)), list(range(11, 21)), list(range(21, 31))]\n", + "total" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 in list one\n" + ] + } + ], + "source": [ + "v = eval(input(\"Enter a number: \"))\n", + "if v in total[0]:\n", + " print(v, \"in list one\")\n", + "elif v in total[1]:\n", + " print(v, \"in list two\")\n", + "elif v in total[2]:\n", + " print(v, \"in list three\")\n", + "else:\n", + " print(\"not in any list\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW.pdf b/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW.pdf similarity index 100% rename from Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW.pdf rename to Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW.pdf diff --git a/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW_EXERCISE.pdf b/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW_EXERCISE.pdf rename to Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/CONTROL_FLOW_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/3.CONTROL_FLOW_SOLUTIONS/PDF/CONTROL_FLOW_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/FOR_LOOPS.ipynb b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/FOR_LOOPS.ipynb new file mode 100644 index 0000000000..298b562424 --- /dev/null +++ b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/FOR_LOOPS.ipynb @@ -0,0 +1,320 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

FOR LOOPS (List, Tuples and Dictionaries)

\n", + "
    \n", + "
  • for loops iterate over elements from a sequence (list, tuple and dictionary)
  • \n", + "
  • Outputs all the elements from the sequence
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "for greeting in range(2):\n", + " print(\"Hello world!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tiger\n", + "lion\n", + "jaguar\n", + "leopard\n" + ] + } + ], + "source": [ + "cats = [\"tiger\", \"lion\", \"jaguar\", \"leopard\"]\n", + "for cat in cats:\n", + " print(cat)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 30 0\n", + "1 31 1\n", + "2 32 4\n", + "3 33 9\n" + ] + } + ], + "source": [ + "for num in range(4):\n", + " print(num, num + 30, num*num)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[10, 20, 30]\n", + "[3.5, 4.5, 5.5]\n", + "['sword', 'hammer', 'shield']\n" + ] + } + ], + "source": [ + "nest1 = [[10, 20, 30], [3.5, 4.5, 5.5],\n", + "[\"sword\", \"hammer\", \"shield\"]]\n", + "for i in range(3):\n", + " print(nest1[i])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Shield:Power level = 35.5'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "weapons = []\n", + "for i in range(3):\n", + " s=nest1[2][i].capitalize() + \":Power level = \"+ str(nest1[0][i] + nest1[1][i])\n", + " weapons.append(s)\n", + "weapons[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 * 1 = 1\n", + "2 * 2 = 4\n", + "3 * 3 = 9\n", + "4 * 4 = 16\n", + "5 * 5 = 25\n", + "6 * 6 = 36\n", + "7 * 7 = 49\n" + ] + } + ], + "source": [ + "for i in range(1, 8):\n", + " print(\"{} * {} = {}\".format(i, i, (i*i)))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.000000 * 1.000000 = 1.000000\n", + "2.000000 * 2.000000 = 4.000000\n", + "3.000000 * 3.000000 = 9.000000\n", + "4.000000 * 4.000000 = 16.000000\n", + "5.000000 * 5.000000 = 25.000000\n", + "6.000000 * 6.000000 = 36.000000\n", + "7.000000 * 7.000000 = 49.000000\n" + ] + } + ], + "source": [ + "for k in range(1, 8):\n", + " print(\"%f * %f = %f\" % (k, k, (k*k)))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 20 30\n", + "a b c\n", + "200 400 800\n", + "10 20 30\n", + "a b c\n", + "200 400 800\n", + "10 20 30\n", + "a b c\n", + "200 400 800\n" + ] + } + ], + "source": [ + "tup1 = ((10, 20, 30), (\"a\", \"b\", \"c\"), (200, 400, 800))\n", + "# TUPLE UNPACKING!\n", + "for i in range(3):\n", + " for x, y, z in tup1:\n", + " print(x, y, z)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 20, 30, 40]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cakes = [\"chocolate\", \"lemon\", \"carrot\", \"vanilla\"]\n", + "d1 = {\"k1\": tuple(cakes), \"k2\": [10, 20, 30, 40]}\n", + "d1[\"k2\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n", + "20\n", + "30\n", + "40\n" + ] + } + ], + "source": [ + "for d in d1[\"k2\"]:\n", + " print(d)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "k1 : chocolate\n", + "k1 : lemon\n", + "k1 : carrot\n", + "k1 : vanilla\n", + "k2 : 10\n", + "k2 : 20\n", + "k2 : 30\n", + "k2 : 40\n" + ] + } + ], + "source": [ + "for x in d1:\n", + " for i in range(4):\n", + " print(x, \":\", d1[x][i])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Tiger \n", + " Level: \n", + " SERIES\n", + "Lion \n", + " Level: \n", + " CRITICAL\n", + "Jaguar \n", + " Level: \n", + " VERY CRITICAL\n", + "Leopard \n", + " Level: \n", + " STABLE\n" + ] + } + ], + "source": [ + "cats\n", + "endangered = [\"series\", \"critical\",\n", + "\"very critical\", \"stable\"]\n", + "for c in range(4):\n", + " print(cats[c].capitalize(), \"\\n Level: \\n\", endangered[c].upper())\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/ADVANCED_LOOPS.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/ADVANCED_LOOPS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/ADVANCED_LOOPS.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/ADVANCED_LOOPS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/BPC.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/BPC.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/BPC.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/BPC.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/CONTROL_FLOW_IN_LOOPS.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/CONTROL_FLOW_IN_LOOPS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/CONTROL_FLOW_IN_LOOPS.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/CONTROL_FLOW_IN_LOOPS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/EXERCISE.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/EXERCISE.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/FOR_LOOPS.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/FOR_LOOPS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/FOR_LOOPS.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/FOR_LOOPS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/SOLUTIONS.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/WHILE_LOOPS.pdf b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/WHILE_LOOPS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/4.WHILE_LOOPS/WHILE_LOOPS.pdf rename to Basics of the Python/Basic of Python/4.WHILE_LOOPS/PDF/WHILE_LOOPS.pdf diff --git a/Basics of the Python/Basic of Python/4.WHILE_LOOPS/WHILE_LOOPS.ipynb b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/WHILE_LOOPS.ipynb new file mode 100644 index 0000000000..99d3b32396 --- /dev/null +++ b/Basics of the Python/Basic of Python/4.WHILE_LOOPS/WHILE_LOOPS.ipynb @@ -0,0 +1,489 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

WHILE LOOPS

\n", + "
    \n", + "
  • Similar to a for loop, while loop output each element, but based on a while statement being True
  • \n", + "
  • Once the while statement is no longer true, the while loop ends
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50\n", + "60\n", + "70\n", + "80\n", + "90\n", + "100\n" + ] + } + ], + "source": [ + "x=50\n", + "while x <= 100:\n", + " print(x)\n", + " x += 10" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The multiplication is: 1\n", + "The multiplication is: 2\n", + "The multiplication is: 4\n", + "The multiplication is: 8\n", + "The multiplication is: 16\n" + ] + } + ], + "source": [ + "num=1 \n", + "while num<20:\n", + " print(\"The multiplication is: \",num)\n", + " num *= 2" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The division is: 1000 2\n", + "The division is: 333 2\n", + "The division is: 111 2\n", + "The division is: 37 2\n", + "The division is: 12 2\n" + ] + } + ], + "source": [ + "div=1000\n", + "while div>10:\n", + " print(\"The division is: \",round(div),2)\n", + " div/=3" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The floor is: 200\n", + "The floor is: 100.0\n", + "The floor is: 50.0\n", + "The floor is: 25.0\n", + "The floor is: 12.5\n" + ] + } + ], + "source": [ + "floor = 200\n", + "while floor > 10:\n", + " print(\"The floor is: \",floor)\n", + " floor /= 2" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "count down to lift off in 10 seconds!\n", + "count down to lift off in 9 seconds!\n", + "count down to lift off in 8 seconds!\n", + "count down to lift off in 7 seconds!\n", + "count down to lift off in 6 seconds!\n", + "count down to lift off in 5 seconds!\n", + "count down to lift off in 4 seconds!\n", + "count down to lift off in 3 seconds!\n", + "count down to lift off in 2 seconds!\n", + "count down to lift off in 1 seconds!\n", + "Blast off !\n" + ] + } + ], + "source": [ + "sub = 10\n", + "while sub > 0:\n", + " print(\"count down to lift off in \",str(sub),\"seconds!\")\n", + " sub -= 1\n", + " if sub ==0:\n", + " print(\"Blast off !\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1+1=2\n", + "2+2=4\n", + "3+3=6\n", + "4+4=8\n", + "5+5=10\n", + "6+6=12\n", + "7+7=14\n" + ] + } + ], + "source": [ + "p=1\n", + "while p < 8:\n", + " print(f\"{p}+{p}={p+p}\")\n", + " p += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1.000000 * 1.000000 = 1.000000\n", + "2.000000 * 2.000000 = 4.000000\n", + "3.000000 * 3.000000 = 9.000000\n", + "4.000000 * 4.000000 = 16.000000\n", + "5.000000 * 5.000000 = 25.000000\n", + "6.000000 * 6.000000 = 36.000000\n", + "7.000000 * 7.000000 = 49.000000\n" + ] + } + ], + "source": [ + "a=1\n", + "while a <8:\n", + " print(\"%f * %f = %f\"%(a,a,a*a))\n", + " a +=1" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1/1.000000 =1\n", + "2/2.000000 =1\n", + "3/3.000000 =1\n", + "4/4.000000 =1\n", + "5/5.000000 =1\n", + "6/6.000000 =1\n", + "7/7.000000 =1\n", + "8/8.000000 =1\n", + "9/9.000000 =1\n", + "10/10.000000 =1\n", + "11/11.000000 =1\n", + "12/12.000000 =1\n", + "13/13.000000 =1\n", + "14/14.000000 =1\n" + ] + } + ], + "source": [ + "p=1;o=1\n", + "while p < 15 and o < 15:\n", + " print(\"%i/%f =%i\"%(p,o,p/o))\n", + " p +=1\n", + " o+=1" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5+10+5=15\n", + "6+11+6=17\n", + "7+12+7=19\n", + "8+13+8=21\n", + "9+14+9=23\n", + "10+15+10=25\n", + "11+16+11=27\n" + ] + } + ], + "source": [ + "num, cake = 5,10\n", + "while num < 12:\n", + " while cake < 17:\n", + " print(f\"{num}+{cake}+{num}={num+cake}\")\n", + " cake += 1\n", + " num += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Java\n", + "R\n", + "C#\n", + "Julia\n", + "CSS\n", + "Go\n" + ] + } + ], + "source": [ + "lang = [\"Python\",\"Java\",\"JavaScript\",\"R\",\"VBA\",\n", + " \"C#\",\"C++\",\"Julia\", \"HTML\",\"CSS\",\"C\",\"Go\"]\n", + "\n", + "x=1\n", + "while x < len(lang):\n", + " print(lang[x])\n", + " x += 2" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "g=0\n", + "while g < 5:\n", + " print(g)\n", + " g += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n" + ] + } + ], + "source": [ + "for d in range(5):\n", + " print(d)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 + 1 = 2\n", + "2 + 2 = 4\n", + "3 + 3 = 6\n", + "4 + 4 = 8\n" + ] + } + ], + "source": [ + "a=1\n", + "b=1\n", + "while a < 5:\n", + " while b< 5:\n", + " print (f\"{a} + {a} = {b+a}\")\n", + " a += 1\n", + " b += 1" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1+1=2\n", + "1+2=3\n", + "1+3=4\n", + "1+4=5\n", + "2+1=3\n", + "2+2=4\n", + "2+3=5\n", + "2+4=6\n", + "3+1=4\n", + "3+2=5\n", + "3+3=6\n", + "3+4=7\n", + "4+1=5\n", + "4+2=6\n", + "4+3=7\n", + "4+4=8\n" + ] + } + ], + "source": [ + "for j in range(1, 5):\n", + " for k in range(1, 5):\n", + " print(f\"{j}+{k}={j+k}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 + 1 = 2\n", + "2 + 2 = 4\n", + "3 + 3 = 6\n", + "4 + 4 = 8\n" + ] + } + ], + "source": [ + "for t in range(1, 5):\n", + " print(\"{} + {} = {}\".format(t, t, t+t))" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "while num1 = 0\n", + "50\n", + "80\n", + "110\n", + "while num1 = 20\n", + "70\n", + "100\n", + "130\n", + "while num1 = 40\n", + "90\n", + "120\n", + "150\n", + "while num1 = 60\n", + "110\n", + "140\n", + "170\n", + "while num1 = 80\n", + "130\n", + "160\n", + "190\n", + "while num1 = 100\n", + "150\n", + "180\n", + "210\n" + ] + } + ], + "source": [ + "num1 = 0\n", + "while num1 < 110:\n", + " print(\"while num1 = \", num1)\n", + " for g in [50, 80, 110]:\n", + " res = num1 + g\n", + " print(res)\n", + " num1 += 20" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/LIST_COMPREHENSION.ipynb b/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/LIST_COMPREHENSION.ipynb new file mode 100644 index 0000000000..df14a702fb --- /dev/null +++ b/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/LIST_COMPREHENSION.ipynb @@ -0,0 +1,271 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

LIST COMPREHENSION

\n", + "
    \n", + "
  • List comprehension is an elegant way to define and create a list in Python
  • \n", + "
  • Relies heavily on for loops
  • \n", + "
  • Significantly reduces lines of code
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['u', 'n', 'i', 'v', 'e', 'r', 's', 'e']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cosmos = []\n", + "for i in \"universe\":\n", + " cosmos.append(i)\n", + "cosmos\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['u', 'n', 'i', 'v', 'e', 'r', 's', 'e']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[u for u in \"universe\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['u', 'n', 'i', 'v', 'e', 'r', 's', 'e']" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(\"universe\")" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "norm = []\n", + "for n in range(0, 11):\n", + " result = n**2\n", + " norm.append(result)\n", + "norm\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "comp = [x**2 for x in range(0, 11)]\n", + "comp\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['cat is hungry!',\n", + " 'dog is hungry!',\n", + " 'rabbit is hungry!',\n", + " 'duck is hungry!',\n", + " 'mouse is hungry!',\n", + " 'piglet is hungry!']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pets = [\"cat\", \"dog\", \"rabbit\", \"duck\", \"mouse\",\"piglet\"]\n", + "rad = []\n", + "for i in range(len(pets)):\n", + " res = pets[i] + \" is hungry!\"\n", + " rad.append(res)\n", + "rad\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[27, 125, 343, 729]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[numbers**3 for numbers in range(0, 11) if numbers >=3 and numbers % 2 == 1]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[27, 125, 343, 729]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "count = []\n", + "for num in range(0,11):\n", + " if num >= 3 and num %2 == 1:\n", + " new = num**3\n", + " count.append(new)\n", + "count\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "growth = []\n", + "for grow in range(0, 20):\n", + " if grow % 2 == 0 and grow > 2:\n", + " growth.append(grow)\n", + "growth" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[4, 6, 8, 10, 12, 14, 16, 18]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[g for g in range(0, 20) if g % 2==0 and g > 2]\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/COMPREHENSION_EXERCISE.pdf b/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/COMPREHENSION_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/COMPREHENSION_EXERCISE.pdf rename to Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/COMPREHENSION_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/COMPREHENSION_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/COMPREHENSION_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/COMPREHENSION_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/COMPREHENSION_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/LIST_COMPREHENSION.pdf b/Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/LIST_COMPREHENSION.pdf similarity index 100% rename from Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/LIST_COMPREHENSION.pdf rename to Basics of the Python/Basic of Python/5.LIST_COMPREHENSION/PDF/LIST_COMPREHENSION.pdf diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/BUILT_IN_FUNCTIONS.ipynb b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/BUILT_IN_FUNCTIONS.ipynb new file mode 100644 index 0000000000..d399339e72 --- /dev/null +++ b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/BUILT_IN_FUNCTIONS.ipynb @@ -0,0 +1,260 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

BUILT-IN FUNCTIONS

\n", + "
    \n", + "
  • Built-in Python functions are always available on command
  • \n", + "
  • As of Python 3.12, there are almost 70+ built in functions
  • \n", + "
  • Link to official DOC
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[83, 14, 64, 71, 11, 36, 69, 72, 45, 93]" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import random as rd\n", + "rd.seed(10)\n", + "numbers = rd.sample(range(10, 100), k = 10)\n", + "numbers\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(numbers)\n", + "min(numbers)\n", + "sum(numbers)\n", + "round(9.12345, 4)\n", + "abs(10)\n", + "eval(\"4.5\")\n", + "pow(4, 2)\n", + "10**2\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[11, 14, 36, 45, 64, 69, 71, 72, 83, 93]" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_numbers = sorted(numbers)\n", + "new_numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[83, 14, 64, 71, 11, 36, 69, 72, 45, 93]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[121, 196, 1296, 2025, 4096, 4761, 5041, 5184, 6889, 8649]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[pow(x, 2) for x in new_numbers]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum([i < 40 for i in new_numbers])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['10', 8.12345, 100, -30, -200]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = [\"10\", 8.12345, 100, -30, -200]\n", + "data\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['10', 8.12345, 100, -30, -200]" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_data = eval(data[0]) + int(data[1]) + data[2] + abs(data[3]) + abs(data[\n", + "4])\n", + "new_data\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[200, 100, 30]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "clean_data = sorted([abs(k) for k in [int(i) for i in data]])[2:][3::-1]\n", + "clean_data" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'The top three values 200, 100, and 30. My favourite number is 200'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"The top three values {0}, {1}, and {2}. My favourite number is {0}\".format(*clean_data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS.ipynb b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS.ipynb new file mode 100644 index 0000000000..86025c8c11 --- /dev/null +++ b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS.ipynb @@ -0,0 +1,182 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

FUNCTIONS

\n", + "
    \n", + "
  • Reduces the amount of code
  • \n", + "
  • Can be called anywhere in the notebook or cell
  • \n", + "
  • Functions must be given a name
  • \n", + "
  • Can contain multiple parameters or none at all
  • \n", + "
  • A parameter can have default values
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "function" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def square(arg1=20, arg2=50):\n", + " \"\"\"This adds two numbers together\"\"\"\n", + " return( arg1 + arg2)\n", + "val = square(90, 110)\n", + "type(square)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n", + "64\n", + "256\n" + ] + }, + { + "data": { + "text/plain": [ + "336" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def squared(num):\n", + " return num**2\n", + "def cubed(num):\n", + " return num**3\n", + "def quad(num):\n", + " return num**4\n", + "v = 4\n", + "print(squared(v))\n", + "print(cubed(v))\n", + "print(quad(v))\n", + "total = squared(v) + cubed(v) + quad(v)\n", + "total" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def greet():\n", + " \"\"\"Enter the time and print appropriate greeting!\"\"\"\n", + " time = eval(input(\"Enter the time: \"))\n", + " if time >= 6 and time < 12:\n", + " print(\"Good morning!\")\n", + " return squared(time)\n", + " elif time >= 12 and time < 18:\n", + " print(\"Afternoon!\")\n", + " return cubed(time)\n", + " elif time >= 18 and time < 21:\n", + " print(\"Good evening!\")\n", + " return quad(time)\n", + " else:\n", + " print(\"Good night!\")\n", + " return time\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Good morning!\n" + ] + } + ], + "source": [ + "number = greet()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "49" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "number" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/BUILT_IN_FUNCTIONS.pdf b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/BUILT_IN_FUNCTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/BUILT_IN_FUNCTIONS.pdf rename to Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/BUILT_IN_FUNCTIONS.pdf diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_EXERCISE.pdf b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_EXERCISE.pdf rename to Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_LECTURE.pdf b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_LECTURE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_LECTURE.pdf rename to Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_LECTURE.pdf diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/FUNCTIONS_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/FUNCTIONS_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/RANDOM_LECTURE1.pdf b/Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/RANDOM_LECTURE1.pdf similarity index 100% rename from Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/RANDOM_LECTURE1.pdf rename to Basics of the Python/Basic of Python/6.FUNCTIONS_LECTURE/PDF/RANDOM_LECTURE1.pdf diff --git a/Basics of the Python/Basic of Python/7.LAMBDA/LAMBDA.ipynb b/Basics of the Python/Basic of Python/7.LAMBDA/LAMBDA.ipynb new file mode 100644 index 0000000000..aa1f014848 --- /dev/null +++ b/Basics of the Python/Basic of Python/7.LAMBDA/LAMBDA.ipynb @@ -0,0 +1,470 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

LAMBDA EXPRESSIONS

\n", + "
    \n", + "
  • Lambda is called an anonymous function meaning it does not require a def name.
  • \n", + "
  • Does not require us to specify an explicit return statement
  • \n", + "
  • Lambdas are single line expressions
  • \n", + "
  • Can be used as a substitute for basic function
  • \n", + "
  • Cannot add a docstring
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def add(arg1, arg2):\n", + " return arg1 + arg2\n", + "add(10, 20)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "g = lambda x, y , z: x + y + z" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "130" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(g)\n", + "g(10,20, 100)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "() missing 3 required positional arguments: 'x', 'y', and 'z'", + "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[0mg\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: () missing 3 required positional arguments: 'x', 'y', and 'z'" + ] + } + ], + "source": [ + "g()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "g2 = lambda num1,num2 : num1 *2 + num2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "25" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "g2(10,5)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def check(num):\n", + " return num % 2 == 0 or num > 5\n", + "check(9)\n", + "c = lambda num : num %2 == 0 or num > 5\n", + "check(10)\n", + "c(9)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def chop(num):\n", + " if num>= 10 and num < 30:\n", + " return num\n", + "chop(50)\n", + "c1 = lambda num2 : num2 > 10 and num2 < 30\n", + "c1(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(range(30))[10:20]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "import random as rd\n", + "w = lambda s, num= rd.randint(0,30): \"inside\" if num \\\n", + " in range(rd.randint(0,s)) else \"outside\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'outside'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "w(9)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def compare(a, b):\n", + " if a > 10:\n", + " return a\n", + " else:\n", + " return b" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "a = 20; b = 5\n", + "con = lambda: a if a > 10 else b" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 3\n", + "con()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'big'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def size(x):\n", + " if x > 100:\n", + " return \"big\"\n", + " else:\n", + " return \"small\"\n", + "size(800)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "big = lambda x: \"big\" if x > 100 else \"small\"" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'small'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "big(9)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f = lambda x:x *x\n", + "f(10)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[f(x) for x in range(10)]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[f(x) for x in range(10)]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "f = lambda x: x**2 if x in range(10) else \"outside\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'outside'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f(20)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "def inside(num):\n", + " if num in list(range(10)):\n", + " return num**2\n", + " else:\n", + " print(\"outside\")" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inside(3)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/7.LAMBDA/ERRORS_EXERCISE.pdf b/Basics of the Python/Basic of Python/7.LAMBDA/PDF/ERRORS_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/7.LAMBDA/ERRORS_EXERCISE.pdf rename to Basics of the Python/Basic of Python/7.LAMBDA/PDF/ERRORS_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/7.LAMBDA/ERRORS_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/7.LAMBDA/PDF/ERRORS_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/7.LAMBDA/ERRORS_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/7.LAMBDA/PDF/ERRORS_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/7.LAMBDA/LAMBDA.pdf b/Basics of the Python/Basic of Python/7.LAMBDA/PDF/LAMBDA.pdf similarity index 100% rename from Basics of the Python/Basic of Python/7.LAMBDA/LAMBDA.pdf rename to Basics of the Python/Basic of Python/7.LAMBDA/PDF/LAMBDA.pdf diff --git a/Basics of the Python/Basic of Python/7.LAMBDA/TRY_EXCEPT_AND_FINALLY.pdf b/Basics of the Python/Basic of Python/7.LAMBDA/PDF/TRY_EXCEPT_AND_FINALLY.pdf similarity index 100% rename from Basics of the Python/Basic of Python/7.LAMBDA/TRY_EXCEPT_AND_FINALLY.pdf rename to Basics of the Python/Basic of Python/7.LAMBDA/PDF/TRY_EXCEPT_AND_FINALLY.pdf diff --git a/Basics of the Python/Basic of Python/8.FILES_SECTION/FILES.ipynb b/Basics of the Python/Basic of Python/8.FILES_SECTION/FILES.ipynb new file mode 100644 index 0000000000..8e91cfa74e --- /dev/null +++ b/Basics of the Python/Basic of Python/8.FILES_SECTION/FILES.ipynb @@ -0,0 +1,227 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

WORKING WITH TXT FILES

\n", + "
    \n", + "
  • Text files are built-in Python feature. No import modules required
  • \n", + "
  • Text files have the .txt format
  • \n", + "
  • Use either open or with open to either read or write a text file
  • \n", + "
  • Use one of four modes for txt files:\n", + "
      \n", + "
    • \"w\" for writing
    • \n", + "
    • \"r\" for reading
    • \n", + "
    • \"r+\" for both reading and writing
    • \n", + "
    • \"a\" for appending new written lines
    • \n", + "
    \n", + "
  • \n", + "
" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "desk = \"C:/Users/Varshni/Desktop/\"\n", + "f = open(desk + \"first.txt\", \"w\")\n", + "f.write(\"This is my first file!\")\n", + "f.write(\"\\nHello world!\")\n", + "f.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "f1 = open(desk + \"first.txt\", \"a\")\n", + "f1.write(\"\\nGood Night!\")\n", + "f1.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "with open(desk + \"first.txt\", \"a\") as g:\n", + " g.write(\"\\nCool!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "This is my first file!\n", + "Hello world!\n", + "Good Night!\n", + "Cool!\n" + ] + } + ], + "source": [ + "f2 = open(desk + \"first.txt\", \"r\")\n", + "print(f2.read())\n", + "f2.close()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "ename": "FileNotFoundError", + "evalue": "[Errno 2] No such file or directory: 'C:/Users/Varshni/Desktop/FILE_0/second.txt'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mFileNotFoundError\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 2\u001b[0m \u001b[0mpath\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"C:/Users/Varshni/Desktop/FILE_{}/second.txt\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgear\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[1;32m----> 4\u001b[1;33m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"w\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mf3\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 5\u001b[0m \u001b[0mf3\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrite\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgear\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\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[0;32m 6\u001b[0m \u001b[1;31m# f3.write(\"\\n\"+gear[1])\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'C:/Users/Varshni/Desktop/FILE_0/second.txt'" + ] + } + ], + "source": [ + "gear = [\"Blade Wolf\", \"Raiden\", \"Sam\"]\n", + "path = \"C:/Users/Varshni/Desktop/FILE_{}/second.txt\"\n", + "for i in range(len(gear)):\n", + " with open(path.format(i), \"w\") as f3:\n", + " f3.write(gear[i])\n", + " # f3.write(\"\\n\"+gear[1])\n", + " f3.write(\"\\n{} is cooler than {}\".format(gear[0], gear[1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "simpsons = [[\"Bart in detention\", \"Lisa at MIT\"],\n", + "[\"Dougnuts...Mmm\", \"Homer: 'Nom, nom, nom!'\"]]\n", + "transformers = [[\"HotRod opens matrix\", \"Optimus returns!\"],\n", + "[\"Unicron explodes\", \"Megatron jumps\"]]\n", + "def File(text):\n", + " path = \"C:/Users/Varshni/Desktop/\"\n", + " for i in range(len(text)):\n", + " with open(path + \"cartoon_{}.txt\".format(i),\"w\") as f:\n", + " f.write(text[i][0])\n", + " f.write(\"\\n\"+text[i][1])\n", + "File(transformers)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "dc = open(\"skynet.txt\", \"w\")\n", + "dc.write(\"\"\"Terminator is \\n\n", + "hunting John connor \\n\n", + "and Sarah is protecting him\"\"\")\n", + "dc.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "d = open(\"skynet.txt\", \"r+\")\n", + "d.write(\"\\n hasta la vista baby!\")\n", + "end = d.readlines()\n", + "d.close()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "film = \"\".join(end)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Carl\" in film.replace(\"\\n\", \" \").split()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "vars", + "language": "python", + "name": "vars" + }, + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basics of the Python/Basic of Python/8.FILES_SECTION/FILES_EXERCISE.pdf b/Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILES_EXERCISE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/8.FILES_SECTION/FILES_EXERCISE.pdf rename to Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILES_EXERCISE.pdf diff --git a/Basics of the Python/Basic of Python/8.FILES_SECTION/FILES_LECTURE.pdf b/Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILES_LECTURE.pdf similarity index 100% rename from Basics of the Python/Basic of Python/8.FILES_SECTION/FILES_LECTURE.pdf rename to Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILES_LECTURE.pdf diff --git a/Basics of the Python/Basic of Python/8.FILES_SECTION/FILE_SOLUTIONS.pdf b/Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILE_SOLUTIONS.pdf similarity index 100% rename from Basics of the Python/Basic of Python/8.FILES_SECTION/FILE_SOLUTIONS.pdf rename to Basics of the Python/Basic of Python/8.FILES_SECTION/PDF/FILE_SOLUTIONS.pdf diff --git a/Basics of the Python/Basic of Python/8.FILES_SECTION/skynet.txt b/Basics of the Python/Basic of Python/8.FILES_SECTION/skynet.txt new file mode 100644 index 0000000000..971646552a --- /dev/null +++ b/Basics of the Python/Basic of Python/8.FILES_SECTION/skynet.txt @@ -0,0 +1,6 @@ +Terminator is + +hunting John connor + +and Sarah is protecting him + hasta la vista baby! \ No newline at end of file