{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Python Language Basics.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "zmaU6m5E4FGx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "e2d8094c-e702-4f8f-a494-498a93fb845d"
      },
      "source": [
        "# 3\n",
        "# Bool\n",
        "is_game_over = False\n",
        "print(is_game_over)\n",
        "is_game_over = True\n",
        "print(is_game_over)\n",
        "is_game_over = 5 > 6\n",
        "print(is_game_over)"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n",
            "True\n",
            "False\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fmd-XQCb9YEY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "0465ee2e-c9aa-4fdf-c9bf-9db14502a842"
      },
      "source": [
        "# int\n",
        "num_lives = 5\n",
        "print(num_lives)\n",
        "# float\n",
        "percent_health = 0.5\n",
        "print(percent_health)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "5\n",
            "0.5\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-H81ZQKq98i0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "58414872-8fcb-425a-dec4-5a88f24d4dae"
      },
      "source": [
        "# str\n",
        "player_name = \"Nimish\"\n",
        "print(player_name)\n",
        "player_name = 'Mammoth'\n",
        "print(player_name)"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Nimish\n",
            "Mammoth\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yn0_sGUI-WBb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "738f8e5b-718b-459d-9eed-673747d18193"
      },
      "source": [
        "# type\n",
        "print(type(is_game_over))\n",
        "print(type(num_lives))\n",
        "print(type(percent_health))\n",
        "print(type(player_name))\n",
        "\n",
        "num_lives = \"5\"\n",
        "print(type(num_lives))"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<class 'bool'>\n",
            "<class 'int'>\n",
            "<class 'float'>\n",
            "<class 'str'>\n",
            "<class 'str'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NG2ZRpVQKUnf",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "ade17083-adf0-4044-8025-4df5dc603045"
      },
      "source": [
        "# 4\n",
        "# Converting to str\n",
        "num_lives = 5\n",
        "# num_lives = \"5\"\n",
        "str_num_lives = str(num_lives)\n",
        "print(type(num_lives))\n",
        "print(type(str_num_lives))"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<class 'int'>\n",
            "<class 'str'>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7Y5QCx2yK-do",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "399c2efa-76d4-4f31-c608-4d4fa2e0ebe4"
      },
      "source": [
        "# Converting to bool\n",
        "print(bool(0))\n",
        "print(bool(0.1))\n",
        "print(bool(\"afsdafsd\"))\n",
        "print(bool(\"False\"))"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n",
            "True\n",
            "True\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mpG8ww_BLVsy",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "73915fa8-7927-4b53-d9dc-289da2f4bd40"
      },
      "source": [
        "# Converting to int\n",
        "# print(int(\"fasdfsd\"))\n",
        "print(int(\"1\"))\n",
        "print(int(0.5))\n",
        "print(int(False))\n",
        "print(int(True))"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1\n",
            "0\n",
            "0\n",
            "1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QWyvYhrcL5Tt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "558a2c22-5d63-43b2-f1d4-fdc9328422ba"
      },
      "source": [
        "# Converting to float\n",
        "print(float(\"1\"))\n",
        "print(float(5))\n",
        "print(float(False))\n",
        "print(float(True))"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1.0\n",
            "5.0\n",
            "0.0\n",
            "1.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4cBro1qUMqjb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "87fa6511-4b7b-43c7-8b50-2161251a9b86"
      },
      "source": [
        "# 6\n",
        "# Assignment operators\n",
        "health = 50\n",
        "# Arithmetic operators\n",
        "new_health = health + 20\n",
        "print(health)\n",
        "health = health + 20\n",
        "print(health)\n",
        "\n",
        "xPos = 5\n",
        "print(xPos % 2)\n",
        "print(xPos // 2)\n",
        "print(xPos ** 2)\n",
        "print(xPos)\n",
        "\n",
        "first_name = \"Nimish\"\n",
        "last_name = \"Narang\"\n",
        "print(first_name + \" \" + last_name)"
      ],
      "execution_count": 38,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "50\n",
            "70\n",
            "1\n",
            "2\n",
            "25\n",
            "5\n",
            "Nimish Narang\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3vTmsa-PiNGC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "942944e1-961d-46d2-d795-f4b096367867"
      },
      "source": [
        "# Assignment-arithmetic operators\n",
        "health = 50\n",
        "health += 20\n",
        "health -= 50\n",
        "print(health)\n",
        "\n",
        "name = \"Nimish\"\n",
        "name += \" Narang\"\n",
        "print(name)"
      ],
      "execution_count": 41,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "20\n",
            "Nimish Narang\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yaf8agx1oBsL",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "ed78b5a5-66e5-462f-dbd7-dc9a52b50e1b"
      },
      "source": [
        "# 7\n",
        "# Conditional operators: > >= < <= == !=\n",
        "result = 5 > 2\n",
        "print(result)\n",
        "print(type(result))\n",
        "print(5 == 2)\n",
        "print(5 != health)\n",
        "print(5.0 == 5)"
      ],
      "execution_count": 54,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True\n",
            "<class 'bool'>\n",
            "False\n",
            "True\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rUkCmsm4piH6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "95229d41-7543-49e0-b407-3b68ac5176a2"
      },
      "source": [
        "# More conditional operators\n",
        "a = \"a\"\n",
        "a_ = \"A\"\n",
        "print(True == False)\n",
        "print(first_name == last_name)\n",
        "print(a == a_)\n",
        "b = \"1\"\n",
        "print(a > b)\n",
        "print(1 > True)\n",
        "print(b == False)\n",
        "# print(b > 5)"
      ],
      "execution_count": 64,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n",
            "False\n",
            "False\n",
            "True\n",
            "False\n",
            "False\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "miIgr6q4rAOW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "08c088ba-22de-4326-ab0d-a440287870cc"
      },
      "source": [
        "# Logical Operators\n",
        "# not and or\n",
        "is_game_over = False\n",
        "is_game_over = not is_game_over\n",
        "print(is_game_over)\n",
        "health = 0\n",
        "lives = 1\n",
        "print(health <= 0 and lives <= 0)\n",
        "print(health <= 0 or lives <= 0)"
      ],
      "execution_count": 68,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True\n",
            "False\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i_KoYE1sDYnw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "89a20af7-fbe3-472d-b4b4-1afa6bc39b5c"
      },
      "source": [
        "# 9\n",
        "# Lists\n",
        "inventory = ['Sword', 'Bread', 'Boots']\n",
        "sword = inventory[0]\n",
        "print(sword)\n",
        "inventory[1] = 'Apples'\n",
        "print(inventory)"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Sword\n",
            "['Sword', 'Apples', 'Boots']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Prhi_MXPFsE-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "a2a8bfb9-68e2-4ce9-9f1c-fba81bf8da71"
      },
      "source": [
        "# List functions\n",
        "inventory = ['Sword', 'Bread', 'Boots']\n",
        "print(len(inventory))\n",
        "print(max(inventory))\n",
        "print(min(inventory))\n",
        "\n",
        "inventory.append('Hat')\n",
        "print(inventory)\n",
        "inventory.insert(0, 'Knife')\n",
        "print(inventory)\n",
        "inventory.pop()\n",
        "print(inventory)\n",
        "inventory.remove('Sword')\n",
        "print(inventory)\n",
        "inventory.clear()"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3\n",
            "Sword\n",
            "Boots\n",
            "['Sword', 'Bread', 'Boots', 'Hat']\n",
            "['Knife', 'Sword', 'Bread', 'Boots', 'Hat']\n",
            "['Knife', 'Sword', 'Bread', 'Boots']\n",
            "['Knife', 'Bread', 'Boots']\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tmEZsJGhPZWI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "7cc69e64-05c8-4654-f721-44906b7ce6e9"
      },
      "source": [
        "# 10 2D Lists\n",
        "universe = [[1, 2, 3],\n",
        "            [1, 2, 3, 4],\n",
        "            [1, 2],\n",
        "            [1, 2, 3]]\n",
        "ninth_world = universe[2][1]\n",
        "ninth_world = 5\n",
        "universe.append([1, 2, 3, 4, 5])\n",
        "universe[1].pop()\n",
        "print(universe)"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[1, 2, 3], [1, 2, 3], [1, 2], [1, 2, 3], [1, 2, 3, 4, 5]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "leYb0rwToXhG",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "80f0168d-95dd-48dc-a60b-413c002048e9"
      },
      "source": [
        "# 11 Tuples\n",
        "item = (\"Health Kit\", 4)\n",
        "name = item[0]\n",
        "print(name)\n",
        "# item[1] = 10\n",
        "item = (\"Knife\", 1)\n",
        "print(item)\n",
        "\n",
        "print(item.count('Knife'))\n",
        "print(item.index(1))\n",
        "\n",
        "item = (1, 2, 3)\n",
        "\n",
        "print(len(item))\n",
        "print(max(item))\n",
        "print(min(item))"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Health Kit\n",
            "('Knife', 1)\n",
            "1\n",
            "1\n",
            "3\n",
            "3\n",
            "1\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cGK0Bm3awm67",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "cd526b0e-f9f9-4567-fbdb-445a0554c1ba"
      },
      "source": [
        "# 12 Dictionaries\n",
        "inventory = {'Knife':1, 'Health Kit': 3, 'Wood': 5}\n",
        "print(inventory['Health Kit'])\n",
        "inventory['Knife'] = 2\n",
        "print(inventory)\n",
        "inventory['Gold'] = 50\n",
        "print(inventory)"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3\n",
            "{'Knife': 2, 'Health Kit': 3, 'Wood': 5}\n",
            "{'Knife': 2, 'Health Kit': 3, 'Wood': 5, 'Gold': 50}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-F2ltLv4z01F",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 153
        },
        "outputId": "adc66fdd-4f1c-4c5c-c5b6-6c181158c1e0"
      },
      "source": [
        "# Dictionary Functions\n",
        "inventory = {'Knife':1, 'Health Kit': 3, 'Wood': 5}\n",
        "# print(inventory['Gold'])\n",
        "print(inventory.get('Gold'))\n",
        "print(inventory.keys())\n",
        "print(inventory.values())\n",
        "\n",
        "print(inventory.pop('Knife'))\n",
        "print(inventory)\n",
        "# inventory.clear()\n",
        "\n",
        "inventory['Apple'] = 10\n",
        "print(len(inventory))\n",
        "print(min(inventory))\n",
        "print(max(inventory))"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "None\n",
            "dict_keys(['Knife', 'Health Kit', 'Wood'])\n",
            "dict_values([1, 3, 5])\n",
            "1\n",
            "{'Health Kit': 3, 'Wood': 5}\n",
            "3\n",
            "Apple\n",
            "Wood\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eAIVKDeGxUOq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "4caf62a0-a1fe-4681-a172-b040ef578151"
      },
      "source": [
        "# 13 Ranges\n",
        "first_ten = range(10)\n",
        "print(first_ten[0])\n",
        "print(first_ten[9])\n",
        "\n",
        "first_ten = range(1,11)\n",
        "print(first_ten[0])\n",
        "print(first_ten[9])\n",
        "\n",
        "print(list(range(1,11,2)))\n",
        "\n",
        "reversed_ten = reversed(first_ten)\n",
        "reversed_ten = list(reversed_ten)\n",
        "print(reversed_ten)\n",
        "\n",
        "print(15 not in first_ten)"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0\n",
            "9\n",
            "1\n",
            "10\n",
            "[1, 3, 5, 7, 9]\n",
            "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cl7p-S6rEhGO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2a08046f-9fa7-48a2-f333-bb7d460fd7d8"
      },
      "source": [
        "# 15 if, elif, else statements\n",
        "key_press = 'adsf'\n",
        "if key_press == 'r':\n",
        "  print('Move right')\n",
        "elif key_press == 'l':\n",
        "  print('Move left')\n",
        "else:\n",
        "  print('Invalid key')\n",
        "\n",
        "command = 'Move right' if key_press == 'r' else 'Invalid key' "
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Invalid key\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j7mT6wcLOyx2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "b190930c-85f1-44aa-bb62-008f47a66a62"
      },
      "source": [
        "# 16 if statement variants\n",
        "num_lives = 3\n",
        "num_health_kits = 3\n",
        "health = 0\n",
        "if health <= 0 and num_health_kits <= 0:\n",
        "  num_lives -= 1\n",
        "  print('You lose a life!')\n",
        "  if num_lives <= 0:\n",
        "    print('You died!')\n",
        "elif health < 10:\n",
        "  print('Warning, less than 10% health!')\n",
        "elif health < 50:\n",
        "  print('Warning, less than 50% health!')\n"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "You died!\n",
            "Warning, less than 10% health!\n",
            "Warning, less than 50% health!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fJ4OlZLmGKrZ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 119
        },
        "outputId": "cc958384-86a1-4710-c102-bd52d70257c7"
      },
      "source": [
        "# 18 while loops\n",
        "x_pos = 0\n",
        "y_pos = 1\n",
        "end_pos = 5\n",
        "enemy_pos = 3\n",
        "while x_pos < end_pos:\n",
        "  x_pos += 1\n",
        "  print(x_pos)\n",
        "  if y_pos > 0:\n",
        "    continue\n",
        "  if x_pos == enemy_pos:\n",
        "    print(\"You collided with the enemy\")\n",
        "    break\n",
        "print(\"Game over\")"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "1\n",
            "2\n",
            "3\n",
            "4\n",
            "5\n",
            "Game over\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1exj2AWoN3y0",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "d6c0b6c2-15f1-48c3-e185-99ea2d7e2be4"
      },
      "source": [
        "# 19 for loops\n",
        "# our_range = range(1,6)\n",
        "# for i in our_range:\n",
        "#   print(i)\n",
        "\n",
        "inventory = ['Boots', 'Arrows', 'Bow', 'Sword']\n",
        "# for item in inventory:\n",
        "#   print(item)\n",
        "\n",
        "# for i in range(4):\n",
        "#   print(inventory[i])\n",
        "#   if i == 1:\n",
        "#     break\n",
        "\n",
        "i = 0\n",
        "while i < len(inventory):\n",
        "  print(inventory[i])\n",
        "  i += 1"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Boots\n",
            "Arrows\n",
            "Bow\n",
            "Sword\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JLQA-mC9tVtt",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "dd4ea187-0823-49ff-9b16-bf3557b91e84"
      },
      "source": [
        "# 21 functions\n",
        "x_pos = 0\n",
        "\n",
        "def move():\n",
        "  global x_pos\n",
        "  x_pos += 1\n",
        "\n",
        "move()\n",
        "move()\n",
        "move()\n",
        "move()\n",
        "print(x_pos)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "4\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3oYYV1ba0iHs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4027e13f-6ae0-44f2-cacd-dad28c90c6eb"
      },
      "source": [
        "# 22 parameters and return values\n",
        "x_pos = 0\n",
        "\n",
        "def move(by_amount=1):\n",
        "  global x_pos\n",
        "  x_pos += by_amount\n",
        "\n",
        "move(5)\n",
        "print(x_pos)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "5\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "10xHIUT41Q4t",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "bb4828ea-b8fb-4708-84d2-2b0d3d4db443"
      },
      "source": [
        "start_pos = 0\n",
        "end_pos = 10\n",
        "x_pos = 0\n",
        "\n",
        "def move(x_pos, by_amount=1):\n",
        "  global start_pos, end_pos\n",
        "  x_pos += by_amount\n",
        "  if x_pos < start_pos:\n",
        "    return start_pos\n",
        "  elif x_pos > end_pos:\n",
        "    return end_pos\n",
        "  return x_pos\n",
        "\n",
        "x_pos = move(x_pos, -15)\n",
        "print(x_pos)"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bzOSXPgux5Sj",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 24 classes\n",
        "class GameCharacter:\n",
        "\n",
        "  speed = 1.0\n",
        "\n",
        "  def __init__(self, name, x_pos, health):\n",
        "    self.name = name\n",
        "    self.x_pos = x_pos\n",
        "    self.health = health\n",
        "  \n",
        "  def move(self, by_amount):\n",
        "    self.x_pos += by_amount\n",
        "    \n",
        "  def take_damage(self, amount):\n",
        "    self.health -= amount\n",
        "    if self.health < 0:\n",
        "      self.health = 0\n",
        "\n",
        "  def check_is_dead(self):\n",
        "    return self.health <= 0\n",
        "\n",
        "  def change_speed(to_speed):\n",
        "    GameCharacter.speed = to_speed\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IYoTrOYR66MC",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 68
        },
        "outputId": "077366d1-0784-471f-8aca-81c2506c2305"
      },
      "source": [
        "# 25 objects\n",
        "game_character = GameCharacter('Nimish', 5, 100)\n",
        "other_character = GameCharacter('Other', 0, 50)\n",
        "# print(type(game_character))\n",
        "# print(game_character.name)\n",
        "# print(other_character.name)\n",
        "\n",
        "# game_character.name = \"asfsdafs\"\n",
        "# print(game_character.name)\n",
        "\n",
        "game_character.move(-2)\n",
        "print(game_character.x_pos)\n",
        "\n",
        "game_character.take_damage(200)\n",
        "print(game_character.health)\n",
        "print(game_character.check_is_dead())"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "3\n",
            "0\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P07-03m6qfqF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 26 inheritance\n",
        "class PlayerCharacter(GameCharacter):\n",
        "\n",
        "  def __init__(self, name, x_pos, health, num_lives):\n",
        "    super().__init__(name, x_pos, health)\n",
        "    self.max_health = health\n",
        "    self.num_lives = num_lives\n",
        "    \n",
        "  def take_damage(self, amount):\n",
        "    self.health -= amount\n",
        "    if self.health <= 0:\n",
        "      self.num_lives -= 1\n",
        "      self.health = self.max_health\n",
        "\n",
        "  def check_is_dead(self):\n",
        "    return self.health <= 0 and self.num_lives <= 0"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FlcihtpCsX5H",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        },
        "outputId": "8650c850-4966-489e-e553-fbc4f6dcde3a"
      },
      "source": [
        "pc = PlayerCharacter('Nimish', 0, 100, 3)\n",
        "gc = GameCharacter('Wolf', 0, 100)\n",
        "\n",
        "print(pc.max_health)\n",
        "# print(gc.max_health)\n",
        "\n",
        "pc.move(2)\n",
        "print(pc.x_pos)\n",
        "gc.move(2)\n",
        "print(gc.x_pos)\n",
        "\n",
        "pc.take_damage(150)\n",
        "gc.take_damage(150)\n",
        "print(pc.health)\n",
        "print(gc.health)\n",
        "\n",
        "print(pc.check_is_dead())\n",
        "print(gc.check_is_dead())"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "100\n",
            "2\n",
            "2\n",
            "100\n",
            "0\n",
            "False\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m2te4jwhEKqE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "e7273136-a13d-4475-bb80-000b05acbb94"
      },
      "source": [
        "gc_1 = GameCharacter('Wolf', 0, 100)\n",
        "gc_2 = GameCharacter('Lion', 2, 150)\n",
        "\n",
        "# print(gc_1.speed)\n",
        "# print(gc_2.speed)\n",
        "# print(GameCharacter.speed)\n",
        "\n",
        "# GameCharacter.speed = 2.0\n",
        "GameCharacter.change_speed(4.0)\n",
        "print(gc_1.speed)\n",
        "# gc_2.speed = 10\n",
        "print(gc_2.speed)"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "4.0\n",
            "4.0\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}