Created
September 26, 2022 07:22
-
-
Save divx1979/6d2ffdcf62dd51115243661b3a8c8de3 to your computer and use it in GitHub Desktop.
PYTHON_inter.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "collapsed_sections": [], | |
| "authorship_tag": "ABX9TyMqJP3mZEJi85+HdBkQmhcS", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/divx1979/6d2ffdcf62dd51115243661b3a8c8de3/python_inter.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "## PY FUNCTIONS" | |
| ], | |
| "metadata": { | |
| "id": "vlDAsWb8C0je" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "id": "0f_wiGGDu5eq" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import pandas as pd" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def is_leap(year):\n", | |
| " leap = False\n", | |
| " \n", | |
| " # Write your logic here\n", | |
| " if year % 4 == 0 & year % 100 != 0:\n", | |
| " leap = True\n", | |
| " elif year % 400 == 0:\n", | |
| " leap = True\n", | |
| " return leap\n", | |
| "\n", | |
| "year = int(input())\n", | |
| "print(is_leap(year))" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "zyFzV61nvBZI", | |
| "outputId": "da3cee15-2925-4d8c-8f4e-eb93daddad0a" | |
| }, | |
| "execution_count": 8, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "1979\n", | |
| "False\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def is_leap(year):\n", | |
| " leap = False\n", | |
| " \n", | |
| " # Write your logic here\n", | |
| " if year % 4 == 0:\n", | |
| " leap = True\n", | |
| " elif year % 100 == 0:\n", | |
| " leap = False\n", | |
| " elif year % 100 != 0:\n", | |
| " leap = False\n", | |
| " elif year % 400 == 0:\n", | |
| " leap = True\n", | |
| " return leap\n", | |
| "\n", | |
| "year = int(input())\n", | |
| "print(is_leap(year))" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "489CifZrvDKc", | |
| "outputId": "386f09e1-cb01-4f62-928e-fbe64df5b669" | |
| }, | |
| "execution_count": 9, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "1979\n", | |
| "False\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "matrix = []\n", | |
| "\n", | |
| "for i in range(10):\n", | |
| " matrix.append([])\n", | |
| "\n", | |
| " for j in range(10):\n", | |
| " matrix[i].append(j)\n", | |
| "\n", | |
| "print(matrix)" | |
| ], | |
| "metadata": { | |
| "id": "Ze8gdv0gyOeU", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "4c4ab42b-295d-444c-a69b-9484d477a9a5" | |
| }, | |
| "execution_count": 10, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "matrix = [[j for j in range(10)] for i in range(10)]\n", | |
| "\n", | |
| "print(matrix)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "GLYxGwh-zMRD", | |
| "outputId": "d4fbb455-08dd-4509-ab5b-a7865cc8c201" | |
| }, | |
| "execution_count": 11, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "list = []\n", | |
| "\n", | |
| "for k in range(3):\n", | |
| " print(k)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "Se2CqaY_zWxH", | |
| "outputId": "8df56997-776c-4252-d1fe-d1078f11f6de" | |
| }, | |
| "execution_count": 12, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "0\n", | |
| "1\n", | |
| "2\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def odd_eve(num1, num2):\n", | |
| " if num1%num2 == 0:\n", | |
| " print('EVEN')\n", | |
| " else:\n", | |
| " print('ODD')" | |
| ], | |
| "metadata": { | |
| "id": "Y14Uc433DraX" | |
| }, | |
| "execution_count": 13, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "odd_eve(2,5)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "otG77qvNEIwO", | |
| "outputId": "e479bf4e-7a90-4c9b-ab42-18d428431644" | |
| }, | |
| "execution_count": 14, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "ODD\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "odd_eve(40, 4)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "Bcd5wMu0EsIH", | |
| "outputId": "e98953a0-c121-4b7e-faeb-94858d166eac" | |
| }, | |
| "execution_count": 15, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "EVEN\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def add_listo(lst):\n", | |
| " tot = 0\n", | |
| " for i in lst:\n", | |
| " print(i)\n", | |
| " tot = tot + int(i)\n", | |
| " return tot" | |
| ], | |
| "metadata": { | |
| "id": "YWGhtXc8EydU" | |
| }, | |
| "execution_count": 16, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "add_listo([2, 4, 6, 10])" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "xhWJrwSrGFaf", | |
| "outputId": "dd0ba11c-616b-4ef8-b131-1ee05b7d4a71" | |
| }, | |
| "execution_count": 17, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "2\n", | |
| "4\n", | |
| "6\n", | |
| "10\n" | |
| ] | |
| }, | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "22" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 17 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Positional Argument\n", | |
| "# Keyword Argument\n", | |
| "\n", | |
| "def me(name, age = 42):\n", | |
| " print('my name is {}. my age is {}'.format(name, age))" | |
| ], | |
| "metadata": { | |
| "id": "n2Qkf9TdGYMl" | |
| }, | |
| "execution_count": 18, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "me('Div')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "YHv1HWnoHpan", | |
| "outputId": "a42e39fb-fd0f-42cd-c033-b3ce28bb5b4e" | |
| }, | |
| "execution_count": 19, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "my name is Div. my age is 42\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst1 = [1, 2, 3, 4, 5 ,6, 8, 10, 12, 14]" | |
| ], | |
| "metadata": { | |
| "id": "aqpptWPBJQgY" | |
| }, | |
| "execution_count": 20, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def oddpluseven(lst):\n", | |
| " odd = 0\n", | |
| " even = 0\n", | |
| " for i in lst:\n", | |
| " if i%2 == 0:\n", | |
| " even = even + i\n", | |
| " else:\n", | |
| " odd = odd + i\n", | |
| " return even, odd" | |
| ], | |
| "metadata": { | |
| "id": "FojOvacWJWz2" | |
| }, | |
| "execution_count": 21, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "oddpluseven(lst1)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "RFaLTnp9J_yF", | |
| "outputId": "a7e98e90-5f18-4467-d27c-e49db6c8ec02" | |
| }, | |
| "execution_count": 22, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "(56, 9)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 22 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Mapping" | |
| ], | |
| "metadata": { | |
| "id": "2JELiDe7KC_8" | |
| }, | |
| "execution_count": 23, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def oddoreven(num):\n", | |
| " if num % 2 == 0:\n", | |
| " print('Even')\n", | |
| " else:\n", | |
| " print('odd')" | |
| ], | |
| "metadata": { | |
| "id": "0oaaeLgNVmFg" | |
| }, | |
| "execution_count": 24, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def square(num):\n", | |
| " return num**2" | |
| ], | |
| "metadata": { | |
| "id": "vgX6vnEupAjW" | |
| }, | |
| "execution_count": 25, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "oddoreven(36)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "7SeQGNSDV4WU", | |
| "outputId": "ef583ab3-10c7-47c5-add7-b74596fafaa2" | |
| }, | |
| "execution_count": 26, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Even\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "square(4)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "s5dYVXRzpOek", | |
| "outputId": "2270ce6e-4975-413f-a183-be9af2dbe892" | |
| }, | |
| "execution_count": 27, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "16" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 27 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst = [1, 2, 4, 5, 3, 7, 6, 8, 10, 14, 18]" | |
| ], | |
| "metadata": { | |
| "id": "b2ci0hyuV74E" | |
| }, | |
| "execution_count": 28, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#oddoreven(lst)" | |
| ], | |
| "metadata": { | |
| "id": "xK6f3uh8WFhe" | |
| }, | |
| "execution_count": 29, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# understand that a list cannot be passed through this function so, we can use mapping fn\n", | |
| "\n", | |
| "res = map(oddoreven, lst)" | |
| ], | |
| "metadata": { | |
| "id": "1RJk9_4_WJYX" | |
| }, | |
| "execution_count": 30, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "for i in res:\n", | |
| " print(i)" | |
| ], | |
| "metadata": { | |
| "id": "4knGzvA0pc0i", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "de05864d-63b1-47f4-d71e-5e667e8b1260" | |
| }, | |
| "execution_count": 31, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "odd\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "odd\n", | |
| "None\n", | |
| "odd\n", | |
| "None\n", | |
| "odd\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n", | |
| "Even\n", | |
| "None\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print(res)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "NMRa5IgiZiyC", | |
| "outputId": "3f3cec47-a006-46e4-ce36-a7dc5b17f5ee" | |
| }, | |
| "execution_count": 32, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "<map object at 0x7f474dd8e990>\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Lambda" | |
| ], | |
| "metadata": { | |
| "id": "Xc7RzMsDZ28U" | |
| }, | |
| "execution_count": 33, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Normal function with only 1 line\n", | |
| "\n", | |
| "def square(X):\n", | |
| " return X**2" | |
| ], | |
| "metadata": { | |
| "id": "To_TZDQ3sefI" | |
| }, | |
| "execution_count": 34, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "squared = lambda x:x**2" | |
| ], | |
| "metadata": { | |
| "id": "hGP1VUd4s0MN" | |
| }, | |
| "execution_count": 35, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "squared(4)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "XQkyQtHctABR", | |
| "outputId": "56beaff7-9245-4459-e37f-1fcbb1505942" | |
| }, | |
| "execution_count": 36, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "16" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 36 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def add(x, y):\n", | |
| " return x + y" | |
| ], | |
| "metadata": { | |
| "id": "84EBJPSutFqZ" | |
| }, | |
| "execution_count": 37, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "added = lambda x, y: x + y" | |
| ], | |
| "metadata": { | |
| "id": "lr3HWgUptP4Y" | |
| }, | |
| "execution_count": 38, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "added(10, 2)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "z7ZbPgKMtUYc", | |
| "outputId": "02f66590-1d8a-41f4-9f5d-4b66740adb41" | |
| }, | |
| "execution_count": 39, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "12" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 39 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def odd(x):\n", | |
| " return x%2 != 0" | |
| ], | |
| "metadata": { | |
| "id": "VhOLNYqztaMm" | |
| }, | |
| "execution_count": 40, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "odded = lambda x: x%2 != 0" | |
| ], | |
| "metadata": { | |
| "id": "s-HwLW0-tjXH" | |
| }, | |
| "execution_count": 41, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "odded(2)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "C8aq0UvrtpDy", | |
| "outputId": "49852073-512e-4686-f000-7620c937b4d4" | |
| }, | |
| "execution_count": 42, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "False" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 42 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def even(num):\n", | |
| " if num%2 == 0:\n", | |
| " return True" | |
| ], | |
| "metadata": { | |
| "id": "oLyWpAvwulIj" | |
| }, | |
| "execution_count": 43, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst1 = [1, 2, 4, 6, 5, 7]" | |
| ], | |
| "metadata": { | |
| "id": "FyC6WItCuuht" | |
| }, | |
| "execution_count": 44, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst = filter(even, lst1)" | |
| ], | |
| "metadata": { | |
| "id": "EM0q4K_1uz2W" | |
| }, | |
| "execution_count": 45, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "for num in lst:\n", | |
| " print(num)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "ZapcVVIBvM9m", | |
| "outputId": "2865d7fd-71ed-4ba3-a794-186b4ace8d0b" | |
| }, | |
| "execution_count": 46, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "2\n", | |
| "4\n", | |
| "6\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "filter(lambda num: num%2 == 0, lst1)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "SQodEdo8vyis", | |
| "outputId": "f81de88b-c9c3-4ad7-b6fd-5b372c5e1398" | |
| }, | |
| "execution_count": 47, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "<filter at 0x7f474dd9c810>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 47 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# Fn Practice" | |
| ], | |
| "metadata": { | |
| "id": "aprnhYeoi7wU" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Question: Write a Python function to find the Max of three numbers.\n", | |
| "\n", | |
| "def max_of_2(x, y):\n", | |
| " if x > y:\n", | |
| " return x\n", | |
| " else:\n", | |
| " return y" | |
| ], | |
| "metadata": { | |
| "id": "MVyiqCbGjCxC" | |
| }, | |
| "execution_count": 48, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def max_of_3(x, y, z):\n", | |
| " return max_of_2(x, max_of_2(y, z))" | |
| ], | |
| "metadata": { | |
| "id": "poHwcZdSjCun" | |
| }, | |
| "execution_count": 49, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Example\n", | |
| "\n", | |
| "max_of_3(-3, 2, 1)" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "CY9-MpU_jCCI", | |
| "outputId": "faa62f07-61a1-4598-d8e8-bcca2bad1af8" | |
| }, | |
| "execution_count": 50, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "2" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 50 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Q. Write a finction to sum all the numbers in a list" | |
| ], | |
| "metadata": { | |
| "id": "6G8ef1qrj6TX" | |
| }, | |
| "execution_count": 51, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def sum_list(num):\n", | |
| " tot = 0\n", | |
| " for i in num:\n", | |
| " tot += i\n", | |
| " return tot" | |
| ], | |
| "metadata": { | |
| "id": "v2k3jXbtmXXB" | |
| }, | |
| "execution_count": 52, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "sum_list([1, 2, 3, 6, 8])" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "3LhrV3CdoBYe", | |
| "outputId": "ce5311b4-5f8f-433b-dc46-4c12f09abbb7" | |
| }, | |
| "execution_count": 53, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "20" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 53 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "print('Hello!')" | |
| ], | |
| "metadata": { | |
| "id": "HbnRDe7KwJ6N", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "outputId": "2fe33254-98e2-418d-e65a-a6143b5c78ae" | |
| }, | |
| "execution_count": 54, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "Hello!\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Q. Write a function to multiply all the numbers in a list." | |
| ], | |
| "metadata": { | |
| "id": "TSy0bxtmof9-" | |
| }, | |
| "execution_count": 55, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def product(lst):\n", | |
| " prod = 1\n", | |
| " for x in lst:\n", | |
| " prod *= x\n", | |
| " return prod" | |
| ], | |
| "metadata": { | |
| "id": "ZpV6ZeJIof6o" | |
| }, | |
| "execution_count": 56, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "product([8, 2, 3, -1, 7])" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "zRxi5yaaqS8q", | |
| "outputId": "8526e3e8-5cca-4b02-864e-9dad72f01b0f" | |
| }, | |
| "execution_count": 57, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "-336" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 57 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Q. Write a function to reverse a string." | |
| ], | |
| "metadata": { | |
| "id": "j2H7tH38qdH4" | |
| }, | |
| "execution_count": 58, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def reverse_str(str1):\n", | |
| "\n", | |
| " rstring = ''\n", | |
| " index = len(str1)\n", | |
| "\n", | |
| " while index > 0:\n", | |
| " rstring += str1[index - 1]\n", | |
| " index = index - 1\n", | |
| " return rstring" | |
| ], | |
| "metadata": { | |
| "id": "UcIgWCA2qct4" | |
| }, | |
| "execution_count": 59, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "reverse_str('abx')" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 35 | |
| }, | |
| "id": "CiHxVSqTuHJ4", | |
| "outputId": "8b7b85b4-0ea1-4968-8f61-5372a1937fe1" | |
| }, | |
| "execution_count": 60, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "'xba'" | |
| ], | |
| "application/vnd.google.colaboratory.intrinsic+json": { | |
| "type": "string" | |
| } | |
| }, | |
| "metadata": {}, | |
| "execution_count": 60 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## 25/09/2022\n", | |
| "## List Comprehension" | |
| ], | |
| "metadata": { | |
| "id": "sUVj3n9ChK4t" | |
| }, | |
| "execution_count": 61, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "new_list = []\n", | |
| "\n", | |
| "def lst_square(lst):\n", | |
| " for i in lst:\n", | |
| " new_list.append(i**2)\n", | |
| " return new_list" | |
| ], | |
| "metadata": { | |
| "id": "0_yw1gPQhr41" | |
| }, | |
| "execution_count": 62, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst_square([1, 2, 3])" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "7UPl-4C0igmk", | |
| "outputId": "478159ad-c0cd-4101-9b4a-8a90d291fddf" | |
| }, | |
| "execution_count": 63, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "[1, 4, 9]" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 63 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "##Above you can see that it took me 4 lines of code to get a result, which I can do in just 1 line\n", | |
| "# Definition of List Comprehension\n", | |
| "## A Python list comprehension consists of brackets containing the expression, which \n", | |
| "## is executed for each element along with the for loop to iterate over each element in the Python list\n", | |
| "\n", | |
| "# So in short, here can get a result list from the existing list in just one line of code.\n", | |
| "# This line of code includes the return value(e.g. [i**2], followed by the iteration loop elem)" | |
| ], | |
| "metadata": { | |
| "id": "KdlKJ4ULikw-" | |
| }, | |
| "execution_count": 64, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "lst = [1, 2, 3, 6, 7, 9]" | |
| ], | |
| "metadata": { | |
| "id": "VbVzngYTjF4n" | |
| }, | |
| "execution_count": 65, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "[i**2 for i in lst]" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "JvttXMCCjOsF", | |
| "outputId": "945d0b1d-3d7c-4ba3-ca85-da7fb9244875" | |
| }, | |
| "execution_count": 66, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "[1, 4, 9, 36, 49, 81]" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 66 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "## Now, I can also add an if clause" | |
| ], | |
| "metadata": { | |
| "id": "CRnD3Gtoqd0P" | |
| }, | |
| "execution_count": 69, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "#e.g : only oeven numbers in lst\n", | |
| "\n", | |
| "[i**2 for i in lst if i%2==0]" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "RIB2rWJmrD25", | |
| "outputId": "f45e4a99-6248-4125-c4cd-be24456d9331" | |
| }, | |
| "execution_count": 70, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "[4, 36]" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 70 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [], | |
| "metadata": { | |
| "id": "qeekHqR7rSg_" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment