From 5fdced05cf9aff410aabebf4f1283ee97c6261d0 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 14:49:50 +0530 Subject: [PATCH 1/9] i submittted basic --- 001basic.ipynb | 439 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 439 insertions(+) create mode 100644 001basic.ipynb diff --git a/001basic.ipynb b/001basic.ipynb new file mode 100644 index 0000000..1401121 --- /dev/null +++ b/001basic.ipynb @@ -0,0 +1,439 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled3.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/001basic.ipynb)" + ] + }, + { + "metadata": { + "id": "-Rwj525iAYKc", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# The Zen of Python\n", + "import this" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "eLSo4Bo6Ahbl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "d7e8a837-9c83-4efa-9fc9-9d890ac0636d" + }, + "cell_type": "code", + "source": [ + "# Comments\n", + "# This is a python tutorial and a single line comment\n", + "''' This is a multiline comment\n", + " pretty awesome!!\n", + " Let me introduce you to jennifer!'''" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "' This is a multiline comment\\n pretty awesome!!\\n Let me introduce you to jennifer!'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 1 + } + ] + }, + { + "metadata": { + "id": "p1G3j0kiAnod", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Simple imports\n", + "import math\n", + "import random" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "PyhlIJjQAsFn", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# importing specific functions from modules\n", + "# imports just the factorial function from math\n", + "from math import factorial\n", + "\n", + "# imports all the functions from math\n", + "from math import *" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "x7KhDJM6AxUd", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Giving aliases\n", + "# The Module name is alaised\n", + "import math as m\n", + "\n", + "# The function name is alaised\n", + "from math import factorial as fact" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "MnOj05kfA3l2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "11a86ae2-8c25-439f-a2c9-437f4ab9f726" + }, + "cell_type": "code", + "source": [ + "# Calling imported functions\n", + "# If you import the module you have to call the functions from the module\n", + "import math\n", + "print (math.factorial(12))\n", + "\n", + "# If you import the functions you can call the function as if it is in your program\n", + "from random import randrange as rg\n", + "print (rg(23, 1000))" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "479001600\n", + "962\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "x9QHu_b0A5jd", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Variables\n", + "msg = \"Python!\" # String\n", + "v2 = 'Python!' # Also String works same\n", + "v1 = 2 # Numbers\n", + "v3 = 3.564 # Floats / Doubles\n", + "v4 = True # Boolean (True / False)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "WIQZBMkMA_w-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 121 + }, + "outputId": "d9a28ea6-1827-4785-ea7b-44a674323228" + }, + "cell_type": "code", + "source": [ + "# print() \n", + "# automatically adds a newline\n", + "print (msg)\n", + "print (v2)\n", + "print (v1)\n", + "print (v3)\n", + "print (v4)\n", + "print (\"Hello Python!\")" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Python!\n", + "Python!\n", + "2\n", + "3.564\n", + "True\n", + "Hello Python!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "s8QzrRBeBEL-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "d3cb6890-9f69-4b0f-b5a1-8385c94405e6" + }, + "cell_type": "code", + "source": [ + "# Note: Both \" and ' can be used to make strings. And this flexibility allows for the following:\n", + "msg2 = 'Jennifer said, \"I love Python!\"'\n", + "msg3 = \"After that Jennifer's Python Interpreter said it back to her!\"\n", + "msg4 = 'Of Course she used the command `print(\"I love Jennifer\")`'\n", + "\n", + "print (msg2)\n", + "print (msg3)\n", + "print (msg4)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer said, \"I love Python!\"\n", + "After that Jennifer's Python Interpreter said it back to her!\n", + "Of Course she used the command `print(\"I love Jennifer\")`\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "lq3p01BzBJ-N", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "11d6d831-ad12-4958-c93f-490bbb31b391" + }, + "cell_type": "code", + "source": [ + "# input()\n", + "msg = input()" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1245\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TF2_AzoWBPKG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "bd6d76da-0b21-4d11-8b20-c30bf8147521" + }, + "cell_type": "code", + "source": [ + "# input() with message\n", + "msg = input (\"Provide some input: \")\n", + "print (msg)" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Provide some input: 25874\n", + "25874\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "R-ZLll-LBUIX", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "8fb3edbe-c00e-42cc-e50b-275ee58ab209" + }, + "cell_type": "code", + "source": [ + "# Check for specific input without storing it\n", + "if input(\"Enter something: \") == \"something\":\n", + " print (\"Something something\")\n", + "else: print (\"Not Something\")" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter something: 2574\n", + "Not Something\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "WcS5CGB9BYxH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "outputId": "b43aeeaf-8610-458d-b2a8-868a0597fafb" + }, + "cell_type": "code", + "source": [ + "# Python takes every input as a string\n", + "# So, if required you can convert to the required type\n", + "msg = input(\"Enter a number: \")\n", + "print (type(msg))\n", + "\n", + "msg = int(input (\"Enter a number again, if not a number this will throw an error: \"))\n", + "print (type(msg))" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number: 215\n", + "\n", + "Enter a number again, if not a number this will throw an error: 24551\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QbsvRodCBejl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 347 + }, + "outputId": "b8c441aa-7bb1-4f93-a777-f4ff0bbdf6d3" + }, + "cell_type": "code", + "source": [ + "# Basic Arithmetic operations\n", + "# Add\n", + "print (3 + 2)\n", + "print (3.4565 + 56.232)\n", + "print ('------------')\n", + "\n", + "# Subtract\n", + "print (3 - 4)\n", + "print (34.56 - 3.78)\n", + "print ('------------')\n", + "\n", + "# Multiply\n", + "print (4 * 3)\n", + "print (7.56 * 34)\n", + "print ('------------')\n", + "\n", + "# Division\n", + "print (5 / 2)\n", + "print (5.0 / 2)\n", + "print (5 / 2.0)\n", + "print (25.0 / 5)\n", + "print ('------------')\n", + "\n", + "# Exponents\n", + "print (4 ** 4)\n", + "print (5.67 ** 3)\n", + "print ('------------')\n", + "\n", + "# Modulo\n", + "print (10%3)\n", + "print (10%11)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "59.6885\n", + "------------\n", + "-1\n", + "30.78\n", + "------------\n", + "12\n", + "257.03999999999996\n", + "------------\n", + "2.5\n", + "2.5\n", + "2.5\n", + "5.0\n", + "------------\n", + "256\n", + "182.28426299999998\n", + "------------\n", + "1\n", + "10\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 70ae8c09617a3d550ddbbd66a061b5a9d6e9c53b Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 15:16:31 +0530 Subject: [PATCH 2/9] Add files via upload I HAVE SUBMITTED THE WEEK 1 --- Untitled3.ipynb | 416 +++++++++++++ Untitled4.ipynb | 291 +++++++++ Untitled5.ipynb | 298 +++++++++ WEEK1BASICS.ipynb | 484 +++++++++++++++ WEEK1CLASSES10.ipynb | 382 ++++++++++++ WEEK1DICTIONARY7.ipynb | 575 +++++++++++++++++ WEEK1FUNCTIONS9.ipynb | 695 +++++++++++++++++++++ WEEK1NO6TUPLES.ipynb | 253 ++++++++ WEEK1NOI6.ipynb | 1329 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 4723 insertions(+) create mode 100644 Untitled3.ipynb create mode 100644 Untitled4.ipynb create mode 100644 Untitled5.ipynb create mode 100644 WEEK1BASICS.ipynb create mode 100644 WEEK1CLASSES10.ipynb create mode 100644 WEEK1DICTIONARY7.ipynb create mode 100644 WEEK1FUNCTIONS9.ipynb create mode 100644 WEEK1NO6TUPLES.ipynb create mode 100644 WEEK1NOI6.ipynb diff --git a/Untitled3.ipynb b/Untitled3.ipynb new file mode 100644 index 0000000..d31a655 --- /dev/null +++ b/Untitled3.ipynb @@ -0,0 +1,416 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled3.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/Untitled3.ipynb)" + ] + }, + { + "metadata": { + "id": "FcQsudSxOuvt", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7f163ba3-082a-4f5e-d4e8-dd9ab54f086f" + }, + "cell_type": "code", + "source": [ + "# if..else\n", + "v1 = 5\n", + "if v1 == 5:\n", + " print (v1)\n", + "else:\n", + " print (\"v1 is not 5\")" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "rwo0T-SiP2vz", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7f4ce049-c152-4ec3-c30e-4c5234194127" + }, + "cell_type": "code", + "source": [ + "# if..elif..else\n", + "s1 = \"Jennifer\"\n", + "s2 = \"loves\"\n", + "s3 = \"Python\"\n", + "if s1 == \"Python\":\n", + " print (\"s1 is Python\")\n", + "elif s2 == \"Jennifer\":\n", + " print (\"s2 is Jennifer\")\n", + "elif s1 == \"loves\":\n", + " print (\"s1 is loves\")\n", + "else:\n", + " print (\"Jennifer loves Python!\")" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer loves Python!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hBAvolikP96c", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "076c79ca-45c3-497d-84ee-ad534e7600b7" + }, + "cell_type": "code", + "source": [ + "# One liner\n", + "v1 = 5\n", + "x = 10 if v1 == 5 else 13\n", + "print (x)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "K4vF9PkqQBCi", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 265 + }, + "outputId": "90a77271-9ded-43cf-e072-43b0457f6bc5" + }, + "cell_type": "code", + "source": [ + "# Let's see the conditionals available\n", + "v1 = \"Jennifer\"\n", + "v2 = \"Python\"\n", + "v3 = 45\n", + "v4 = 67\n", + "v5 = 45\n", + "\n", + "# Test for equality\n", + "print (v1 == v2)\n", + "\n", + "# Test for greater than and greater than equal\n", + "print (v4 > v3)\n", + "print (v5 >= v2)\n", + "\n", + "# Test for lesser than and lesser than equal\n", + "print (v4 < v3)\n", + "print (v5 <= v2)\n", + "\n", + "# Inequality\n", + "print (v1 != v2)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;31m# Test for greater than and greater than equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv4\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mv3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv5\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mv2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# Test for lesser than and lesser than equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: '>=' not supported between instances of 'int' and 'str'" + ] + } + ] + }, + { + "metadata": { + "id": "WJIAMUEqQU3G", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "49452707-4d35-41a8-aa17-1f0bbb415d63" + }, + "cell_type": "code", + "source": [ + "# Note:\n", + "v1 = 45\n", + "v2 = \"45\"\n", + "print (v1 == v2) # False\n", + "print (str(v1) == v2) # True" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "g2j5yT55QaAi", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "eba2e16b-faf0-4fba-8022-7565e3484bd6" + }, + "cell_type": "code", + "source": [ + "# Ignore case when comparing two strings\n", + "s1 = \"Jennifer\"\n", + "s2 = \"jennifer\"\n", + "\n", + "print (s1 == s2) # False\n", + "print (s1.lower() == s2.lower()) # True\n", + "# OR\n", + "print (s1.upper() == s2.upper()) # True" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qz4GsBUbQdwV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "a3def0c0-6171-44a5-e86c-9c6a4b8e53f3" + }, + "cell_type": "code", + "source": [ + "# Checking multiple conditions 'and' and 'or'\n", + "v1 = \"Jennifer\"\n", + "v2 = \"Python\"\n", + "\n", + "# 'and' -> evaluates true when both conditions are True\n", + "print (v1 == \"Jennifer\" and v2 == \"Python\")\n", + "# 'or' -> evaluates true when any one condition is True\n", + "print (v1 == \"Python\" or v2 == \"Python\")" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "ZcU8N9F3QhJJ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1463cfcb-0209-473a-edf6-4dae54c48da3" + }, + "cell_type": "code", + "source": [ + "s1 = \"Jennifer\"\n", + "s2 = \"Python\"\n", + "\n", + "print (s1 > s2) # True -> since 'Jennifer' comes lexographically before 'Python'" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Z5oha0OiQn16", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "8c478dbf-b6da-4afc-a361-c6e990e36929" + }, + "cell_type": "code", + "source": [ + "# Check whether a value is in a list -> 'in'\n", + "l1 = [23, 45, 67, \"Jennifer\", \"Python\", 'A']\n", + "\n", + "print (23 in l1)\n", + "print ('A' in l1)\n", + "print (\"Python\" in l1)\n", + "print (32 in l1)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n", + "False\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "-lZ8zl0UQpwg", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "3c9447f7-2241-4f4e-8f3b-19729d77ad9d" + }, + "cell_type": "code", + "source": [ + "# Putting it together\n", + "l1 = [23, 1, 'A', \"Jennifer\", 9.34]\n", + "\n", + "# This is True, so the other statements are not checked\n", + "if 23 in l1 and 'B' not in l1: # Note: use of 'not'\n", + " print (\"1\")\n", + "elif 23 >= l1[0]: # True\n", + " print (\"2\")\n", + "elif 2.45 < l1[-1]: # True\n", + " print (\"3\")" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BWhED1oAQuS2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "8777d93b-0f98-4878-e352-641af626c611" + }, + "cell_type": "code", + "source": [ + "# Checking if list is empty\n", + "l1 = []\n", + "l2 = [\"Jennifer\"]\n", + "\n", + "if l1:\n", + " print (1)\n", + "elif l2:\n", + " print (2)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xRzBTlOjQxKR", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/Untitled4.ipynb b/Untitled4.ipynb new file mode 100644 index 0000000..fb967de --- /dev/null +++ b/Untitled4.ipynb @@ -0,0 +1,291 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled4.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/Untitled4.ipynb)" + ] + }, + { + "metadata": { + "id": "OL6c8amtTUaD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "4d45a09c-01b6-4b3b-90c4-e884cc97ac44" + }, + "cell_type": "code", + "source": [ + "# Simple while\n", + "# Loop runs till the condition is True\n", + "v1 = 5\n", + "while v1 <= 10:\n", + " print (v1)\n", + " v1 += 1" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qLs4URYsTdRs", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Infinite Loops\n", + "while 1:\n", + " print (1)\n", + " \n", + "while True:\n", + " print (1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "9dbJ6GU-TgVm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2098c86e-161e-4f84-e794-16a0ec0e6b1c" + }, + "cell_type": "code", + "source": [ + "# One Liner while\n", + "v1 = 0\n", + "while v1 <= 40: v1 += 1\n", + "print (v1)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "41\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "nRh8cLVTTqcO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ac619988-f986-4d72-e401-30aa56bb2cd2" + }, + "cell_type": "code", + "source": [ + "v1 = 2\n", + "++v1\n", + "++v1\n", + "print (v1)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "oLznfARXTvKn", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Terminate loop on a certain user input\n", + "# Note: The loop will break only when the user inputs 100\n", + "v1 = 1\n", + "while v1 != 100:\n", + " v1 = int(input(\"Enter new v1: \"))\n", + " print (\"v1 modified to: \" + str(v1))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "9SBSiHiGT3ia", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# 'break' -> breaks out of loop, doesn't execute any statement after it\n", + "while 1:\n", + " v1 = int(input())\n", + " if v1 == 100: \n", + " break;\n", + " print (v1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "pF7mmykoT9he", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# 'continue' -> continues to next iteration, skips all statements after it for that iteration\n", + "# Note: When 'v1' < 100 the last print statement is skipped and the control moves to the next iteration\n", + "while 1:\n", + " print (\"Iteration begins\")\n", + " v1 = int(input())\n", + " if v1 == 100:\n", + " break;\n", + " elif v1 < 100:\n", + " print (\"v1 less than 100\")\n", + " continue;\n", + " print (\"Iteration complete\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "oWWDTaY9UHNv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "f9853ef4-0d82-4127-b415-d2eddd09e9f9" + }, + "cell_type": "code", + "source": [ + "# while with lists\n", + "l1 = [\"Jennifer\", 12, \"Python\", 'A', 56, 'B', 2.12, \"Scarlett\"]\n", + "l2 = []\n", + "i = 0\n", + "while i < len(l1):\n", + " if type(l1[i]) == int:\n", + " l2.append(l1[i])\n", + " i += 1\n", + "print (l2)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 56]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Qxh7TMd6UKEA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "74503dc7-4707-4f3e-ef60-210abb272d16" + }, + "cell_type": "code", + "source": [ + "# Removing all instances of a specific value in list\n", + "l1 = ['A', 'B', 'C', 'D', 'A', 'E', 'Q', 'A', 'Z', 'A', 'Q', 'D', 'A']\n", + "while 'A' in l1: l1.remove('A')\n", + "print (l1)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['B', 'C', 'D', 'E', 'Q', 'Z', 'Q', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4gv8cxRlUOPF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 90 + }, + "outputId": "0be2db46-ca04-4698-c835-d42574d6e07b" + }, + "cell_type": "code", + "source": [ + "# Filing a dictionary\n", + "d1 = {}\n", + "while 1:\n", + " key = input(\"Enter a key: \")\n", + " value = input(\"Enter a value: \")\n", + " d1[key] = value;\n", + " if input(\"exit? \") == \"yes\": break;\n", + "print (d1)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a key: ASU\n", + "Enter a value: 250\n", + "exit? 25\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/Untitled5.ipynb b/Untitled5.ipynb new file mode 100644 index 0000000..a669390 --- /dev/null +++ b/Untitled5.ipynb @@ -0,0 +1,298 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled5.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/Untitled5.ipynb)" + ] + }, + { + "metadata": { + "id": "3Y0DTuD0V_hg", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Import the string module to get all the in-built helper methods for string\n", + "import string" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "440EE-LdWFfz", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "c39aec50-6bdf-497c-ab7c-132b28e65356" + }, + "cell_type": "code", + "source": [ + "# Case change of string variables\n", + "name = \"jennifEr loves Python\"\n", + "\n", + "# Title case\n", + "name_t = name.title() \n", + "print (name_t)\n", + "\n", + "# Upper case\n", + "name_t = name.upper() \n", + "print (name_t)\n", + "\n", + "# Lower case\n", + "name_t = name.lower() \n", + "print (name_t)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer Loves Python\n", + "JENNIFER LOVES PYTHON\n", + "jennifer loves python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "druqRLXEWHlP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "24b83eac-970f-486b-811b-8cc8788afebd" + }, + "cell_type": "code", + "source": [ + "# String Concatenation\n", + "fname = \"jennifer\"\n", + "lname = \"python\"\n", + "flname = fname + \" \" + lname\n", + "\n", + "print (fname + \" \" + lname)\n", + "print (\"Jennifer\" + \" \" + \"Python\")\n", + "# OR equivalently\n", + "print (flname.title())" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "jennifer python\n", + "Jennifer Python\n", + "Jennifer Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "p16MKRFJWbZA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "ea1b279f-5d07-4d44-f4ac-ea84351216cd" + }, + "cell_type": "code", + "source": [ + "# Adding WhiteSpaces\n", + "print (\"Jen\\nloves\\npython\")\n", + "print (\"Jen\\tloves\\tpython\")\n", + "print (\"Jen\\tloves\\npython\")" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jen\n", + "loves\n", + "python\n", + "Jen\tloves\tpython\n", + "Jen\tloves\n", + "python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "I752ZTRoWd-Q", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + }, + "outputId": "c46b272c-16ab-4b1b-e664-c9e8d100af59" + }, + "cell_type": "code", + "source": [ + "# Stripping Whitespace\n", + "name1 = \" Jennifer\"\n", + "name2 = \"Jennifer \"\n", + "name3 = \" Jennifer \"\n", + "\n", + "print (name1)\n", + "print (name2)\n", + "print (name3)\n", + "print (\"----------\")\n", + "print (name1.lstrip()) # lstrip() takes all extra whitespaces from left\n", + "print (name2.rstrip()) # rstrip() takes all extra whitespaces from right\n", + "print (name3.strip()) # strip() takes all extra whitespaces from both left and right" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + " Jennifer\n", + "Jennifer \n", + " Jennifer \n", + "----------\n", + "Jennifer\n", + "Jennifer\n", + "Jennifer\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "HPgA0SW2WhEx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ed0f3f69-3d12-43c7-ca0a-761cabc669bc" + }, + "cell_type": "code", + "source": [ + "# str() -> casts other data types to string\n", + "jensage = 21\n", + "# print (\"Jennifer's age is \" + jensage) ## This is an error as one cannot concatenate string and integer\n", + "print (\"Jennifer's age is \" + str(jensage)) # This works!" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer's age is 21\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "XAzLWUr-WlNq", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "85f622be-a3b8-4e6e-d0c3-7f9a1189da30" + }, + "cell_type": "code", + "source": [ + "# Lists, Dictionaries, Tuples and other objects can be casted to String\n", + "l_langs = [\"Python\", \"R\", \"Julia\"] # List\n", + "t_langs = (\"Python\", \"R\", \"Julia\") # Tuple\n", + "d_langs = {1: \"Python\", 2: \"R\", 3: \"Julia\"} # Dictionary\n", + "\n", + "print (\"This is a list: \" + str(l_langs))\n", + "print (\"This is a tuple: \" + str(t_langs))\n", + "print (\"This is a dictionary: \" + str(d_langs))" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "This is a list: ['Python', 'R', 'Julia']\n", + "This is a tuple: ('Python', 'R', 'Julia')\n", + "This is a dictionary: {1: 'Python', 2: 'R', 3: 'Julia'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "s1vP3tKTWr09", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 204 + }, + "outputId": "c4ea5cb0-4a43-4035-9822-5c3babfb8a81" + }, + "cell_type": "code", + "source": [ + "# Some helpful constants built-in the string module\n", + "print (\"All Letters: \" + string.ascii_letters)\n", + "print (\"Lowecase: \" + string.ascii_lowercase)\n", + "print (\"Uppercase: \" + string.ascii_uppercase)\n", + "print (\"Punctuations: \" + string.punctuation)\n", + "print (\"Numbers: \" + string.digits)\n", + "print (\"Hex Digits: \" + string.hexdigits)\n", + "print (\"Oct Digits: \" + string.octdigits)\n", + "print (\"Whitespace: \" + string.whitespace)\n", + "print (\"Printable: \" + string.printable)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "All Letters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n", + "Lowecase: abcdefghijklmnopqrstuvwxyz\n", + "Uppercase: ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", + "Punctuations: !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\n", + "Numbers: 0123456789\n", + "Hex Digits: 0123456789abcdefABCDEF\n", + "Oct Digits: 01234567\n", + "Whitespace: \t\n", + "\r\u000b\f\n", + "Printable: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n", + "\r\u000b\f\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/WEEK1BASICS.ipynb b/WEEK1BASICS.ipynb new file mode 100644 index 0000000..a4ac24d --- /dev/null +++ b/WEEK1BASICS.ipynb @@ -0,0 +1,484 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled2.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/ASIFWEEK1BASICS.ipynb)" + ] + }, + { + "metadata": { + "id": "RMYqjM3fK42C", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 374 + }, + "outputId": "63f95807-266d-4ac6-917a-ddafbfd11962" + }, + "cell_type": "code", + "source": [ + "# The Zen of Python\n", + "import this" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "The Zen of Python, by Tim Peters\n", + "\n", + "Beautiful is better than ugly.\n", + "Explicit is better than implicit.\n", + "Simple is better than complex.\n", + "Complex is better than complicated.\n", + "Flat is better than nested.\n", + "Sparse is better than dense.\n", + "Readability counts.\n", + "Special cases aren't special enough to break the rules.\n", + "Although practicality beats purity.\n", + "Errors should never pass silently.\n", + "Unless explicitly silenced.\n", + "In the face of ambiguity, refuse the temptation to guess.\n", + "There should be one-- and preferably only one --obvious way to do it.\n", + "Although that way may not be obvious at first unless you're Dutch.\n", + "Now is better than never.\n", + "Although never is often better than *right* now.\n", + "If the implementation is hard to explain, it's a bad idea.\n", + "If the implementation is easy to explain, it may be a good idea.\n", + "Namespaces are one honking great idea -- let's do more of those!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "0De_ykwDK9Vv", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "0c9d3b7d-59ea-4992-b812-fe393f62f3d6" + }, + "cell_type": "code", + "source": [ + "# Comments\n", + "# This is a python tutorial and a single line comment\n", + "''' This is a multiline comment\n", + " pretty awesome!!\n", + " Let me introduce you to jennifer!'''" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "' This is a multiline comment\\n pretty awesome!!\\n Let me introduce you to jennifer!'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 2 + } + ] + }, + { + "metadata": { + "id": "aN-Tp9iAK_8C", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Simple imports\n", + "import math\n", + "import random" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "ipzew6nNLC4O", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# importing specific functions from modules\n", + "# imports just the factorial function from math\n", + "from math import factorial\n", + "\n", + "# imports all the functions from math\n", + "from math import *" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "F0Ih7uLdLFHW", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Giving aliases\n", + "# The Module name is alaised\n", + "import math as m\n", + "\n", + "# The function name is alaised\n", + "from math import factorial as fact" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "MCzEeRLZLIhQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "bf077b54-3c8e-448f-fcca-5bbbd02b4a75" + }, + "cell_type": "code", + "source": [ + "# Calling imported functions\n", + "# If you import the module you have to call the functions from the module\n", + "import math\n", + "print (math.factorial(12))\n", + "\n", + "# If you import the functions you can call the function as if it is in your program\n", + "from random import randrange as rg\n", + "print (rg(23, 1000))" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "479001600\n", + "860\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "senls-o8LLxP", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Variables\n", + "msg = \"Python!\" # String\n", + "v2 = 'Python!' # Also String works same\n", + "v1 = 2 # Numbers\n", + "v3 = 3.564 # Floats / Doubles\n", + "v4 = True # Boolean (True / False)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "Xt60hODtLOzG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "cc5bc681-9d5b-4bf5-e9f7-3e65146cdcab" + }, + "cell_type": "code", + "source": [ + "# print() \n", + "# automatically adds a newline\n", + "print (msg)\n", + "print (v2)\n", + "print (v1)\n", + "print (v3)\n", + "print (v4)\n", + "print (\"Hello Python!\")" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Python!\n", + "Python!\n", + "2\n", + "3.564\n", + "True\n", + "Hello Python!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QledXmpwLRQp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "da2d6b74-c94c-40fb-ee0b-37fefa7c4b25" + }, + "cell_type": "code", + "source": [ + "# Note: Both \" and ' can be used to make strings. And this flexibility allows for the following:\n", + "msg2 = 'Jennifer said, \"I love Python!\"'\n", + "msg3 = \"After that Jennifer's Python Interpreter said it back to her!\"\n", + "msg4 = 'Of Course she used the command `print(\"I love Jennifer\")`'\n", + "\n", + "print (msg2)\n", + "print (msg3)\n", + "print (msg4)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer said, \"I love Python!\"\n", + "After that Jennifer's Python Interpreter said it back to her!\n", + "Of Course she used the command `print(\"I love Jennifer\")`\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "sRS9zreyLZv5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "c0d368b7-17f9-45bc-b95b-68599561c38e" + }, + "cell_type": "code", + "source": [ + "# input()\n", + "msg = input()" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "124567\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hE0_F3dZLfnw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e33f5781-3289-4bb1-8390-dda5ec117b9f" + }, + "cell_type": "code", + "source": [ + "# input() with message\n", + "msg = input (\"Provide some input: \")\n", + "print (msg)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Provide some input: 5248\n", + "5248\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "nqMe_kZJLiBL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "ad9fec0d-8e27-4324-b864-9dfc4c1f17c0" + }, + "cell_type": "code", + "source": [ + "# Check for specific input without storing it\n", + "if input(\"Enter something: \") == \"something\":\n", + " print (\"Something something\")\n", + "else: print (\"Not Something\")" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter something: 2548\n", + "Not Something\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "lLjsTa2nL6p6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "4ae5d653-f264-4a4e-81c3-bde95a4a8bc9" + }, + "cell_type": "code", + "source": [ + "# Python takes every input as a string\n", + "# So, if required you can convert to the required type\n", + "msg = input(\"Enter a number: \")\n", + "print (type(msg))\n", + "\n", + "msg = int(input (\"Enter a number again, if not a number this will throw an error: \"))\n", + "print (type(msg))" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number: 258852\n", + "\n", + "Enter a number again, if not a number this will throw an error: 258\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "XFf3Z6MZL_Pe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 340 + }, + "outputId": "724f267c-2e91-49b8-d98d-03bd70a26e40" + }, + "cell_type": "code", + "source": [ + "# Basic Arithmetic operations\n", + "# Add\n", + "print (3 + 2)\n", + "print (3.4565 + 56.232)\n", + "print ('------------')\n", + "\n", + "# Subtract\n", + "print (3 - 4)\n", + "print (34.56 - 3.78)\n", + "print ('------------')\n", + "\n", + "# Multiply\n", + "print (4 * 3)\n", + "print (7.56 * 34)\n", + "print ('------------')\n", + "\n", + "# Division\n", + "print (5 / 2)\n", + "print (5.0 / 2)\n", + "print (5 / 2.0)\n", + "print (25.0 / 5)\n", + "print ('------------')\n", + "\n", + "# Exponents\n", + "print (4 ** 4)\n", + "print (5.67 ** 3)\n", + "print ('------------')\n", + "\n", + "# Modulo\n", + "print (10%3)\n", + "print (10%11)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "59.6885\n", + "------------\n", + "-1\n", + "30.78\n", + "------------\n", + "12\n", + "257.03999999999996\n", + "------------\n", + "2.5\n", + "2.5\n", + "2.5\n", + "5.0\n", + "------------\n", + "256\n", + "182.28426299999998\n", + "------------\n", + "1\n", + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "MArOl2rqMAb9", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/WEEK1CLASSES10.ipynb b/WEEK1CLASSES10.ipynb new file mode 100644 index 0000000..4bf9791 --- /dev/null +++ b/WEEK1CLASSES10.ipynb @@ -0,0 +1,382 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled10.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/WEEK1CLASSES10.ipynb)" + ] + }, + { + "metadata": { + "id": "pcVaggGtho5O", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Simple class\n", + "class Programmer():\n", + " \"\"\"This is called a docstring. This class is to create a Programmer. \n", + " Functions inside a class is called a method.\n", + " Methods are automatically passed the 'self' argument. \n", + " Any variable prefixed with 'self.' is available to the class.\n", + " We will also be able to access this self prefixed variables from any instance of the class.\"\"\"\n", + " \n", + " def __init__(self, name, age, *known_languages):\n", + " \"\"\"__init__ is a special method that Python automatically calls \n", + " when a new instance of the class is created. \"\"\"\n", + " self.name = name\n", + " self.age = age\n", + " self.languages = set(known_languages)\n", + " \n", + " # Default value for a class variable\n", + " self.concepts_revised = 0\n", + " \n", + " def add_new_language(self, lang):\n", + " self.languages.add(lang)\n", + " print (str(self.name) + \" knows a new language : \" + str(lang) + \" !!\")\n", + " \n", + " def revise_concept(self, concept):\n", + " self.concepts_revised += 1\n", + " print (str(self.name) + \" just revised \" + str(concept) + \" !!\")\n", + " \n", + " def languages_known(self):\n", + " return list(self.languages)\n", + " \n", + " def cv(self):\n", + " print (\"Name : \" + str(self.name))\n", + " print (\"Age : \" + str(self.age))\n", + " print (\"Skills : \" + str(self.languages))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "krDSg0mfhwJG", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Creating an instance of the class\n", + "# This calls the __init__() method\n", + "a_programmer = Programmer(\"Jennifer\", 21, \"Python\", \"R\", \"Julia\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "YlvtJsOmh03j", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "58a4d38c-fd51-4758-930d-c7a3c5cdc4e3" + }, + "cell_type": "code", + "source": [ + "# Accessing the attributes\n", + "print (a_programmer.name.title())\n", + "print (a_programmer.age)\n", + "print (a_programmer.languages)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer\n", + "21\n", + "{'Julia', 'R', 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "h0PN4sxSh3dQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "726c38b6-76f1-4ebe-bf8e-30801d7e286e" + }, + "cell_type": "code", + "source": [ + "# Calling Methods\n", + "a_programmer.add_new_language(\"Ruby\")\n", + "print (a_programmer.languages)\n", + "\n", + "print (\"\\nCV for \" + str(a_programmer.name.title()) + \"\\n=================\")\n", + "a_programmer.cv()" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer knows a new language : Ruby !!\n", + "{'Julia', 'R', 'Ruby', 'Python'}\n", + "\n", + "CV for Jennifer\n", + "=================\n", + "Name : Jennifer\n", + "Age : 21\n", + "Skills : {'Julia', 'R', 'Ruby', 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "-MMxV19Vh8iY", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 221 + }, + "outputId": "afcc68f3-d4f1-4573-8069-3edafb963f2f" + }, + "cell_type": "code", + "source": [ + "# Creating multiple instances\n", + "b_programmer = Programmer (\"Scarlett\", 21, \"Python\", \"Julia\", \"SPLUS\", \"Ruby\")\n", + "c_programmer = Programmer (\"Ariel\", 20, \"C++\", \"Java\", \"Python\")\n", + "\n", + "print (\"\\nCV for \" + str(b_programmer.name.title()) + \"\\n=================\")\n", + "b_programmer.cv()\n", + "\n", + "print (\"\\nCV for \" + str(c_programmer.name.title()) + \"\\n=================\")\n", + "c_programmer.cv()" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "CV for Scarlett\n", + "=================\n", + "Name : Scarlett\n", + "Age : 21\n", + "Skills : {'Julia', 'Ruby', 'SPLUS', 'Python'}\n", + "\n", + "CV for Ariel\n", + "=================\n", + "Name : Ariel\n", + "Age : 20\n", + "Skills : {'C++', 'Java', 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4qhvzn5Mh8lQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "63bff6ab-1d35-4b7d-95d6-a76a192abe9a" + }, + "cell_type": "code", + "source": [ + "# Directly modifying Attribute's value\n", + "print (\"Concepts Revised by \" + str(b_programmer.name.title()) + \" : \" + str(b_programmer.concepts_revised))\n", + "b_programmer.concepts_revised += 10\n", + "print (\"Concepts Revised by \" + str(b_programmer.name.title()) + \" : \" + str(b_programmer.concepts_revised))" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Concepts Revised by Scarlett : 0\n", + "Concepts Revised by Scarlett : 10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wrAI-tjuh8p9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "697a9905-4162-40f3-d683-eab73ba454ce" + }, + "cell_type": "code", + "source": [ + "# Modifying Attribute's value through a method\n", + "print (\"\\nConcepts Revised by \" + str(c_programmer.name.title()) + \" : \" + str(c_programmer.concepts_revised))\n", + "c_programmer.revise_concept(\"Python Lists\")\n", + "c_programmer.revise_concept(\"Python Tuples\")\n", + "c_programmer.revise_concept(\"Python Dictionaries\")\n", + "print (\"Concepts Revised by \" + str(c_programmer.name.title()) + \" : \" + str(c_programmer.concepts_revised))" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "Concepts Revised by Ariel : 0\n", + "Ariel just revised Python Lists !!\n", + "Ariel just revised Python Tuples !!\n", + "Ariel just revised Python Dictionaries !!\n", + "Concepts Revised by Ariel : 3\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wFt0T6bBh8s0", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Inheritance\n", + "'''\n", + "Programmer => Parent Class\n", + "Developer => Child Class\n", + "\n", + "Child class inherits from the base class.\n", + "The classes share a IS A relationship.\n", + "So, in this case, Developer IS A Programmer.\n", + "It has available all methods and variables from the parent class.\n", + "And can define methods and variables of its own.\n", + "'''\n", + "class Developer(Programmer):\n", + " def __init__(self, name, age, expertise, yoe, *known_languages):\n", + " \n", + " # Call the parent class to initialize and give the child class an instance of the parent\n", + " super().__init__(name, age, known_languages)\n", + " self.expertise = expertise\n", + " self.years_of_experience = yoe\n", + " \n", + " def specializes_in(self):\n", + " return self.expertise\n", + " \n", + " def cv(self):\n", + " \"\"\"\n", + " This method overrides the cv() method in the parent class.\n", + " Any method in child class with same name as a method inherited from parent class\n", + " overrides the parent class method.\n", + " \"\"\"\n", + " print (\"Name : \" + str(self.name))\n", + " print (\"Age : \" + str(self.age))\n", + " print (\"Skills : \" + str(self.languages))\n", + " print (\"Expertise : \" + str(self.expertise))\n", + " print (\"Years of Experience : \" + str(self.years_of_experience))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "7Zp-alP3iNDS", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Creating an instance of the child class\n", + "a_developer = Developer (\"Jennifer\", 21, \"Android\", 2, \"Java\", \"Kotlin\", \"Python\", \"R\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "iFZ8bppoiRQX", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "69e06731-0f69-4d15-e04d-ebcd7fa8c8a8" + }, + "cell_type": "code", + "source": [ + "# Call to methods and variables from the child as well as parent class\n", + "print (str(a_developer.name) + \" specializes in \" + str(a_developer.specializes_in()) + \".\")\n", + "print (str(a_developer.name) + \" can code in \" + str(a_developer.languages_known()) + \".\")\n", + "print (str(a_developer.name) + \" has \" + str(a_developer.years_of_experience) + \" years of experience.\")" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer specializes in Android.\n", + "Jennifer can code in [('Java', 'Kotlin', 'Python', 'R')].\n", + "Jennifer has 2 years of experience.\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TbiHdZ84iRTO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + }, + "outputId": "975ead63-a30b-44b2-da29-99affe55fbba" + }, + "cell_type": "code", + "source": [ + "# Calling the overriden method in the child class\n", + "a_developer.cv()" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Name : Jennifer\n", + "Age : 21\n", + "Skills : {('Java', 'Kotlin', 'Python', 'R')}\n", + "Expertise : Android\n", + "Years of Experience : 2\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file diff --git a/WEEK1DICTIONARY7.ipynb b/WEEK1DICTIONARY7.ipynb new file mode 100644 index 0000000..922472b --- /dev/null +++ b/WEEK1DICTIONARY7.ipynb @@ -0,0 +1,575 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled8.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/WEEK1DICTIONARY7.ipynb)" + ] + }, + { + "metadata": { + "id": "cj0bS86edRJr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "1881f099-8a76-4c9e-8e5a-82ae53297a9d" + }, + "cell_type": "code", + "source": [ + "# Simple Dictionary\n", + "# Dictionary allows to have key:value pairs\n", + "d1 = {\"Jennifer\":8, 'A':65, 66:'B', 9.45:\"Decimals\"}\n", + "\n", + "print (d1[\"Jennifer\"])\n", + "print (d1['A'])\n", + "print (d1[66])\n", + "print (d1[9.45])" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "65\n", + "B\n", + "Decimals\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "3hRCfN0tdhn3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "f223f658-4f9a-4986-99ce-c33c58c581cc" + }, + "cell_type": "code", + "source": [ + "# Adding new kay:value pairs\n", + "d1 = {\"Jennifer\":8, 'A':65, 66:'B', 9.45:\"Decimals\"}\n", + "\n", + "d1[\"Scarlett\"] = 8\n", + "d1[7.56] = \"Is a decimal!\"\n", + "d1['Q'] = 17\n", + "\n", + "print (d1[\"Scarlett\"])\n", + "print (d1[7.56])\n", + "print (d1['Q'])\n", + "\n", + "print (d1)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "8\n", + "Is a decimal!\n", + "17\n", + "{'Jennifer': 8, 'A': 65, 66: 'B', 9.45: 'Decimals', 'Scarlett': 8, 7.56: 'Is a decimal!', 'Q': 17}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "PVreBgHDdkR_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "38aa4de4-a611-4a03-d763-bb5099ea2aa1" + }, + "cell_type": "code", + "source": [ + "# Declaring an empty dictionary\n", + "d1 = {}\n", + "\n", + "# Add new values\n", + "d1[\"Jennifer\"] = \"Python\"\n", + "d1[\"Scarlett\"] = \"Python\"\n", + "d1[45] = 56\n", + "\n", + "print (d1)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'Jennifer': 'Python', 'Scarlett': 'Python', 45: 56}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "aX-a4WtQdnrF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "87bb4b4f-8308-4f1a-bfd5-30aea7cdca71" + }, + "cell_type": "code", + "source": [ + "# Modifying values in a dictionary\n", + "d1 = {\"Python\":\"Is a language\", \"Jennifer\":\"Feels like a supergirl with Python\"}\n", + "\n", + "d1[\"Python\"] = \"Is Love\"\n", + "d1[\"Jennifer\"] = 8\n", + "print (d1)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'Python': 'Is Love', 'Jennifer': 8}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uD5Z_9DydsBU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ebb3d84b-5bec-416a-ded6-d22945ce1f91" + }, + "cell_type": "code", + "source": [ + "# Removing Key:Value pairs\n", + "d1 = {\"Key\":\"Value\", \"Jennifer\":\"Scarlett\", \"Scarlett\":\"Jennifer\"}\n", + "\n", + "del d1[\"Key\"]\n", + "print (d1)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'Jennifer': 'Scarlett', 'Scarlett': 'Jennifer'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "KFPTlpmedtdu", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 122 + }, + "outputId": "52d3b0dc-f7ac-44cd-dc1b-ac21df034eaf" + }, + "cell_type": "code", + "source": [ + "# Storing a dictionary inside a dictionary\n", + "d1 = {'A':65, 'B':66, 'C':67, 'D': {\n", + " \"Breaking\": \"Dict into dicts\", \n", + " \"Inception\": \"All over\", \n", + " '!': \"XD\"}, \n", + " 'E':69, \n", + " \"What happened with D?\": \"It became a 'D'ictionary! XD\",\n", + " 66:66}\n", + "\n", + "print (d1['D'][\"Inception\"])\n", + "print (d1[\"What happened with D?\"])\n", + "print ('\\n')\n", + "print (d1)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "All over\n", + "It became a 'D'ictionary! XD\n", + "\n", + "\n", + "{'A': 65, 'B': 66, 'C': 67, 'D': {'Breaking': 'Dict into dicts', 'Inception': 'All over', '!': 'XD'}, 'E': 69, 'What happened with D?': \"It became a 'D'ictionary! XD\", 66: 66}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "C7DCBYZwd3P2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 493 + }, + "outputId": "fafaee73-a6ae-4537-e872-19968686de61" + }, + "cell_type": "code", + "source": [ + "# Looping through a dictionary\n", + "for key, value in d1.items():\n", + " print(\"Key: \" + str(key))\n", + " print (\"Value: \" + str(value))\n", + " print('\\n')" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Key: A\n", + "Value: 65\n", + "\n", + "\n", + "Key: B\n", + "Value: 66\n", + "\n", + "\n", + "Key: C\n", + "Value: 67\n", + "\n", + "\n", + "Key: D\n", + "Value: {'Breaking': 'Dict into dicts', 'Inception': 'All over', '!': 'XD'}\n", + "\n", + "\n", + "Key: E\n", + "Value: 69\n", + "\n", + "\n", + "Key: What happened with D?\n", + "Value: It became a 'D'ictionary! XD\n", + "\n", + "\n", + "Key: 66\n", + "Value: 66\n", + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "IBKhTkhZd40s", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 493 + }, + "outputId": "cdfa4725-c8b7-4df0-d152-3ea3961bf75f" + }, + "cell_type": "code", + "source": [ + "# Looping through the keys in dictionary\n", + "for key in d1.keys():\n", + " print(\"Key: \" + str(key))\n", + " print (\"Value: \" + str(d1[key]))\n", + " print('\\n')" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Key: A\n", + "Value: 65\n", + "\n", + "\n", + "Key: B\n", + "Value: 66\n", + "\n", + "\n", + "Key: C\n", + "Value: 67\n", + "\n", + "\n", + "Key: D\n", + "Value: {'Breaking': 'Dict into dicts', 'Inception': 'All over', '!': 'XD'}\n", + "\n", + "\n", + "Key: E\n", + "Value: 69\n", + "\n", + "\n", + "Key: What happened with D?\n", + "Value: It became a 'D'ictionary! XD\n", + "\n", + "\n", + "Key: 66\n", + "Value: 66\n", + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "jffqGelid8PB", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + }, + "outputId": "157b5f9e-d9e8-4587-f49b-507519482019" + }, + "cell_type": "code", + "source": [ + "# Note: Default behaviour is to loop through keys if not specified d1.keys()\n", + "for k in d1:\n", + " print(\"Key: \" + str(k))" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Key: A\n", + "Key: B\n", + "Key: C\n", + "Key: D\n", + "Key: E\n", + "Key: What happened with D?\n", + "Key: 66\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "2Al1cSKceF29", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1ac92c2e-a53c-40b4-d4f2-cc47af269c1a" + }, + "cell_type": "code", + "source": [ + "# Check if a particular key is present\n", + "if 'F' not in d1.keys():\n", + " print (\"What happened to F?\")" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "What happened to F?\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qFhEM03eeJ38", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Looping through dictonary keys in sorted order (increasing)\n", + "for key in sorted(d1.keys()):\n", + " print (\"Key :\" + key + '\\t' + \"Value :\" + str(d1[key]) + '\\n')" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "xwpYuzl2eJ5m", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Looping through dictonary keys in sorted order (decreasing)\n", + "for key in sorted(d1.keys(), reverse=True):\n", + " print (\"Key :\" + key + '\\t' + \"Value :\" + str(d1[key]) + '\\n')" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "ynlWZvpJeJ7r", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + }, + "outputId": "353a6a04-f1ed-47bd-8494-6d56618d8318" + }, + "cell_type": "code", + "source": [ + "# Looping through values in dictionary (with repeats)\n", + "# Note: If two or more keys have the same value then this method will print all of them\n", + "for value in d1.values():\n", + " print (str(value).title())" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "65\n", + "66\n", + "67\n", + "{'Breaking': 'Dict Into Dicts', 'Inception': 'All Over', '!': 'Xd'}\n", + "69\n", + "It Became A 'D'Ictionary! Xd\n", + "66\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "fUgsQOGbeJ9M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "daae4462-8969-40cf-b82d-40e4d064046a" + }, + "cell_type": "code", + "source": [ + "# List in Dictionary\n", + "d1 = {\"l1\":['A', 'B', 'C', 'D'],\n", + " \"l2\":['E', 'F', 'G', 'H'],\n", + " 45 : \"qwerty\",\n", + " '$' : \"$Dollar$\"}\n", + "\n", + "# Accessing the elements in list\n", + "print (d1[\"l1\"][2])\n", + "print (d1[\"l2\"][-1])" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "C\n", + "H\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hq7r6RcteJ_r", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 255 + }, + "outputId": "696d92bd-e6dc-48ef-b359-f3b188764f46" + }, + "cell_type": "code", + "source": [ + "# Looping over just the lists in dictionary\n", + "for k in d1.keys():\n", + " if type(d1[k]) == list:\n", + " print (\"List :\" + k)\n", + " for val in d1[k]:\n", + " print (val)\n", + " print ('\\n')" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "List :l1\n", + "A\n", + "B\n", + "C\n", + "D\n", + "\n", + "\n", + "List :l2\n", + "E\n", + "F\n", + "G\n", + "H\n", + "\n", + "\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "5wckDWa3eKCl", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "c3HiJOMueKIE", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/WEEK1FUNCTIONS9.ipynb b/WEEK1FUNCTIONS9.ipynb new file mode 100644 index 0000000..8e3959a --- /dev/null +++ b/WEEK1FUNCTIONS9.ipynb @@ -0,0 +1,695 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled9.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/WEEK1FUNCTIONS9.ipynb)" + ] + }, + { + "metadata": { + "id": "PsG8_4x5fXqZ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "6517bda3-b217-4d2a-ee43-15585eb5a468" + }, + "cell_type": "code", + "source": [ + "# Simple function\n", + "# Function definition\n", + "def a_func():\n", + " print (\"A Message from the other world!\")\n", + "\n", + "# Function Call\n", + "a_func()\n", + "a_func()" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "A Message from the other world!\n", + "A Message from the other world!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "r1er6P3_feRY", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "da475327-f82d-4d3e-c0b3-d8cf3438baf7" + }, + "cell_type": "code", + "source": [ + "# Passing arguments to functions\n", + "def add_two(num):\n", + " print (int(num)+2)\n", + "\n", + "add_two (3)\n", + "add_two (\"45\") # This will work as we are casting the passed parameter to Integer before adding" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "47\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EeJhJqzOfflW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "3b8562d1-d172-4bc2-d12c-8fcf454d67a9" + }, + "cell_type": "code", + "source": [ + "# Multiple arguments\n", + "def add_sub(add_two_to_this, sub_two_from_this):\n", + " print (\"Added 2 to : \" + str(add_two_to_this))\n", + " print (\"Answer: \" + str(int(add_two_to_this)+2))\n", + " \n", + " print (\"Subtracted 3 from : \" + str(sub_two_from_this))\n", + " print (\"Answer: \" + str(int(sub_two_from_this)-2))\n", + " \n", + "add_sub (45, \"67\")\n", + "add_sub (\"-156745\", 12131)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Added 2 to : 45\n", + "Answer: 47\n", + "Subtracted 3 from : 67\n", + "Answer: 65\n", + "Added 2 to : -156745\n", + "Answer: -156743\n", + "Subtracted 3 from : 12131\n", + "Answer: 12129\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "reYwCmpcffnW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "3df0c5c8-4317-4f11-a89d-4536513106bb" + }, + "cell_type": "code", + "source": [ + "# Ordering of passed argument matters\n", + "def coding_in(name, language):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(\"Jennifer\", \"Python\")\n", + "# If you change the order, results are unexpected\n", + "coding_in(\"Python\", \"Jennifer\")" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Python is coding in Jennifer\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Li3zeTSZffqO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2a650cb2-274b-4118-da16-5913c8232b46" + }, + "cell_type": "code", + "source": [ + "# Keyword Arguments\n", + "# Pass the argumnets as key:value pairs, so even if unordered it won't produce unexpected results\n", + "def coding_in(name, language):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(name=\"Jennifer\", language=\"Python\")\n", + "# If you change the order, results are same\n", + "coding_in(language=\"Python\", name=\"Jennifer\")" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Jennifer is coding in Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wDu_rmBSffuC", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "be330909-312b-496b-98b4-52a1a5ffd31b" + }, + "cell_type": "code", + "source": [ + "# Default Values for parameters\n", + "# Note: If you do not pass arguments required by the function and that argument does not have a default value, \n", + "# then python will throw an error\n", + "def coding_in(name, language=\"Python\"):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(\"Jennifer\") # Since 2nd argument is not passed, it takes on the default parameter given\n", + "coding_in(\"Jennifer\", \"R\") # Since 2nd argument is passed, it takes on the passed arguemnt" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Jennifer is coding in R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "x1nU7KTGffvZ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + }, + "outputId": "47b7b335-661e-433b-a18e-2445b468960d" + }, + "cell_type": "code", + "source": [ + "# Note: You cannot have parameters with no default value after parameters having default value\n", + "# Following is an error\n", + "def coding_in(name, language1=\"Python\", language2):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in(\"Jennifer\", \"Python\", \"R\")" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def coding_in(name, language1=\"Python\", language2):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ] + }, + { + "metadata": { + "id": "3u1xMiO9ffy7", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "9deb0598-ebbd-48d4-c842-4b9022d92942" + }, + "cell_type": "code", + "source": [ + "# Easy fix to above error is to declare all non-default parameters, \n", + "# and then start declaring the default parameters\n", + "# Note: Default parameters can be used to make an argument optional\n", + "def coding_in(name, language2, language1=\"Python\"):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in(\"Jennifer\", \"Python\", \"R\")\n", + "coding_in(\"Jennifer\", \"R\")" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in R and Python\n", + "Jennifer is coding in Python and R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "UFMKAa9Ef-vL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "0c494cf0-643c-4710-cd98-28141f1f8977" + }, + "cell_type": "code", + "source": [ + "# All parameters can be default\n", + "def coding_in(name=\"Jennifer\", language1=\"Python\", language2=\"R\"):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in()" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python and R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "2yhLoHmXf-3R", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "7e1ea667-377e-4318-f69f-ee9f83211566" + }, + "cell_type": "code", + "source": [ + "# return\n", + "def pow_4(num):\n", + " return num**4\n", + "\n", + "v1 = pow_4(34) # v1 now stores 34^4\n", + "v2 = pow_4(23) # v2 now stores 23^4\n", + "\n", + "print (\"34^4 = \" + str(v1))\n", + "print (\"23^4 = \" + str(v2))" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34^4 = 1336336\n", + "23^4 = 279841\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qlF9xfi7gE0g", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "9fd0aad5-b777-4406-b4ce-4c070d8e127f" + }, + "cell_type": "code", + "source": [ + "# can return any data type or object from function\n", + "def make_a_coder(name, age, language):\n", + " d1 = {'name': name, \n", + " 'age' : age,\n", + " 'language' : language}\n", + " return d1\n", + "\n", + "print(make_a_coder(\"Jennifer\", \"21\", \"Python\"))" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'name': 'Jennifer', 'age': '21', 'language': 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "R0JfIzTSgItd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "67a814e3-7aa0-4796-bfb7-476d18a60683" + }, + "cell_type": "code", + "source": [ + "# can pass any data type or object to function\n", + "def make_many_coders(list_of_coders):\n", + " print (\"Name \\tLanguage \\tAge\")\n", + " print (\"========\\t==========\\t===\")\n", + " for coder,details in list_of_coders.items():\n", + " print (str(coder) + \"\\t\" + str(details[0]) + \"\\t\\t\" + str(details[-1]))\n", + " \n", + " return str(len(list_of_coders))\n", + "\n", + "d1 = {\"Jennifer\": [\"Python\", 21],\n", + " \"Scarlett\": [\"R\", 21]}\n", + "print ('\\n' + make_many_coders(d1) + \" coders found!\")" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Name \tLanguage \tAge\n", + "========\t==========\t===\n", + "Jennifer\tPython\t\t21\n", + "Scarlett\tR\t\t21\n", + "\n", + "2 coders found!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "7yQHS3zXgNg2", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "10ab34e7-1caf-45b0-b87f-d1d106727112" + }, + "cell_type": "code", + "source": [ + "# If a list passed to a function is changed inside the function then the change is permanent\n", + "def make_language_list(language_list, new_language):\n", + " language_list.append(new_language)\n", + " \n", + "lang_list = [\"Python\", \"R\"]\n", + "print (lang_list)\n", + "\n", + "make_language_list(lang_list, \"Julia\")\n", + "print (lang_list)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Python', 'R']\n", + "['Python', 'R', 'Julia']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "kBGFm0CdgNms", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "fb31e07d-308c-4be3-f8a3-53a45e5b3b19" + }, + "cell_type": "code", + "source": [ + "# Preventing a function from modifying a list\n", + "def make_language_list(language_list, new_language):\n", + " language_list.append(new_language)\n", + " \n", + "lang_list = [\"Python\", \"R\"]\n", + "print (lang_list)\n", + "\n", + "make_language_list(lang_list[:], \"Julia\")\n", + "print (lang_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Python', 'R']\n", + "['Python', 'R']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "a9Qxt6WpgUr0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "8797e76b-6dbc-48c3-d7c4-38ce2a2bba57" + }, + "cell_type": "code", + "source": [ + "# Passing Arbitrary number of arguments\n", + "# The '*' tells Python to make a tuple of whatever number of arguments it receives at that position\n", + "def languages(*many_languages): \n", + " print (many_languages)\n", + " \n", + "languages (\"Python\")\n", + "languages (\"Python\", \"R\", \"Julia\", \"Ruby\", \"Go\")" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "('Python',)\n", + "('Python', 'R', 'Julia', 'Ruby', 'Go')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Q-v80_n9gXxc", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "dce12d57-80b4-4a29-c389-183440574110" + }, + "cell_type": "code", + "source": [ + "# Passing Arbitrary number of arguments with a normal argument\n", + "# Note: The parameter that accepts arbitrary number of arguments needs to be placed at last\n", + "def knows_languages(name, *languages):\n", + " print (str(name) + \" knows: \" + str(languages))\n", + " \n", + "knows_languages(\"Jennifer\", \"Python\")\n", + "knows_languages(\"Jennifer\", \"Python\", \"R\", \"Julia\", \"Ruby\")" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer knows: ('Python',)\n", + "Jennifer knows: ('Python', 'R', 'Julia', 'Ruby')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TfZx8ddEgnf6", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "" + ] + }, + { + "metadata": { + "id": "rjilRiZ1goOj", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 129 + }, + "outputId": "6891f159-0d08-4c11-ec13-7fa9aaa617ac" + }, + "cell_type": "code", + "source": [ + "# Note: You cannot have two or more parameters that accepts arbitrary number of arguments\n", + "# Hence, following is an error!\n", + "def knows_languages_and_modules(name, *languages, *modules):\n", + " print (str(name) + \" knows: \" + str(languages))\n", + " \n", + "knows_languages(\"Jennifer\", \"Python\", \"PyGtk\")\n", + "knows_languages(\"Jennifer\", \"Python\", \"R\", \"Julia\", \"Ruby\", \"PyGtk\", \"PyGame\", \"audioop\")" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def knows_languages_and_modules(name, *languages, *modules):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ] + }, + { + "metadata": { + "id": "Aea_MwYHgstl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "d8cb2703-2f41-433c-faf7-b5737b99cd55" + }, + "cell_type": "code", + "source": [ + "# Using Arbitrary Keyword Argument\n", + "# Note: '**' tells Python to create a dictionary of the extra arguments passed after the first required positional argument\n", + "def make_a_coder(name, **details):\n", + " coder = {}\n", + " coder[\"name\"] = name;\n", + " for key, value in details.items():\n", + " coder[key] = value\n", + " return coder\n", + "\n", + "print (make_a_coder(\"Jennifer\", location=\"California\", age=\"21\", language=(\"Python\", \"R\")))\n", + "\n", + "# We can do this since we are using keyword arguments\n", + "print (make_a_coder(location=\"California\", age=\"21\", language=(\"Python\", \"R\"), name=\"Jennifer\"))" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'name': 'Jennifer', 'location': 'California', 'age': '21', 'language': ('Python', 'R')}\n", + "{'name': 'Jennifer', 'location': 'California', 'age': '21', 'language': ('Python', 'R')}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "quiyIR_lgswC", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "khEpYdW8gsx8", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "hrrHF4tBgs1C", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "16o3dUVdgssW", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/WEEK1NO6TUPLES.ipynb b/WEEK1NO6TUPLES.ipynb new file mode 100644 index 0000000..a0b5460 --- /dev/null +++ b/WEEK1NO6TUPLES.ipynb @@ -0,0 +1,253 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled7.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/WEEK1NO6TUPLES.ipynb)" + ] + }, + { + "metadata": { + "id": "raWpQTC-bAaU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "c60b7e6c-ecd0-432b-f0b2-6624919e7b98" + }, + "cell_type": "code", + "source": [ + "# Simple Tuples\n", + "# Tuples are list, the elements stored in which cannot be changed.\n", + "t1 = (23, 45)\n", + "print (t1)\n", + "print (t1[1])" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(23, 45)\n", + "45\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Jquc-jVGbfpe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "88528c3a-5b8b-4b7c-e875-365022e15199" + }, + "cell_type": "code", + "source": [ + "# Can have more than two elements\n", + "t1 = (1, 2, 3, 4, 5, 6, 7)\n", + "print (t1)\n", + "print (t1[4])" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5, 6, 7)\n", + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "pF0g-AxybkEW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e4d9d5b0-a250-478b-82e6-0f1e3931b753" + }, + "cell_type": "code", + "source": [ + "# Cannot change the elements stored as compared to lists\n", + "l1 = [1, 2, 3, 4, 5, 6, 7]\n", + "t1 = (1, 2, 3, 4, 5, 6, 7)\n", + "\n", + "# Can change elements in list\n", + "l1[4] = 1\n", + "# Cannot change elements in tuple (Uncomment it to see the error)\n", + "#t1[4] = 1\n", + "\n", + "print (l1)\n", + "print (t1)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 1, 6, 7]\n", + "(1, 2, 3, 4, 5, 6, 7)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "lqjdIrWxbmFH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + }, + "outputId": "47d55168-6af4-4792-a307-12fba0c0a2c3" + }, + "cell_type": "code", + "source": [ + "# Looping over a tuple\n", + "t1 = (34, 12, 56, 78, 89)\n", + "for t in t1:\n", + " print (t)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34\n", + "12\n", + "56\n", + "78\n", + "89\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "r6Smq-9CbmHF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "d79d7a86-efc2-47fd-ca60-0bcd16544bad" + }, + "cell_type": "code", + "source": [ + "# Writing over a tuple\n", + "# One cannot change the elements stored in the tuple. \n", + "# But you can assign a new tuple to the variable that stores the tuple you wanna change.\n", + "t1 = (23, 12, 45, 78)\n", + "print (t1)\n", + "\n", + "t1 = (45, 67) # t1 changed to new value\n", + "print (t1)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(23, 12, 45, 78)\n", + "(45, 67)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "kTgKmVDWbmKG", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "DIN7jg6xbmLq", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "1jCPpew1bmOW", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "YWOvHXTpbmQG", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "bpzCXqkebmTf", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/WEEK1NOI6.ipynb b/WEEK1NOI6.ipynb new file mode 100644 index 0000000..6e5d898 --- /dev/null +++ b/WEEK1NOI6.ipynb @@ -0,0 +1,1329 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled6.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/ASIF8240233397/Assignment-1/blob/master/WEEK1NOI6.ipynb)" + ] + }, + { + "metadata": { + "id": "WjLOFKkJXQSb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "813231e1-ff08-4524-b68d-cf9db72de9e3" + }, + "cell_type": "code", + "source": [ + "# Simple Lists\n", + "names = [\"Jennifer\", \"Python\", \"Scarlett\"]\n", + "nums = [1, 2, 3, 4, 5]\n", + "chars = ['A', 'q', 'E', 'z', 'Y']\n", + "\n", + "print (names)\n", + "print (nums)\n", + "print (chars)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'Python', 'Scarlett']\n", + "[1, 2, 3, 4, 5]\n", + "['A', 'q', 'E', 'z', 'Y']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wqC9x39fXXih", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "6f0adf6d-296a-43b6-b5de-848e9e59e6fd" + }, + "cell_type": "code", + "source": [ + "# Can have multiple data types in one list\n", + "rand_list = [\"Jennifer\", \"Python\", \"refinneJ\", 'J', '9', 9, 12.90, \"Who\"]\n", + "print (rand_list)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'Python', 'refinneJ', 'J', '9', 9, 12.9, 'Who']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "g9nJBCmcXmYc", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "0825921b-2cd0-4c63-f9ed-1892288944f3" + }, + "cell_type": "code", + "source": [ + "# Accessing elements in a list\n", + "# O-indexed\n", + "print (names[2])\n", + "print (rand_list[3])\n", + "print (names[0] + \" \" + rand_list[2].title())" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Scarlett\n", + "J\n", + "Jennifer Refinnej\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TShWgwSJXu0B", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "" + ] + }, + { + "metadata": { + "id": "B2E28FACXvei", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2eb6cd09-3975-4506-bc39-19e3b57c8f17" + }, + "cell_type": "code", + "source": [ + "# Negetive indexes: Access elements from the end of the list without knowing the size of the list\n", + "print (rand_list[-1]) # Returns the last element of the list [1st from the end]\n", + "print (rand_list[-2]) # Returns the 2nd last element\n", + "# and so on.." + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Who\n", + "12.9\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BljuOVjDXxxG", + "colab_type": "text" + }, + "cell_type": "markdown", + "source": [ + "" + ] + }, + { + "metadata": { + "id": "SLN9SKCDXyDF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e090f875-33ba-492c-ae25-7483664a9b03" + }, + "cell_type": "code", + "source": [ + "# Negetive indexes: Access elements from the end of the list without knowing the size of the list\n", + "print (rand_list[-1]) # Returns the last element of the list [1st from the end]\n", + "print (rand_list[-2]) # Returns the 2nd last element\n", + "# and so on.." + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Who\n", + "12.9\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "mEdNAo8cX3oh", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "fbc880e2-f52f-47dc-e81b-f519f854377b" + }, + "cell_type": "code", + "source": [ + "# Now here's a question.\n", + "print (rand_list[-1] + \" is \" + names[2] + \"?\")\n", + "print (\"A) \" + rand_list[0] + \"'s sister\\tB) \" + names[0] + \"'s Friend\\nC) Not Related to \" + rand_list[-8] + \"\\tD) Nice question but I don't know\")" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Who is Scarlett?\n", + "A) Jennifer's sister\tB) Jennifer's Friend\n", + "C) Not Related to Jennifer\tD) Nice question but I don't know\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "dASR1GleX6qa", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "b5f2641e-3834-4ad3-af03-f2f1687c534e" + }, + "cell_type": "code", + "source": [ + "# Modifying elements in a list\n", + "str_list = [\"Scarlett\", \"is\", \"a\", \"nice\", 'girl', '!']\n", + "\n", + "print (str_list)\n", + "str_list[0] = \"Jennifer\"\n", + "print (str_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Scarlett', 'is', 'a', 'nice', 'girl', '!']\n", + "['Jennifer', 'is', 'a', 'nice', 'girl', '!']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BLNIO_lJYDf7", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ae5f8e3b-d60d-45bc-8aa8-f7b630a60634" + }, + "cell_type": "code", + "source": [ + "# Adding elements to a list\n", + "# Use append() to add elements to the end of the list\n", + "str_list.append ('She is 21.')\n", + "print (str_list)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'is', 'a', 'nice', 'girl', '!', 'She is 21.']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8Jxgy-tRYFra", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "0034f1eb-6f59-4451-9315-96ceaa57fe8e" + }, + "cell_type": "code", + "source": [ + "# So, you can build lists like this\n", + "my_list = []\n", + "my_list.append (\"myname\")\n", + "my_list.append (\"myage\")\n", + "my_list.append (\"myaddress\")\n", + "my_list.append (\"myphn\")\n", + "my_list.append (\"is\")\n", + "my_list.append (1234567890)\n", + "print (my_list)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['myname', 'myage', 'myaddress', 'myphn', 'is', 1234567890]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Fz2Fa19jYULV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e0bf6884-106c-409a-f480-8f06693b5ac2" + }, + "cell_type": "code", + "source": [ + "# Insert elements at specific positions of the list\n", + "# insert(index, element)\n", + "my_list.insert (0, \"Mr/Miss/Mrs\")\n", + "print (my_list)\n", + "\n", + "my_list.insert(4, \"mybday\")\n", + "print (my_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Mr/Miss/Mrs', 'myname', 'myage', 'myaddress', 'myphn', 'is', 1234567890]\n", + "['Mr/Miss/Mrs', 'myname', 'myage', 'myaddress', 'mybday', 'myphn', 'is', 1234567890]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "sQJ0dFsBYXfQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "a8a5d5b7-34fe-4634-9c3d-458d40bd1c09" + }, + "cell_type": "code", + "source": [ + "# Using '-1' to insert at the end doesn't work and inserts element at the 2nd last position.\n", + "my_list = ['A', 'B', 'C', 'D']\n", + "my_list.insert (-1, 'E')\n", + "print (my_list)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'E', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "lkNjMAwMYafx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "e4096290-323b-4090-f47b-ac1037a8d7a0" + }, + "cell_type": "code", + "source": [ + "# Using '-2' inserts at 3rd last position\n", + "# In general, use '-n' to insert at 'n+1'th position from end.\n", + "my_list = ['A', 'B', 'C', 'D']\n", + "my_list.insert (-2, 'E')\n", + "print (my_list)" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'E', 'C', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "WU0atyGEYdRb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "1d3b79b3-774d-42b3-ac51-84ce5a269d3f" + }, + "cell_type": "code", + "source": [ + "# Insert elements at the end\n", + "l1 = ['A', 'B', 'C', 'D']\n", + "l2 = ['A', 'B', 'C', 'D']\n", + "\n", + "l1.append('E')\n", + "l2.insert(len(my_list), 'E')\n", + "print (l1)\n", + "print (l2)" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n", + "['A', 'B', 'C', 'D', 'E']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "37t5gqXQYh-e", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7f8e114f-e447-49a5-8ce8-6913bcacff4c" + }, + "cell_type": "code", + "source": [ + "# Length of the list\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "print (len(l1))" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wfPFmyO9YmIN", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "3425f117-56a8-4c12-e8f8-b14653801942" + }, + "cell_type": "code", + "source": [ + "# # Removing elements from list\n", + "# del can remove any element from list as long as you know its index\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "print (l1)\n", + "\n", + "del l1[0]\n", + "print (l1)\n", + "\n", + "del l1[-1]\n", + "print (l1)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n", + "['B', 'C', 'D', 'E']\n", + "['B', 'C', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "vTD6M3VNYtMw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2e574447-0a58-4615-abe9-fc0f90628439" + }, + "cell_type": "code", + "source": [ + "# pop() can remove the last element from list when used without any arguments\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "# pop() returns the last element, so c would store the popped element\n", + "c = l1.pop()\n", + "\n", + "print (l1)\n", + "print (c) " + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D']\n", + "E\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RWAQaebVYxJJ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "439fc078-7bb6-4080-dc8d-944f08365685" + }, + "cell_type": "code", + "source": [ + "# pop(n) -> Removes the element at index 'n' and returns it\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "\n", + "# Removes the element at 0 position and returns it\n", + "c = l1.pop(0)\n", + "print (l1)\n", + "print (c)\n", + "\n", + "# Works as expected with negetive indexes\n", + "c = l1.pop(-1)\n", + "print (l1)\n", + "print (c)" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['B', 'C', 'D', 'E']\n", + "A\n", + "['B', 'C', 'D']\n", + "E\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "waIOhl6vY0MA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "fa902353-9fa5-4dd9-9f21-28bd8f0e74c9" + }, + "cell_type": "code", + "source": [ + "# Removing an item by value\n", + "# remove() only removes the first occurence of the value that is specified.\n", + "q1 = [\"Seriously, \", \"what\", \"happened\", \"to\", \"Jennifer\", \"and\", \"Jennifer\", \"?\"]\n", + "print (q1)\n", + "\n", + "q1.remove (\"Jennifer\")\n", + "print (q1)\n", + "\n", + "n1 = \"and\"\n", + "q1.remove(n1)\n", + "print (q1)" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Seriously, ', 'what', 'happened', 'to', 'Jennifer', 'and', 'Jennifer', '?']\n", + "['Seriously, ', 'what', 'happened', 'to', 'and', 'Jennifer', '?']\n", + "['Seriously, ', 'what', 'happened', 'to', 'Jennifer', '?']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "FxnlIj8ZY4_G", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "b3ea93e3-3b4b-4e18-c627-0b4c21ab3453" + }, + "cell_type": "code", + "source": [ + "# Sorting a list\n", + "# sort() -> sorts list in increasing or decreasing order, *permantantly*\n", + "# Sorts in alphabetical order\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.sort()\n", + "print (l1)\n", + "\n", + "# Sorts in increasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "l2.sort()\n", + "print (l2)" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n", + "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 45.67, 90, 200]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "UY4-hZCSY5No", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "e8e0d8b8-b5ce-4324-b795-fa8b65ddc345" + }, + "cell_type": "code", + "source": [ + "# Reverse sorts alphabetical order\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.sort(reverse=True)\n", + "print (l1)\n", + "\n", + "# Sorts in decreasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "l2.sort(reverse=True)\n", + "print (l2)" + ], + "execution_count": 20, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['E', 'D', 'C', 'B', 'A']\n", + "[200, 90, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uqeubf5gY5Ym", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "ee0a0440-33c4-4284-f05f-a915a3a4c0f4" + }, + "cell_type": "code", + "source": [ + "# sorted() -> Sorts list in increasing or decreasing order, *temporarily*\n", + "# Sorts in increasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "print (l2)\n", + "print (sorted(l2))\n", + "print (l2)" + ], + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "[0, 1, 2, 4, 9.45, 12.01, 12.02, 16, 45.67, 90, 200]\n", + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "f2XE7PoNY5kK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "5c53aea2-fce0-4b64-9e2c-fee033b327c9" + }, + "cell_type": "code", + "source": [ + "# Sorts in decreasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "print (l2)\n", + "print (sorted(l2, reverse=True))\n", + "print (l2)" + ], + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "[200, 90, 45.67, 16, 12.02, 12.01, 9.45, 4, 2, 1, 0]\n", + "[2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xKpQ9NJhY5uk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "f12d0917-ea4d-454d-9055-da564b971503" + }, + "cell_type": "code", + "source": [ + "# Reverse list\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.reverse()\n", + "print (l1)" + ], + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "kljvmsMSZOXH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + }, + "outputId": "f2ed61a5-ee7a-4dfe-bbe7-b68158430fb1" + }, + "cell_type": "code", + "source": [ + "# Looping Through a list using for\n", + "l1 = [\"Scarlett\", \"is\", \"now\", \"back\", \"from\", \"her first\", \"Python\", \"lesson.\"]\n", + "\n", + "# Do notice the indentations\n", + "for each_word in l1:\n", + " print (each_word)" + ], + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Scarlett\n", + "is\n", + "now\n", + "back\n", + "from\n", + "her first\n", + "Python\n", + "lesson.\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "7LRf7kU-ZUGM", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "dbf46920-9f69-4410-94c9-0bc7332b86e6" + }, + "cell_type": "code", + "source": [ + "# Looping through a list using while\n", + "l1 = [\"Scarlett\", \"is\", \"in\", \"love\", \"with\", \"Python\"]\n", + "i = 0\n", + "while i is not len(l1):\n", + " print (l1[i])\n", + " i += 1" + ], + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Scarlett\n", + "is\n", + "in\n", + "love\n", + "with\n", + "Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "0BRsoHIVY540", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "b355bc35-cd83-4938-ae60-752249674df1" + }, + "cell_type": "code", + "source": [ + "# Numerical lists\n", + "# Note: range(n, m) will loop over numbers from n to m-1\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "print (\"Guess how much Scarlett scored in her first lesson out of 5:\")\n", + "for val in range(1, 6):\n", + " print (l1[val-1] + \") \" + str(val))" + ], + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Guess how much Scarlett scored in her first lesson out of 5:\n", + "A) 1\n", + "B) 2\n", + "C) 3\n", + "D) 4\n", + "E) 5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "xorNubfJZd5z", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1c4129a0-ca4d-44e0-99d6-d05cd3f84699" + }, + "cell_type": "code", + "source": [ + "# Using range() to make a list of numbers\n", + "num_list = list(range(1, 6))\n", + "print (num_list)" + ], + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "aSN5XZFnZiy9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b4f1ea6c-9ff7-4f22-c7f1-5663c2a0f54f" + }, + "cell_type": "code", + "source": [ + "# Use range() to skip values at intervals\n", + "# range (num_to_start_from, num_to_end_at+1, interval)\n", + "l1 = list(range(10, 51, 5))\n", + "print (l1)" + ], + "execution_count": 28, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[10, 15, 20, 25, 30, 35, 40, 45, 50]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "b5USM--xZkv0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "aaaddb13-c92b-4c85-8408-2d9106a1d276" + }, + "cell_type": "code", + "source": [ + "# Operations with list of numbers with -> min() max() sum()\n", + "l1 = [2, 3, 4, 45, 1, 5, 6, 3, 1, 23, 14]\n", + "\n", + "print (\"Sum: \" + str(sum(l1)))\n", + "print (\"Max: \" + str(max(l1)))\n", + "print (\"Min: \" + str(min(l1)))" + ], + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Sum: 107\n", + "Max: 45\n", + "Min: 1\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BtWBngkEZrD4", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "6bc72035-f4a4-484e-da37-56f527ba6691" + }, + "cell_type": "code", + "source": [ + "# List Comprehensions\n", + "# Simple\n", + "l1 = [i for i in range(20, 30, 1)]\n", + "l2 = [i+1 for i in range(20, 30, 1)]\n", + "l3 = [[i, i**2] for i in range(2, 12, 3)]\n", + "print (l1)\n", + "print (l2)\n", + "print (l3)" + ], + "execution_count": 30, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]\n", + "[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]\n", + "[[2, 4], [5, 25], [8, 64], [11, 121]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "HoKbahVOZs09", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "f81ebfc5-834c-40a8-c545-f51ff408ea9c" + }, + "cell_type": "code", + "source": [ + "# A few more list comprehension examples\n", + "equi_list_1 = [[x, y, z] for x in range(1, 3) for y in range(3, 6) for z in range(6, 9)]\n", + "print (equi_list_1)" + ], + "execution_count": 31, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 5, 6], [1, 5, 7], [1, 5, 8], [2, 3, 6], [2, 3, 7], [2, 3, 8], [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 5, 6], [2, 5, 7], [2, 5, 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wj5RySixZxR5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 54 + }, + "outputId": "0632f478-75d7-4808-dfd4-b068cb92bc06" + }, + "cell_type": "code", + "source": [ + "# The above list comprehension is equivalent of the following code\n", + "equi_list_2 = []\n", + "for x in range(1, 3):\n", + " for y in range(3, 6):\n", + " for z in range(6, 9):\n", + " equi_list_2.append([x, y, z])\n", + "print (equi_list_2)" + ], + "execution_count": 32, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 5, 6], [1, 5, 7], [1, 5, 8], [2, 3, 6], [2, 3, 7], [2, 3, 8], [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 5, 6], [2, 5, 7], [2, 5, 8]]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "-YlhaCEQZzb6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "9648f87c-a98c-41e1-8f8c-6200fb8ba576" + }, + "cell_type": "code", + "source": [ + "# Proof of equivalence (Do execute the above two blocks of code before running this)\n", + "print (equi_list_1 == equi_list_2)\n" + ], + "execution_count": 33, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TX3zJEiFZ4Lt", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "3ea199af-51f4-4242-ffbc-4b781211e690" + }, + "cell_type": "code", + "source": [ + "# List Comprehension with conditionals\n", + "l1 = [x if x%5==0 else \"blank\" for x in range(20)]\n", + "print (l1)" + ], + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[0, 'blank', 'blank', 'blank', 'blank', 5, 'blank', 'blank', 'blank', 'blank', 10, 'blank', 'blank', 'blank', 'blank', 15, 'blank', 'blank', 'blank', 'blank']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Fe7WDafZZ4Ok", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "b2723391-a510-478d-f57a-f7583d020f9d" + }, + "cell_type": "code", + "source": [ + "# One more list comprehension with conditionals\n", + "l1 = [\"Jennifer\", \"met\", \"Scarlett\", \"in\", \"Python\", \"lessons\", \"they\", \"take.\"]\n", + "l2 = [[str(x) + \") \" + y] for x in range(len(l1)) for y in l1 if l1[x] == y]\n", + "print (l2)" + ], + "execution_count": 35, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[['0) Jennifer'], ['1) met'], ['2) Scarlett'], ['3) in'], ['4) Python'], ['5) lessons'], ['6) they'], ['7) take.']]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "F2IWF0sQZ4Tl", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "66bcefce-de93-45fd-c501-d8abc1227991" + }, + "cell_type": "code", + "source": [ + "# Slicing a list\n", + "l1 = [\"Jennifer\", \"is\", \"now\", \"friends\", \"with\", \"Scarlett\"]\n", + "\n", + "# [start_index : end_index+1]\n", + "print(\"[2:5] --> \" + str(l1[2:5]))\n", + "print(\"[:4] --> \" + str(l1[:4])) # everthing before 4th index [excluding the 4th]\n", + "print(\"[2:] --> \" + str(l1[2:])) # everything from 2nd index [including the 2nd]\n", + "print(\"[:] --> \" + str(l1[:])) # every element in the list" + ], + "execution_count": 36, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[2:5] --> ['now', 'friends', 'with']\n", + "[:4] --> ['Jennifer', 'is', 'now', 'friends']\n", + "[2:] --> ['now', 'friends', 'with', 'Scarlett']\n", + "[:] --> ['Jennifer', 'is', 'now', 'friends', 'with', 'Scarlett']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "S9RXmRoBZ4Wb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 85 + }, + "outputId": "7323db2c-5121-4f55-cad5-2599ced3398e" + }, + "cell_type": "code", + "source": [ + "# Some more slicing\n", + "l1 = [\"Jennifer\", \"and\", \"Scarlett\", \"now\", \"Pythonistas\", \"!\"]\n", + "\n", + "print (\"[-2:] --> \" + str(l1[-2:]))\n", + "print (\"[:-3] --> \" + str(l1[:-3]))\n", + "print (\"[-5:-2] --> \" + str(l1[-5:-2]))\n", + "print (\"[-4:-6] --> \" + str(l1[-4:-6]))" + ], + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[-2:] --> ['Pythonistas', '!']\n", + "[:-3] --> ['Jennifer', 'and', 'Scarlett']\n", + "[-5:-2] --> ['and', 'Scarlett', 'now']\n", + "[-4:-6] --> []\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "95Rp2XP0aFa1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "42ad9718-e06c-48a9-a6bf-6833d832d0f3" + }, + "cell_type": "code", + "source": [ + "# Looping through a slice\n", + "l1 = [\"Pythonistas\", \"rock\", \"!!!\", \"XD\"]\n", + "for w in l1[-4:-1]:\n", + " print (w.upper())" + ], + "execution_count": 38, + "outputs": [ + { + "output_type": "stream", + "text": [ + "PYTHONISTAS\n", + "ROCK\n", + "!!!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wOytf0WvaJJm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "c35af46b-a94a-42b6-e977-bd8bc8a38974" + }, + "cell_type": "code", + "source": [ + "# Copying a list\n", + "l1 = [\"We\", \"should\", \"use\", \"[:]\", \"to\", \"copy\", \"the\", \"whole\", \"list\"]\n", + "l2 = l1[:]\n", + "print(l2)" + ], + "execution_count": 39, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['We', 'should', 'use', '[:]', 'to', 'copy', 'the', 'whole', 'list']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "mduR7RZ5aJPy", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "cc30c44b-ecf8-4937-fbba-91e858a0272c" + }, + "cell_type": "code", + "source": [ + "# Proof that the above two lists are different\n", + "l2.append(\". Using [:] ensures the two lists are different\")\n", + "\n", + "print (l1)\n", + "print (l2)" + ], + "execution_count": 40, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['We', 'should', 'use', '[:]', 'to', 'copy', 'the', 'whole', 'list']\n", + "['We', 'should', 'use', '[:]', 'to', 'copy', 'the', 'whole', 'list', '. Using [:] ensures the two lists are different']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "gPP4WJ7qaJWc", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "7e24398b-0847-4fc7-adef-75cdf0a9ea85" + }, + "cell_type": "code", + "source": [ + "# What happens if we directly assign one list to the other instead of using slices\n", + "l1 = [\"Jennifer\", \"now\", \"wonders\", \"what\", \"happens\", \"if\", \"we\", \"directly\", \"assign.\"]\n", + "l2 = l1\n", + "l2.append(\"Both variables point to the same list\")\n", + "\n", + "print (l1)\n", + "print (l2)" + ], + "execution_count": 41, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'now', 'wonders', 'what', 'happens', 'if', 'we', 'directly', 'assign.', 'Both variables point to the same list']\n", + "['Jennifer', 'now', 'wonders', 'what', 'happens', 'if', 'we', 'directly', 'assign.', 'Both variables point to the same list']\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From cfad00f968e68368ac0e6bb954ca83f0bbc0f0ea Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 15:57:05 +0530 Subject: [PATCH 3/9] Created using Colaboratory --- condition6.ipynb | 403 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 403 insertions(+) create mode 100644 condition6.ipynb diff --git a/condition6.ipynb b/condition6.ipynb new file mode 100644 index 0000000..1e2dce3 --- /dev/null +++ b/condition6.ipynb @@ -0,0 +1,403 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled6.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/condition6.ipynb)" + ] + }, + { + "metadata": { + "id": "dKdXyrfvQGqW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "cab59594-bc56-4a4a-d165-f510d84b0355" + }, + "cell_type": "code", + "source": [ + "# if..else\n", + "v1 = 5\n", + "if v1 == 5:\n", + " print (v1)\n", + "else:\n", + " print (\"v1 is not 5\")" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "FdozUzoiQX3Y", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "c65f97dc-ca1c-4737-d75f-8fe019d26640" + }, + "cell_type": "code", + "source": [ + "# if..elif..else\n", + "s1 = \"Jennifer\"\n", + "s2 = \"loves\"\n", + "s3 = \"Python\"\n", + "if s1 == \"Python\":\n", + " print (\"s1 is Python\")\n", + "elif s2 == \"Jennifer\":\n", + " print (\"s2 is Jennifer\")\n", + "elif s1 == \"loves\":\n", + " print (\"s1 is loves\")\n", + "else:\n", + " print (\"Jennifer loves Python!\")" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer loves Python!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "cgdM49vUQX6g", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "f28f7a38-1c95-4606-dee2-0f11b1bf8f9d" + }, + "cell_type": "code", + "source": [ + "# One liner\n", + "v1 = 5\n", + "x = 10 if v1 == 5 else 13\n", + "print (x)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "k4EQsMmBQX-I", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 270 + }, + "outputId": "b52f14ab-fea2-4a2d-eaf2-958275d2b51f" + }, + "cell_type": "code", + "source": [ + "# Let's see the conditionals available\n", + "v1 = \"Jennifer\"\n", + "v2 = \"Python\"\n", + "v3 = 45\n", + "v4 = 67\n", + "v5 = 45\n", + "\n", + "# Test for equality\n", + "print (v1 == v2)\n", + "\n", + "# Test for greater than and greater than equal\n", + "print (v4 > v3)\n", + "print (v5 >= v2)\n", + "\n", + "# Test for lesser than and lesser than equal\n", + "print (v4 < v3)\n", + "print (v5 <= v2)\n", + "\n", + "# Inequality\n", + "print (v1 != v2)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "TypeError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;31m# Test for greater than and greater than equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv4\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mv3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 12\u001b[0;31m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv5\u001b[0m \u001b[0;34m>=\u001b[0m \u001b[0mv2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 13\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m# Test for lesser than and lesser than equal\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: '>=' not supported between instances of 'int' and 'str'" + ] + } + ] + }, + { + "metadata": { + "id": "M6iqWF9dQYBB", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "ba141868-dba1-4151-eb87-1daef9612db1" + }, + "cell_type": "code", + "source": [ + "# Note:\n", + "v1 = 45\n", + "v2 = \"45\"\n", + "print (v1 == v2) # False\n", + "print (str(v1) == v2) # True" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "VDTXy39BQYEn", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "5a9ccfc7-ea9f-4b9d-fc3b-924eb9907193" + }, + "cell_type": "code", + "source": [ + "# Ignore case when comparing two strings\n", + "s1 = \"Jennifer\"\n", + "s2 = \"jennifer\"\n", + "\n", + "print (s1 == s2) # False\n", + "print (s1.lower() == s2.lower()) # True\n", + "# OR\n", + "print (s1.upper() == s2.upper()) # True" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "gPgCuwyPQxf3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "9b572a60-e412-4a73-ea01-8abec07a3c81" + }, + "cell_type": "code", + "source": [ + "# Checking multiple conditions 'and' and 'or'\n", + "v1 = \"Jennifer\"\n", + "v2 = \"Python\"\n", + "\n", + "# 'and' -> evaluates true when both conditions are True\n", + "print (v1 == \"Jennifer\" and v2 == \"Python\")\n", + "# 'or' -> evaluates true when any one condition is True\n", + "print (v1 == \"Python\" or v2 == \"Python\")" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True\n", + "True\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "K1Do4gy8QxnO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "523faf0b-471b-4971-b7e6-0d73be05fe54" + }, + "cell_type": "code", + "source": [ + "s1 = \"Jennifer\"\n", + "s2 = \"Python\"\n", + "\n", + "print (s1 > s2) # True -> since 'Jennifer' comes lexographically before 'Python'" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "False\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "L65z-_NeQ13H", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 87 + }, + "outputId": "7cebb9cc-992d-4905-e268-b3c4d1ab8c1e" + }, + "cell_type": "code", + "source": [ + "# Check whether a value is in a list -> 'in'\n", + "l1 = [23, 45, 67, \"Jennifer\", \"Python\", 'A']\n", + "\n", + "print (23 in l1)\n", + "print ('A' in l1)\n", + "print (\"Python\" in l1)\n", + "print (32 in l1)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "True\n", + "False\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "RWpqgIl5Q16n", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "418c98dc-cd81-48f4-d526-4424f9cd2946" + }, + "cell_type": "code", + "source": [ + "# Putting it together\n", + "l1 = [23, 1, 'A', \"Jennifer\", 9.34]\n", + "\n", + "# This is True, so the other statements are not checked\n", + "if 23 in l1 and 'B' not in l1: # Note: use of 'not'\n", + " print (\"1\")\n", + "elif 23 >= l1[0]: # True\n", + " print (\"2\")\n", + "elif 2.45 < l1[-1]: # True\n", + " print (\"3\")" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "1\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "TpywWBs4Q7f_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "ed47b94b-b6d3-4478-eb3c-cc8379853587" + }, + "cell_type": "code", + "source": [ + "# Checking if list is empty\n", + "l1 = []\n", + "l2 = [\"Jennifer\"]\n", + "\n", + "if l1:\n", + " print (1)\n", + "elif l2:\n", + " print (2)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 54617d0496463b175f20541d2ece566c0414cf1c Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:03:07 +0530 Subject: [PATCH 4/9] Created using Colaboratory --- loops.ipynb | 360 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 loops.ipynb diff --git a/loops.ipynb b/loops.ipynb new file mode 100644 index 0000000..800bd10 --- /dev/null +++ b/loops.ipynb @@ -0,0 +1,360 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled7.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/loops.ipynb)" + ] + }, + { + "metadata": { + "id": "xVLPiTpyRYrT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 121 + }, + "outputId": "d764099f-cf76-4eaf-8eaa-ac7b379861e8" + }, + "cell_type": "code", + "source": [ + "# Simple while\n", + "# Loop runs till the condition is True\n", + "v1 = 5\n", + "while v1 <= 10:\n", + " print (v1)\n", + " v1 += 1" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "d-WiGrJqRZRJ", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Infinite Loops\n", + "while 1:\n", + " print (1)\n", + " \n", + "while True:\n", + " print (1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "GWB7-hpsRZTu", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "116ae752-7b0a-4d55-89af-186f36478978" + }, + "cell_type": "code", + "source": [ + "# One Liner while\n", + "v1 = 0\n", + "while v1 <= 40: v1 += 1\n", + "print (v1)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "41\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "98La2XxFRZV_", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "e87f9af8-124d-4bd6-8c9e-288d227d97a7" + }, + "cell_type": "code", + "source": [ + "v1 = 2\n", + "++v1\n", + "++v1\n", + "print (v1)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "2\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "BygAlculRZYO", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Terminate loop on a certain user input\n", + "# Note: The loop will break only when the user inputs 100\n", + "v1 = 1\n", + "while v1 != 100:\n", + " v1 = int(input(\"Enter new v1: \"))\n", + " print (\"v1 modified to: \" + str(v1))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "aI7Lf1lQR5Xy", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 963 + }, + "outputId": "42b42e42-d80e-4549-d653-10c61f4c05ff" + }, + "cell_type": "code", + "source": [ + "# 'break' -> breaks out of loop, doesn't execute any statement after it\n", + "while 1:\n", + " v1 = int(input())\n", + " if v1 == 100: \n", + " break;\n", + " print (v1)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 729\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 730\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 731\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 394\u001b[0m \"\"\"\n\u001b[0;32m--> 395\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 396\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mv1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mv1\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mv1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "metadata": { + "id": "lV_Jog4iRZab", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1085 + }, + "outputId": "b8da105e-1002-495d-849b-4263bbcd6c4f" + }, + "cell_type": "code", + "source": [ + "# 'continue' -> continues to next iteration, skips all statements after it for that iteration\n", + "# Note: When 'v1' < 100 the last print statement is skipped and the control moves to the next iteration\n", + "while 1:\n", + " print (\"Iteration begins\")\n", + " v1 = int(input())\n", + " if v1 == 100:\n", + " break;\n", + " elif v1 < 100:\n", + " print (\"v1 less than 100\")\n", + " continue;\n", + " print (\"Iteration complete\")" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Iteration begins\n", + "5\n", + "v1 less than 100\n", + "Iteration begins\n", + "58552\n", + "Iteration complete\n", + "Iteration begins\n" + ], + "name": "stdout" + }, + { + "output_type": "error", + "ename": "KeyboardInterrupt", + "evalue": "ignored", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 729\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 730\u001b[0;31m \u001b[0mident\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreply\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msession\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdin_socket\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 731\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/jupyter_client/session.py\u001b[0m in \u001b[0;36mrecv\u001b[0;34m(self, socket, mode, content, copy)\u001b[0m\n\u001b[1;32m 802\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 803\u001b[0;31m \u001b[0mmsg_list\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msocket\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv_multipart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 804\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mzmq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mZMQError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/sugar/socket.py\u001b[0m in \u001b[0;36mrecv_multipart\u001b[0;34m(self, flags, copy, track)\u001b[0m\n\u001b[1;32m 394\u001b[0m \"\"\"\n\u001b[0;32m--> 395\u001b[0;31m \u001b[0mparts\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtrack\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 396\u001b[0m \u001b[0;31m# have first part already, only loop while more to receive\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket.Socket.recv\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32mzmq/backend/cython/socket.pyx\u001b[0m in \u001b[0;36mzmq.backend.cython.socket._recv_copy\u001b[0;34m()\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/zmq/backend/cython/checkrc.pxd\u001b[0m in \u001b[0;36mzmq.backend.cython.checkrc._check_rc\u001b[0;34m()\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: ", + "\nDuring handling of the above exception, another exception occurred:\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Iteration begins\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mv1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mv1\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m100\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m;\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 703\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 704\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 705\u001b[0;31m \u001b[0mpassword\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 706\u001b[0m )\n\u001b[1;32m 707\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 733\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 735\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 736\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 737\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " + ] + } + ] + }, + { + "metadata": { + "id": "4dliIu1gRZca", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "87a98aac-077d-4212-ff69-db4dccf0a373" + }, + "cell_type": "code", + "source": [ + "# while with lists\n", + "l1 = [\"Jennifer\", 12, \"Python\", 'A', 56, 'B', 2.12, \"Scarlett\"]\n", + "l2 = []\n", + "i = 0\n", + "while i < len(l1):\n", + " if type(l1[i]) == int:\n", + " l2.append(l1[i])\n", + " i += 1\n", + "print (l2)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[12, 56]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Kzf2B0CjSOSG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "30837c43-e332-4767-d13b-344b4447908e" + }, + "cell_type": "code", + "source": [ + "# Removing all instances of a specific value in list\n", + "l1 = ['A', 'B', 'C', 'D', 'A', 'E', 'Q', 'A', 'Z', 'A', 'Q', 'D', 'A']\n", + "while 'A' in l1: l1.remove('A')\n", + "print (l1)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['B', 'C', 'D', 'E', 'Q', 'Z', 'Q', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uuzHjSgvSOWY", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 91 + }, + "outputId": "c8fae08d-687c-4d5c-83b1-00fc16b89add" + }, + "cell_type": "code", + "source": [ + "# Filing a dictionary\n", + "d1 = {}\n", + "while 1:\n", + " key = input(\"Enter a key: \")\n", + " value = input(\"Enter a value: \")\n", + " d1[key] = value;\n", + " if input(\"exit? \") == \"yes\": break;\n", + "print (d1)" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a key: 2545\n", + "Enter a value: 151\n", + "exit? 22\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 28782e4696a8fb0e9596771e7cb08855f334dd5c Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:07:49 +0530 Subject: [PATCH 5/9] Created using Colaboratory --- string.ipynb | 251 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 string.ipynb diff --git a/string.ipynb b/string.ipynb new file mode 100644 index 0000000..5060e92 --- /dev/null +++ b/string.ipynb @@ -0,0 +1,251 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled8.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/string.ipynb)" + ] + }, + { + "metadata": { + "id": "NVXNM0PHSwxc", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Import the string module to get all the in-built helper methods for string\n", + "import string" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "DM6YE0rPSxei", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "7ffeb3f1-88af-4f4f-a98c-8d574484f6fb" + }, + "cell_type": "code", + "source": [ + "# Case change of string variables\n", + "name = \"jennifEr loves Python\"\n", + "\n", + "# Title case\n", + "name_t = name.title() \n", + "print (name_t)\n", + "\n", + "# Upper case\n", + "name_t = name.upper() \n", + "print (name_t)\n", + "\n", + "# Lower case\n", + "name_t = name.lower() \n", + "print (name_t)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer Loves Python\n", + "JENNIFER LOVES PYTHON\n", + "jennifer loves python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "YQCRtP2tSxk9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "24ef8c8e-86cf-41fe-b005-dede344b513c" + }, + "cell_type": "code", + "source": [ + "# String Concatenation\n", + "fname = \"jennifer\"\n", + "lname = \"python\"\n", + "flname = fname + \" \" + lname\n", + "\n", + "print (fname + \" \" + lname)\n", + "print (\"Jennifer\" + \" \" + \"Python\")\n", + "# OR equivalently\n", + "print (flname.title())" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "jennifer python\n", + "Jennifer Python\n", + "Jennifer Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "hf6wqXesSxpP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 121 + }, + "outputId": "a98722d2-a0a4-4d79-fb54-72823e861650" + }, + "cell_type": "code", + "source": [ + "# Adding WhiteSpaces\n", + "print (\"Jen\\nloves\\npython\")\n", + "print (\"Jen\\tloves\\tpython\")\n", + "print (\"Jen\\tloves\\npython\")" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jen\n", + "loves\n", + "python\n", + "Jen\tloves\tpython\n", + "Jen\tloves\n", + "python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "g2oco9U5Sxtd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 69 + }, + "outputId": "7921dd2c-eb60-422f-95eb-b96d2c89eb69" + }, + "cell_type": "code", + "source": [ + "# Lists, Dictionaries, Tuples and other objects can be casted to String\n", + "l_langs = [\"Python\", \"R\", \"Julia\"] # List\n", + "t_langs = (\"Python\", \"R\", \"Julia\") # Tuple\n", + "d_langs = {1: \"Python\", 2: \"R\", 3: \"Julia\"} # Dictionary\n", + "\n", + "print (\"This is a list: \" + str(l_langs))\n", + "print (\"This is a tuple: \" + str(t_langs))\n", + "print (\"This is a dictionary: \" + str(d_langs))" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "This is a list: ['Python', 'R', 'Julia']\n", + "This is a tuple: ('Python', 'R', 'Julia')\n", + "This is a dictionary: {1: 'Python', 2: 'R', 3: 'Julia'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "gHlgZE7oSxvu", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "dc255914-a49a-4259-baaf-e66d9bdeb14e" + }, + "cell_type": "code", + "source": [ + "# str() -> casts other data types to string\n", + "jensage = 21\n", + "# print (\"Jennifer's age is \" + jensage) ## This is an error as one cannot concatenate string and integer\n", + "print (\"Jennifer's age is \" + str(jensage)) # This works!" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer's age is 21\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "_qAu4GCfSxxe", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "XeirsfkZSxzu", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0hVVGnEKSx39", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 9f7e0696ead95bbbddaf27353118b197592faf23 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:15:53 +0530 Subject: [PATCH 6/9] Created using Colaboratory --- list.ipynb | 648 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 648 insertions(+) create mode 100644 list.ipynb diff --git a/list.ipynb b/list.ipynb new file mode 100644 index 0000000..8c96f3c --- /dev/null +++ b/list.ipynb @@ -0,0 +1,648 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled9.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/list.ipynb)" + ] + }, + { + "metadata": { + "id": "6us7ClfETrmZ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 67 + }, + "outputId": "04b3078e-c275-4b42-c531-05ace8470231" + }, + "cell_type": "code", + "source": [ + "# Simple Lists\n", + "names = [\"Jennifer\", \"Python\", \"Scarlett\"]\n", + "nums = [1, 2, 3, 4, 5]\n", + "chars = ['A', 'q', 'E', 'z', 'Y']\n", + "\n", + "print (names)\n", + "print (nums)\n", + "print (chars)" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'Python', 'Scarlett']\n", + "[1, 2, 3, 4, 5]\n", + "['A', 'q', 'E', 'z', 'Y']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "_DWn589oTwQe", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "257a7718-94e9-4005-d90e-bb60bda47417" + }, + "cell_type": "code", + "source": [ + "# Can have multiple data types in one list\n", + "rand_list = [\"Jennifer\", \"Python\", \"refinneJ\", 'J', '9', 9, 12.90, \"Who\"]\n", + "print (rand_list)" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'Python', 'refinneJ', 'J', '9', 9, 12.9, 'Who']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "vxqgsPW0Tz4W", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "9e017b73-52b2-479a-c736-65d243e91009" + }, + "cell_type": "code", + "source": [ + "# Accessing elements in a list\n", + "# O-indexed\n", + "print (names[2])\n", + "print (rand_list[3])\n", + "print (names[0] + \" \" + rand_list[2].title())" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Scarlett\n", + "J\n", + "Jennifer Refinnej\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Q-znEFcvT0en", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "2510911d-6f4a-4943-c6e9-2c831418d9da" + }, + "cell_type": "code", + "source": [ + "# Negetive indexes: Access elements from the end of the list without knowing the size of the list\n", + "print (rand_list[-1]) # Returns the last element of the list [1st from the end]\n", + "print (rand_list[-2]) # Returns the 2nd last element\n", + "# and so on.." + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Who\n", + "12.9\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qtLwGxRLT0jd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "7b0d8a87-7fe6-47c5-94fb-ccf713f877ad" + }, + "cell_type": "code", + "source": [ + "# Now here's a question.\n", + "print (rand_list[-1] + \" is \" + names[2] + \"?\")\n", + "print (\"A) \" + rand_list[0] + \"'s sister\\tB) \" + names[0] + \"'s Friend\\nC) Not Related to \" + rand_list[-8] + \"\\tD) Nice question but I don't know\")" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Who is Scarlett?\n", + "A) Jennifer's sister\tB) Jennifer's Friend\n", + "C) Not Related to Jennifer\tD) Nice question but I don't know\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "w7AGK0iCT0nd", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "67ca539a-d7e4-45de-f7e5-c3d365e76301" + }, + "cell_type": "code", + "source": [ + "# Modifying elements in a list\n", + "str_list = [\"Scarlett\", \"is\", \"a\", \"nice\", 'girl', '!']\n", + "\n", + "print (str_list)\n", + "str_list[0] = \"Jennifer\"\n", + "print (str_list)" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Scarlett', 'is', 'a', 'nice', 'girl', '!']\n", + "['Jennifer', 'is', 'a', 'nice', 'girl', '!']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "r4v5PFkhT0rt", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "3cab2a3c-5a5b-41b4-bc7a-2d1068cc1a87" + }, + "cell_type": "code", + "source": [ + "# Adding elements to a list\n", + "# Use append() to add elements to the end of the list\n", + "str_list.append ('She is 21.')\n", + "print (str_list)" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Jennifer', 'is', 'a', 'nice', 'girl', '!', 'She is 21.']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4NqTnZwxUcG1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8483dccc-713e-4af7-daec-44ffdf245a2b" + }, + "cell_type": "code", + "source": [ + "# So, you can build lists like this\n", + "my_list = []\n", + "my_list.append (\"myname\")\n", + "my_list.append (\"myage\")\n", + "my_list.append (\"myaddress\")\n", + "my_list.append (\"myphn\")\n", + "my_list.append (\"is\")\n", + "my_list.append (1234567890)\n", + "print (my_list)" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['myname', 'myage', 'myaddress', 'myphn', 'is', 1234567890]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "t05tcf_TUcJs", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "db0a14ed-0b57-467c-e461-6010126494ed" + }, + "cell_type": "code", + "source": [ + "# Insert elements at specific positions of the list\n", + "# insert(index, element)\n", + "my_list.insert (0, \"Mr/Miss/Mrs\")\n", + "print (my_list)\n", + "\n", + "my_list.insert(4, \"mybday\")\n", + "print (my_list)" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Mr/Miss/Mrs', 'myname', 'myage', 'myaddress', 'myphn', 'is', 1234567890]\n", + "['Mr/Miss/Mrs', 'myname', 'myage', 'myaddress', 'mybday', 'myphn', 'is', 1234567890]\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "jNghZMJKUcMU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "0872352c-f7f0-4b56-a290-276375cbe2c3" + }, + "cell_type": "code", + "source": [ + "# Using '-1' to insert at the end doesn't work and inserts element at the 2nd last position.\n", + "my_list = ['A', 'B', 'C', 'D']\n", + "my_list.insert (-1, 'E')\n", + "print (my_list)" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'E', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "HlluwVknUnTV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "730778f2-7613-4610-ba9a-ab2f156af213" + }, + "cell_type": "code", + "source": [ + "# Using '-2' inserts at 3rd last position\n", + "# In general, use '-n' to insert at 'n+1'th position from end.\n", + "my_list = ['A', 'B', 'C', 'D']\n", + "my_list.insert (-2, 'E')\n", + "print (my_list)" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'E', 'C', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "JYbObdpyUqWW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "fdf1aeb8-0b1e-4492-d843-422be8e1aae6" + }, + "cell_type": "code", + "source": [ + "# Insert elements at the end\n", + "l1 = ['A', 'B', 'C', 'D']\n", + "l2 = ['A', 'B', 'C', 'D']\n", + "\n", + "l1.append('E')\n", + "l2.insert(len(my_list), 'E')\n", + "print (l1)\n", + "print (l2)" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n", + "['A', 'B', 'C', 'D', 'E']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "IIwxDiHpU5e0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "64511bbe-48f4-40c5-b0c0-a7255b1ba446" + }, + "cell_type": "code", + "source": [ + "# Length of the list\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "print (len(l1))" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "8Yfq2doDU5mA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "7fe88120-fd28-4e2a-e6ea-f51f5594333e" + }, + "cell_type": "code", + "source": [ + "# # Removing elements from list\n", + "# del can remove any element from list as long as you know its index\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "print (l1)\n", + "\n", + "del l1[0]\n", + "print (l1)\n", + "\n", + "del l1[-1]\n", + "print (l1)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['A', 'B', 'C', 'D', 'E']\n", + "['B', 'C', 'D', 'E']\n", + "['B', 'C', 'D']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "mz4k7WsGVBKV", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# pop() can remove the last element from list when used without any arguments\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "# pop() returns the last element, so c would store the popped element\n", + "c = l1.pop()\n", + "\n", + "print (l1)\n", + "print (c) " + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "pnx8Vd3pVBNl", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# pop(n) -> Removes the element at index 'n' and returns it\n", + "l1 = ['A', 'B', 'C', 'D', 'E']\n", + "\n", + "# Removes the element at 0 position and returns it\n", + "c = l1.pop(0)\n", + "print (l1)\n", + "print (c)\n", + "\n", + "# Works as expected with negetive indexes\n", + "c = l1.pop(-1)\n", + "print (l1)\n", + "print (c)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "fYbtwRaEVBQm", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Removing an item by value\n", + "# remove() only removes the first occurence of the value that is specified.\n", + "q1 = [\"Seriously, \", \"what\", \"happened\", \"to\", \"Jennifer\", \"and\", \"Jennifer\", \"?\"]\n", + "print (q1)\n", + "\n", + "q1.remove (\"Jennifer\")\n", + "print (q1)\n", + "\n", + "n1 = \"and\"\n", + "q1.remove(n1)\n", + "print (q1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "tv28EostVBTX", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Sorting a list\n", + "# sort() -> sorts list in increasing or decreasing order, *permantantly*\n", + "# Sorts in alphabetical order\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.sort()\n", + "print (l1)\n", + "\n", + "# Sorts in increasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "l2.sort()\n", + "print (l2)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "LFD_Ah7_VBVl", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Reverse sorts alphabetical order\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.sort(reverse=True)\n", + "print (l1)\n", + "\n", + "# Sorts in decreasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "l2.sort(reverse=True)\n", + "print (l2)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "oAlkQ0ODVOcO", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# sorted() -> Sorts list in increasing or decreasing order, *temporarily*\n", + "# Sorts in increasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "print (l2)\n", + "print (sorted(l2))\n", + "print (l2)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "JJJw4V5XVOfk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Sorts in decreasing order\n", + "l2 = [2, 200, 16, 4, 1, 0, 9.45, 45.67, 90, 12.01, 12.02]\n", + "print (l2)\n", + "print (sorted(l2, reverse=True))\n", + "print (l2)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "wPqvXipbVOjM", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Reverse list\n", + "l1 = ['E', 'D', 'C', 'B', 'A']\n", + "l1.reverse()\n", + "print (l1)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0FLvBlN6VV_t", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# What happens if we directly assign one list to the other instead of using slices\n", + "l1 = [\"Jennifer\", \"now\", \"wonders\", \"what\", \"happens\", \"if\", \"we\", \"directly\", \"assign.\"]\n", + "l2 = l1\n", + "l2.append(\"Both variables point to the same list\")\n", + "\n", + "print (l1)\n", + "print (l2)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "5YMprRhKVOmO", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 4c8cc8d1a6c69bd2a7ac381a5029069d77cd6ffc Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:19:13 +0530 Subject: [PATCH 7/9] Created using Colaboratory --- tuples.ipynb | 214 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 tuples.ipynb diff --git a/tuples.ipynb b/tuples.ipynb new file mode 100644 index 0000000..b200274 --- /dev/null +++ b/tuples.ipynb @@ -0,0 +1,214 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled10.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/tuples.ipynb)" + ] + }, + { + "metadata": { + "id": "sRe4gd82V5sR", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "afc380d6-119f-4e70-f4e0-b970056d271e" + }, + "cell_type": "code", + "source": [ + "# Simple Tuples\n", + "# Tuples are list, the elements stored in which cannot be changed.\n", + "t1 = (23, 45)\n", + "print (t1)\n", + "print (t1[1])" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(23, 45)\n", + "45\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "gtKBsqwsV-0V", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "e0b1105f-dd6e-4b75-f94a-8de82369d8e6" + }, + "cell_type": "code", + "source": [ + "# Can have more than two elements\n", + "t1 = (1, 2, 3, 4, 5, 6, 7)\n", + "print (t1)\n", + "print (t1[4])" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(1, 2, 3, 4, 5, 6, 7)\n", + "5\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "YNd9riUoV-7E", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "58d94f0f-f879-40bf-9368-ef6e0b08f9a2" + }, + "cell_type": "code", + "source": [ + "# Cannot change the elements stored as compared to lists\n", + "l1 = [1, 2, 3, 4, 5, 6, 7]\n", + "t1 = (1, 2, 3, 4, 5, 6, 7)\n", + "\n", + "# Can change elements in list\n", + "l1[4] = 1\n", + "# Cannot change elements in tuple (Uncomment it to see the error)\n", + "#t1[4] = 1\n", + "\n", + "print (l1)\n", + "print (t1)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 1, 6, 7]\n", + "(1, 2, 3, 4, 5, 6, 7)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "UVQ6PgWPV--M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 106 + }, + "outputId": "3071d33f-4b08-4734-d0ef-476726ff0607" + }, + "cell_type": "code", + "source": [ + "# Looping over a tuple\n", + "t1 = (34, 12, 56, 78, 89)\n", + "for t in t1:\n", + " print (t)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34\n", + "12\n", + "56\n", + "78\n", + "89\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "Nwe4BL6LV_At", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "f58fc874-88f8-4ef9-a18e-67989852734d" + }, + "cell_type": "code", + "source": [ + "# Writing over a tuple\n", + "# One cannot change the elements stored in the tuple. \n", + "# But you can assign a new tuple to the variable that stores the tuple you wanna change.\n", + "t1 = (23, 12, 45, 78)\n", + "print (t1)\n", + "\n", + "t1 = (45, 67) # t1 changed to new value\n", + "print (t1)" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "(23, 12, 45, 78)\n", + "(45, 67)\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "MZJESEkZV_D3", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "AlZlDeSHV_Gk", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 8196b6d4271bd9cd754caaa95ca7dac8db7c1d89 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:32:00 +0530 Subject: [PATCH 8/9] Created using Colaboratory --- function2.ipynb | 666 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 666 insertions(+) create mode 100644 function2.ipynb diff --git a/function2.ipynb b/function2.ipynb new file mode 100644 index 0000000..c2ee217 --- /dev/null +++ b/function2.ipynb @@ -0,0 +1,666 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled12.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/function2.ipynb)" + ] + }, + { + "metadata": { + "id": "p788-p9pXu2V", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "9b4791d1-d6f1-4a1f-9b0b-b04923d6267e" + }, + "cell_type": "code", + "source": [ + "# Simple function\n", + "# Function definition\n", + "def a_func():\n", + " print (\"A Message from the other world!\")\n", + "\n", + "# Function Call\n", + "a_func()\n", + "a_func()" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "A Message from the other world!\n", + "A Message from the other world!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "tfqEZe2NX3IV", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "ceb58163-b95d-4bbd-c146-a357827c7706" + }, + "cell_type": "code", + "source": [ + "# Passing arguments to functions\n", + "def add_two(num):\n", + " print (int(num)+2)\n", + "\n", + "add_two (3)\n", + "add_two (\"45\") # This will work as we are casting the passed parameter to Integer before adding" + ], + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": [ + "5\n", + "47\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "4Qk-_toLX3LK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 159 + }, + "outputId": "baa2ecb9-6faa-4782-f74d-d49e9173f729" + }, + "cell_type": "code", + "source": [ + "# Multiple arguments\n", + "def add_sub(add_two_to_this, sub_two_from_this):\n", + " print (\"Added 2 to : \" + str(add_two_to_this))\n", + " print (\"Answer: \" + str(int(add_two_to_this)+2))\n", + " \n", + " print (\"Subtracted 3 from : \" + str(sub_two_from_this))\n", + " print (\"Answer: \" + str(int(sub_two_from_this)-2))\n", + " \n", + "add_sub (45, \"67\")\n", + "add_sub (\"-156745\", 12131)" + ], + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Added 2 to : 45\n", + "Answer: 47\n", + "Subtracted 3 from : 67\n", + "Answer: 65\n", + "Added 2 to : -156745\n", + "Answer: -156743\n", + "Subtracted 3 from : 12131\n", + "Answer: 12129\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "majAOadPX3Qr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "e0c32404-aeb6-4978-ced5-983263c5a39f" + }, + "cell_type": "code", + "source": [ + "# Ordering of passed argument matters\n", + "def coding_in(name, language):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(\"Jennifer\", \"Python\")\n", + "# If you change the order, results are unexpected\n", + "coding_in(\"Python\", \"Jennifer\")" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Python is coding in Jennifer\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "QtVDuJs3X3Ta", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "2db224e9-a490-4427-d9db-267c27ffb9a5" + }, + "cell_type": "code", + "source": [ + "# Keyword Arguments\n", + "# Pass the argumnets as key:value pairs, so even if unordered it won't produce unexpected results\n", + "def coding_in(name, language):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(name=\"Jennifer\", language=\"Python\")\n", + "# If you change the order, results are same\n", + "coding_in(language=\"Python\", name=\"Jennifer\")" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Jennifer is coding in Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "uoJf0Or2X3XT", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "c808a958-9561-49bb-cd76-d39340d47465" + }, + "cell_type": "code", + "source": [ + "# Keyword Arguments\n", + "# Pass the argumnets as key:value pairs, so even if unordered it won't produce unexpected results\n", + "def coding_in(name, language):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(name=\"Jennifer\", language=\"Python\")\n", + "# If you change the order, results are same\n", + "coding_in(language=\"Python\", name=\"Jennifer\")" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Jennifer is coding in Python\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "aamcZyjBYGiF", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "805d72aa-8c5c-40fd-9adc-7fe1e0a6244e" + }, + "cell_type": "code", + "source": [ + "# Default Values for parameters\n", + "# Note: If you do not pass arguments required by the function and that argument does not have a default value, \n", + "# then python will throw an error\n", + "def coding_in(name, language=\"Python\"):\n", + " print (str(name) + \" is coding in \" + str(language))\n", + " \n", + "coding_in(\"Jennifer\") # Since 2nd argument is not passed, it takes on the default parameter given\n", + "coding_in(\"Jennifer\", \"R\") # Since 2nd argument is passed, it takes on the passed arguemnt" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python\n", + "Jennifer is coding in R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "iI5zQA9-YGlN", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 132 + }, + "outputId": "37242474-7bcf-46f6-f812-8b2700c1615a" + }, + "cell_type": "code", + "source": [ + "# Note: You cannot have parameters with no default value after parameters having default value\n", + "# Following is an error\n", + "def coding_in(name, language1=\"Python\", language2):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in(\"Jennifer\", \"Python\", \"R\")" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def coding_in(name, language1=\"Python\", language2):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m non-default argument follows default argument\n" + ] + } + ] + }, + { + "metadata": { + "id": "g4f_ibACYGpQ", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "bd957654-98d4-445f-c442-675a003a0a68" + }, + "cell_type": "code", + "source": [ + "# Easy fix to above error is to declare all non-default parameters, \n", + "# and then start declaring the default parameters\n", + "# Note: Default parameters can be used to make an argument optional\n", + "def coding_in(name, language2, language1=\"Python\"):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in(\"Jennifer\", \"Python\", \"R\")\n", + "coding_in(\"Jennifer\", \"R\")" + ], + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in R and Python\n", + "Jennifer is coding in Python and R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "qQ8E42ORYGsa", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "ff662df1-e905-43c0-c592-c9800309bc45" + }, + "cell_type": "code", + "source": [ + "# All parameters can be default\n", + "def coding_in(name=\"Jennifer\", language1=\"Python\", language2=\"R\"):\n", + " print (str(name) + \" is coding in \" + str(language1) + \" and \" + str(language2))\n", + "\n", + "coding_in()" + ], + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer is coding in Python and R\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "5FEYW8ctYGu9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "2ebd6b6e-4ddc-4690-b7ae-004ae53d1422" + }, + "cell_type": "code", + "source": [ + "# return\n", + "def pow_4(num):\n", + " return num**4\n", + "\n", + "v1 = pow_4(34) # v1 now stores 34^4\n", + "v2 = pow_4(23) # v2 now stores 23^4\n", + "\n", + "print (\"34^4 = \" + str(v1))\n", + "print (\"23^4 = \" + str(v2))" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "34^4 = 1336336\n", + "23^4 = 279841\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "0mHJG6JTYZy9", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + }, + "outputId": "8bddd267-2ec6-4863-ac17-f822d9a9def8" + }, + "cell_type": "code", + "source": [ + "# can return any data type or object from function\n", + "def make_a_coder(name, age, language):\n", + " d1 = {'name': name, \n", + " 'age' : age,\n", + " 'language' : language}\n", + " return d1\n", + "\n", + "print(make_a_coder(\"Jennifer\", \"21\", \"Python\"))" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'name': 'Jennifer', 'age': '21', 'language': 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "wC-H8jIEYZ2I", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 124 + }, + "outputId": "faa639a5-67dd-470c-b6d1-b59af1273721" + }, + "cell_type": "code", + "source": [ + "# can pass any data type or object to function\n", + "def make_many_coders(list_of_coders):\n", + " print (\"Name \\tLanguage \\tAge\")\n", + " print (\"========\\t==========\\t===\")\n", + " for coder,details in list_of_coders.items():\n", + " print (str(coder) + \"\\t\" + str(details[0]) + \"\\t\\t\" + str(details[-1]))\n", + " \n", + " return str(len(list_of_coders))\n", + "\n", + "d1 = {\"Jennifer\": [\"Python\", 21],\n", + " \"Scarlett\": [\"R\", 21]}\n", + "print ('\\n' + make_many_coders(d1) + \" coders found!\")" + ], + "execution_count": 13, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Name \tLanguage \tAge\n", + "========\t==========\t===\n", + "Jennifer\tPython\t\t21\n", + "Scarlett\tR\t\t21\n", + "\n", + "2 coders found!\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "nOYgiwE2YZ4w", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "ddca1bf2-870a-4649-b2e3-36f14f41ecea" + }, + "cell_type": "code", + "source": [ + "# If a list passed to a function is changed inside the function then the change is permanent\n", + "def make_language_list(language_list, new_language):\n", + " language_list.append(new_language)\n", + " \n", + "lang_list = [\"Python\", \"R\"]\n", + "print (lang_list)\n", + "\n", + "make_language_list(lang_list, \"Julia\")\n", + "print (lang_list)" + ], + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Python', 'R']\n", + "['Python', 'R', 'Julia']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "sIyI1iryYZ9W", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "f73700ec-c048-4700-f42e-2376853f9a05" + }, + "cell_type": "code", + "source": [ + "# Preventing a function from modifying a list\n", + "def make_language_list(language_list, new_language):\n", + " language_list.append(new_language)\n", + " \n", + "lang_list = [\"Python\", \"R\"]\n", + "print (lang_list)\n", + "\n", + "make_language_list(lang_list[:], \"Julia\")\n", + "print (lang_list)" + ], + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['Python', 'R']\n", + "['Python', 'R']\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "v1W062zAYvo1", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "4263889b-233c-4f6e-c00f-af2e6cd90f9d" + }, + "cell_type": "code", + "source": [ + "# Passing Arbitrary number of arguments\n", + "# The '*' tells Python to make a tuple of whatever number of arguments it receives at that position\n", + "def languages(*many_languages): \n", + " print (many_languages)\n", + " \n", + "languages (\"Python\")\n", + "languages (\"Python\", \"R\", \"Julia\", \"Ruby\", \"Go\")" + ], + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": [ + "('Python',)\n", + "('Python', 'R', 'Julia', 'Ruby', 'Go')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "SZb6aXkEYvsL", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "1a7926db-c19a-4511-f8b6-95c4eafaba16" + }, + "cell_type": "code", + "source": [ + "# Passing Arbitrary number of arguments with a normal argument\n", + "# Note: The parameter that accepts arbitrary number of arguments needs to be placed at last\n", + "def knows_languages(name, *languages):\n", + " print (str(name) + \" knows: \" + str(languages))\n", + " \n", + "knows_languages(\"Jennifer\", \"Python\")\n", + "knows_languages(\"Jennifer\", \"Python\", \"R\", \"Julia\", \"Ruby\")" + ], + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer knows: ('Python',)\n", + "Jennifer knows: ('Python', 'R', 'Julia', 'Ruby')\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "lyf56abRYvvU", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 132 + }, + "outputId": "58054ae1-5052-476e-cf22-0102b58b4259" + }, + "cell_type": "code", + "source": [ + "# Note: You cannot have two or more parameters that accepts arbitrary number of arguments\n", + "# Hence, following is an error!\n", + "def knows_languages_and_modules(name, *languages, *modules):\n", + " print (str(name) + \" knows: \" + str(languages))\n", + " \n", + "knows_languages(\"Jennifer\", \"Python\", \"PyGtk\")\n", + "knows_languages(\"Jennifer\", \"Python\", \"R\", \"Julia\", \"Ruby\", \"PyGtk\", \"PyGame\", \"audioop\")" + ], + "execution_count": 18, + "outputs": [ + { + "output_type": "error", + "ename": "SyntaxError", + "evalue": "ignored", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m def knows_languages_and_modules(name, *languages, *modules):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" + ] + } + ] + }, + { + "metadata": { + "id": "rcoIEGu8YZ7x", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "866dbd2d-8494-4351-e066-928b3156d76a" + }, + "cell_type": "code", + "source": [ + "# Using Arbitrary Keyword Argument\n", + "# Note: '**' tells Python to create a dictionary of the extra arguments passed after the first required positional argument\n", + "def make_a_coder(name, **details):\n", + " coder = {}\n", + " coder[\"name\"] = name;\n", + " for key, value in details.items():\n", + " coder[key] = value\n", + " return coder\n", + "\n", + "print (make_a_coder(\"Jennifer\", location=\"California\", age=\"21\", language=(\"Python\", \"R\")))\n", + "\n", + "# We can do this since we are using keyword arguments\n", + "print (make_a_coder(location=\"California\", age=\"21\", language=(\"Python\", \"R\"), name=\"Jennifer\"))" + ], + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{'name': 'Jennifer', 'location': 'California', 'age': '21', 'language': ('Python', 'R')}\n", + "{'name': 'Jennifer', 'location': 'California', 'age': '21', 'language': ('Python', 'R')}\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file From 2778ea1a0c7d67cf3692fc814bef2ab7799b5281 Mon Sep 17 00:00:00 2001 From: MONA7584908095 <43202653+MONA7584908095@users.noreply.github.com> Date: Tue, 25 Sep 2018 16:35:18 +0530 Subject: [PATCH 9/9] Created using Colaboratory --- classes3.ipynb | 382 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 382 insertions(+) create mode 100644 classes3.ipynb diff --git a/classes3.ipynb b/classes3.ipynb new file mode 100644 index 0000000..08c44c1 --- /dev/null +++ b/classes3.ipynb @@ -0,0 +1,382 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Untitled13.ipynb", + "version": "0.3.2", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "[View in Colaboratory](https://colab.research.google.com/github/MONA7584908095/Assignment-1/blob/MONA7584908095/classes3.ipynb)" + ] + }, + { + "metadata": { + "id": "ZnZzBz9GZPQE", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Simple class\n", + "class Programmer():\n", + " \"\"\"This is called a docstring. This class is to create a Programmer. \n", + " Functions inside a class is called a method.\n", + " Methods are automatically passed the 'self' argument. \n", + " Any variable prefixed with 'self.' is available to the class.\n", + " We will also be able to access this self prefixed variables from any instance of the class.\"\"\"\n", + " \n", + " def __init__(self, name, age, *known_languages):\n", + " \"\"\"__init__ is a special method that Python automatically calls \n", + " when a new instance of the class is created. \"\"\"\n", + " self.name = name\n", + " self.age = age\n", + " self.languages = set(known_languages)\n", + " \n", + " # Default value for a class variable\n", + " self.concepts_revised = 0\n", + " \n", + " def add_new_language(self, lang):\n", + " self.languages.add(lang)\n", + " print (str(self.name) + \" knows a new language : \" + str(lang) + \" !!\")\n", + " \n", + " def revise_concept(self, concept):\n", + " self.concepts_revised += 1\n", + " print (str(self.name) + \" just revised \" + str(concept) + \" !!\")\n", + " \n", + " def languages_known(self):\n", + " return list(self.languages)\n", + " \n", + " def cv(self):\n", + " print (\"Name : \" + str(self.name))\n", + " print (\"Age : \" + str(self.age))\n", + " print (\"Skills : \" + str(self.languages))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "1ykzzvMuZXiT", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Creating an instance of the class\n", + "# This calls the __init__() method\n", + "a_programmer = Programmer(\"Jennifer\", 21, \"Python\", \"R\", \"Julia\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "c7k-mHYZZYjR", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "a507627c-4bc7-4d7e-9cac-db360977c8f6" + }, + "cell_type": "code", + "source": [ + "# Accessing the attributes\n", + "print (a_programmer.name.title())\n", + "print (a_programmer.age)\n", + "print (a_programmer.languages)" + ], + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer\n", + "21\n", + "{'R', 'Julia', 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "2FdZVbPzZbYI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 159 + }, + "outputId": "9b7438d9-cfb4-4d95-8a6f-a2612aa61d4c" + }, + "cell_type": "code", + "source": [ + "# Calling Methods\n", + "a_programmer.add_new_language(\"Ruby\")\n", + "print (a_programmer.languages)\n", + "\n", + "print (\"\\nCV for \" + str(a_programmer.name.title()) + \"\\n=================\")\n", + "a_programmer.cv()" + ], + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer knows a new language : Ruby !!\n", + "{'R', 'Julia', 'Ruby', 'Python'}\n", + "\n", + "CV for Jennifer\n", + "=================\n", + "Name : Jennifer\n", + "Age : 21\n", + "Skills : {'R', 'Julia', 'Ruby', 'Python'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "EKKtMw1CZbbN", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 230 + }, + "outputId": "1a42c8a1-4c0c-417c-b9ef-1ef189dc62db" + }, + "cell_type": "code", + "source": [ + "# Creating multiple instances\n", + "b_programmer = Programmer (\"Scarlett\", 21, \"Python\", \"Julia\", \"SPLUS\", \"Ruby\")\n", + "c_programmer = Programmer (\"Ariel\", 20, \"C++\", \"Java\", \"Python\")\n", + "\n", + "print (\"\\nCV for \" + str(b_programmer.name.title()) + \"\\n=================\")\n", + "b_programmer.cv()\n", + "\n", + "print (\"\\nCV for \" + str(c_programmer.name.title()) + \"\\n=================\")\n", + "c_programmer.cv()" + ], + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "CV for Scarlett\n", + "=================\n", + "Name : Scarlett\n", + "Age : 21\n", + "Skills : {'Ruby', 'Julia', 'SPLUS', 'Python'}\n", + "\n", + "CV for Ariel\n", + "=================\n", + "Name : Ariel\n", + "Age : 20\n", + "Skills : {'C++', 'Python', 'Java'}\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "VIriP2DEZbfH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 52 + }, + "outputId": "b87b62c1-eff2-4b8d-a95a-d7b2362af530" + }, + "cell_type": "code", + "source": [ + "# Directly modifying Attribute's value\n", + "print (\"Concepts Revised by \" + str(b_programmer.name.title()) + \" : \" + str(b_programmer.concepts_revised))\n", + "b_programmer.concepts_revised += 10\n", + "print (\"Concepts Revised by \" + str(b_programmer.name.title()) + \" : \" + str(b_programmer.concepts_revised))" + ], + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Concepts Revised by Scarlett : 0\n", + "Concepts Revised by Scarlett : 10\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "an1e71GYZYmI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 124 + }, + "outputId": "3df7ca6a-2913-4afb-c6e6-aa9d85bdebfe" + }, + "cell_type": "code", + "source": [ + "# Modifying Attribute's value through a method\n", + "print (\"\\nConcepts Revised by \" + str(c_programmer.name.title()) + \" : \" + str(c_programmer.concepts_revised))\n", + "c_programmer.revise_concept(\"Python Lists\")\n", + "c_programmer.revise_concept(\"Python Tuples\")\n", + "c_programmer.revise_concept(\"Python Dictionaries\")\n", + "print (\"Concepts Revised by \" + str(c_programmer.name.title()) + \" : \" + str(c_programmer.concepts_revised))" + ], + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": [ + "\n", + "Concepts Revised by Ariel : 0\n", + "Ariel just revised Python Lists !!\n", + "Ariel just revised Python Tuples !!\n", + "Ariel just revised Python Dictionaries !!\n", + "Concepts Revised by Ariel : 3\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "KfrJErkDZYn5", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Inheritance\n", + "'''\n", + "Programmer => Parent Class\n", + "Developer => Child Class\n", + "\n", + "Child class inherits from the base class.\n", + "The classes share a IS A relationship.\n", + "So, in this case, Developer IS A Programmer.\n", + "It has available all methods and variables from the parent class.\n", + "And can define methods and variables of its own.\n", + "'''\n", + "class Developer(Programmer):\n", + " def __init__(self, name, age, expertise, yoe, *known_languages):\n", + " \n", + " # Call the parent class to initialize and give the child class an instance of the parent\n", + " super().__init__(name, age, known_languages)\n", + " self.expertise = expertise\n", + " self.years_of_experience = yoe\n", + " \n", + " def specializes_in(self):\n", + " return self.expertise\n", + " \n", + " def cv(self):\n", + " \"\"\"\n", + " This method overrides the cv() method in the parent class.\n", + " Any method in child class with same name as a method inherited from parent class\n", + " overrides the parent class method.\n", + " \"\"\"\n", + " print (\"Name : \" + str(self.name))\n", + " print (\"Age : \" + str(self.age))\n", + " print (\"Skills : \" + str(self.languages))\n", + " print (\"Expertise : \" + str(self.expertise))\n", + " print (\"Years of Experience : \" + str(self.years_of_experience))" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "0usPkZaaZYqA", + "colab_type": "code", + "colab": {} + }, + "cell_type": "code", + "source": [ + "# Creating an instance of the child class\n", + "a_developer = Developer (\"Jennifer\", 21, \"Android\", 2, \"Java\", \"Kotlin\", \"Python\", \"R\")" + ], + "execution_count": 0, + "outputs": [] + }, + { + "metadata": { + "id": "PwfqjX9PZYt4", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "3372e312-3fe6-4099-f53a-2d50ebbd0501" + }, + "cell_type": "code", + "source": [ + "# Call to methods and variables from the child as well as parent class\n", + "print (str(a_developer.name) + \" specializes in \" + str(a_developer.specializes_in()) + \".\")\n", + "print (str(a_developer.name) + \" can code in \" + str(a_developer.languages_known()) + \".\")\n", + "print (str(a_developer.name) + \" has \" + str(a_developer.years_of_experience) + \" years of experience.\")" + ], + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Jennifer specializes in Android.\n", + "Jennifer can code in [('Java', 'Kotlin', 'Python', 'R')].\n", + "Jennifer has 2 years of experience.\n" + ], + "name": "stdout" + } + ] + }, + { + "metadata": { + "id": "iWeZVrPeZvLb", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 106 + }, + "outputId": "6c0258ff-ab0a-4b24-e35f-532311c233ab" + }, + "cell_type": "code", + "source": [ + "# Calling the overriden method in the child class\n", + "a_developer.cv()" + ], + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Name : Jennifer\n", + "Age : 21\n", + "Skills : {('Java', 'Kotlin', 'Python', 'R')}\n", + "Expertise : Android\n", + "Years of Experience : 2\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file