diff --git a/Ex1_Python.ipynb b/Ex1_Python.ipynb index 9467544..a6f3f71 100644 --- a/Ex1_Python.ipynb +++ b/Ex1_Python.ipynb @@ -1,290 +1,605 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "view-in-github" - }, - "source": [ - "\"Open" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "thgO68_HZ0Aa" - }, - "source": [ - "# Introduction" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "PIh3i0c_Z_yP" - }, - "source": [ - "This section introduces the Python Programming uisng Colab. Follow the instructions and complete the exercises." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gCw6SfeFZ78l" - }, - "source": [ - "# Ex 1 : Check Version" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6Vm-27iLabjm" - }, - "source": [ - "Using the below cell, Import the required library and check the version of the python in your environment. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "PJDYHB4AXVdc" - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "5qTEiwGlawf5" - }, - "source": [ - "# Ex2 : Convert Currency. " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XH8K1p0ypVCg" - }, - "source": [ - "Write a program that asks the user for the amount in usd and converts it to British pounds. The exchange rate is from usd to BP is given as 0.82" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "thgO68_HZ0Aa" + }, + "source": [ + "# Introduction" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PIh3i0c_Z_yP" + }, + "source": [ + "This section introduces the Python Programming uisng Colab. Follow the instructions and complete the exercises." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gCw6SfeFZ78l" + }, + "source": [ + "# Ex 1 : Check Version" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6Vm-27iLabjm" + }, + "source": [ + "Using the below cell, Import the required library and check the version of the python in your environment." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "PJDYHB4AXVdc" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n" + ] + }, + { + "cell_type": "code", + "source": [ + "import sys\n", + "sys.version" + ], + "metadata": { + "id": "p-x9fgmCg1Cd", + "outputId": "587f9cde-e95b-4fb8-c84e-d87acc71ca1b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 35 + } + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'3.11.12 (main, Apr 9 2025, 08:55:54) [GCC 11.4.0]'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 2 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5qTEiwGlawf5" + }, + "source": [ + "# Ex2 : Convert Currency." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XH8K1p0ypVCg" + }, + "source": [ + "Write a program that asks the user for the amount in usd and converts it to British pounds. The exchange rate is from usd to BP is given as 0.82" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x9xQXOnRa0MS", + "outputId": "6b2f6c9e-4532-4dc1-ae56-f73772c0e360" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the amount in USD: 1500\n", + "1500 usd is equivalent to 1230.0 british pound\n" + ] + } + ], + "source": [ + "usd_amount = input (\"Enter the amount in USD: \")\n", + "bp_amount = int(usd_amount) *0.82\n", + "print(\"{} usd is equivalent to {} british pound\".format(usd_amount, bp_amount))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ePfECwUIpli4" + }, + "source": [ + "# Ex 3: Temperature Converter" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y60hUWDsp3pK" + }, + "source": [ + "Write a program that convert temperature in Feranhite to celsius." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "G9PbO6qPoUfn", + "outputId": "78edf862-52cc-477a-a644-d016feef6282", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the Feranhite degree 100\n", + "100 Feranhite is equivalent to 37.77777777777778 Celsius\n" + ] + } + ], + "source": [ + "Feranhite_degree = input (\"Enter the Feranhite degree \")\n", + "Celsius_degree = int (Feranhite - 32) * 5/9\n", + "print (\"{} Feranhite is equivalent to {} Celsius\".format(Feranhite_degree, Celsius_degree))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a2kNRO9Vp7Qz" + }, + "source": [ + "# Ex 4: Daily Maths" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lItKon9qqEUn" + }, + "source": [ + "You are working in a retail shop. Write a program that computes the total cost for a customer that bought 3 differnt types of fruits : apple, orange, mango. Ask the user to enter the quantity and price for each type." + ] + }, + { + "cell_type": "code", + "source": [ + "#Ask customers to provide quantity and price of fruits: apple, orange, and mango\n", + "apple_quantity = float(input(\"Enter the quantity of apples(qty): \"))\n", + "apple_price = float(input(\"Enter the price of each apple ($): \"))\n", + "orange_quantity = float(input(\"Enter the quantity of oranges(qty): \"))\n", + "orange_price = float(input(\"Enter the price of each orange ($): \"))\n", + "mango_quantity = float(input(\"Enter the quantity of mangoes(qty): \"))\n", + "mango_price = float(input(\"Enter the price of each mango ($): \"))\n", + "\n", + "#Not allowed customer's inputs of negative value\n", + "if any(value<0 for value in [apple_quantity, apple_price, orange_quantity, orange_price, mango_quantity, mango_price]):\n", + " print(\"Error: Quantities and prices cannot be negative.\")\n", + "\n", + "#Calculate cost for each fruit\n", + "apple_cost = apple_quantity * apple_price\n", + "orange_cost = orange_quantity * orange_price\n", + "mango_cost = mango_quantity * mango_price\n", + "\n", + "#Calculate total cost\n", + "total_cost = apple_cost + orange_cost + mango_cost\n", + "\n", + "#Print the price for apple, orange and mango\n", + "print (f\"Apples:{apple_quantity} x {apple_price} = $ {apple_cost:}\")\n", + "print (f\"Oranges:{orange_quantity} x {orange_price} = $ {orange_cost:}\")\n", + "print (f\"Mangoes:{mango_quantity} x {mango_price} = $ {mango_cost:}\")" + ], + "metadata": { + "id": "6lx_tcXijC8b", + "outputId": "7dc8b32a-f8a0-409f-8d17-b243d66f2e7d", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the quantity of apples(qty): 2\n", + "Enter the price of each apple ($): 3\n", + "Enter the quantity of oranges(qty): 3\n", + "Enter the price of each orange ($): 3\n", + "Enter the quantity of mangoes(qty): 5\n", + "Enter the price of each mango ($): 5\n", + "Apples:2.0 x 3.0 = $ 6.0\n", + "Oranges:3.0 x 3.0 = $ 9.0\n", + "Mangoes:5.0 x 5.0 = $ 25.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "r988Fb9b2gqx" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "31fXlmEQqgvW" + }, + "source": [ + "# Ex 5 : Greeting" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zvnmj8MGrJIw" + }, + "source": [ + "Write a program to greet the customer who provides the user name. Your program should ask the name of the customer and print \"Hello + customer name\"" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "id": "Z6ynxZMarIXh", + "outputId": "154e3bf2-31db-4ec0-e168-f8761a2bedd6", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Welcome \n", + "Hello\n", + "Enter Customer name: Alex\n", + "Customer name is:Alex \n" + ] + } + ], + "source": [ + "print (\"Welcome \")\n", + "print (\"Hello\")\n", + "customer_name = input(\"Enter Customer name: \")\n", + "print(f\"Customer name is:{customer_name} \")" + ] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "enf_hrmGnD0y" + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oe8oJ3k2gLHT" + }, + "source": [ + "# Ex 6: Random Number\n", + "Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x^y." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "id": "_EaI6GJTgLHT", + "outputId": "ff5f697d-3fe2-4e22-da1f-81c6c20e0374", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A random number between 1 and 10: 2\n", + "A random number between 2 and 5: 2\n", + "x^y: 4\n" + ] + } + ], + "source": [ + "from random import randint\n", + "x = randint(1,10)\n", + "print('A random number between 1 and 10: ', x)\n", + "\n", + "y = randint(2,5)\n", + "print(\"A random number between 2 and 5:\", y)\n", + "\n", + "print(\"x^y:\", x**y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Nk_pK1xUgLHT" + }, + "source": [ + "# Ex 7: Greet many times.\n", + "Write a program that generates a random number between 1 and 10 and prints your name that many times." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "id": "oy7n2PUGgLHU", + "outputId": "72ff6bbb-0112-4c3d-8575-32084f874617", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1. Sharon Wai\n", + "2. Sharon Wai\n", + "3. Sharon Wai\n", + "4. Sharon Wai\n", + "5. Sharon Wai\n", + "6. Sharon Wai\n", + "7. Sharon Wai\n", + "1. Sharon Wai\n" + ] + } + ], + "source": [ + "import random\n", + "import random\n", + "\n", + "#Define my name to print\n", + "your_name = \"Sharon Wai\"\n", + "\n", + "#Generate a random number between 1 and 10\n", + "result = random.randint(1,10)\n", + "\n", + "#To print name many times.\n", + "for i in range(result):\n", + " print(f\"{i+1}. {name}\")\n", + "result = random.randint(1,10)\n", + "\n", + "#To print name many times.\n", + "for i in range(result):\n", + " print(f\"{i+1}. {name}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "70DTA1t-gLHU" + }, + "source": [ + "# Ex 8: Computing.\n", + "Write a program that asks the user to enter two numbers, x and y, and computes |x-y|/(x+y)." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "id": "3sQ1yFe5gLHU", + "outputId": "d3ba7576-d8d4-437d-de62-aafdb0c51399", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 106 + } + }, + "outputs": [ + { + "output_type": "error", + "ename": "IndentationError", + "evalue": "expected an indented block after 'if' statement on line 5 (, line 6)", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m6\u001b[0m\n\u001b[0;31m print(\"Error: Division by zero is not allowed (x + y = 0.)\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block after 'if' statement on line 5\n" + ] + } + ], + "source": [ + "# Ask the user to enter two numbers\n", + "x = float(input(\"Enter the first number (x):\"))\n", + "y = float (input(\"Enter the second number (y):\"))\n", + "\n", + "if x + y == 0:\n", + "print(\"Error: Division by zero is not allowed (x + y = 0.)\")\n", + "else:\n", + "\n", + " result = abs(x-y)/(x+y)\n", + " print(f\"The result of abs(x-y)/(x+y) is: {result}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g-h6PkfggLHU" + }, + "source": [ + "# Ex 9: Computing\n", + "\n", + "Write a program that asks the user for a number of seconds and prints out how many minutes and seconds that is. For instance, 200 seconds is 3 minutes and 20 seconds." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "id": "vZIj1RnygLHV", + "outputId": "0e8e23de-97dc-45d0-dd13-562dd570a8e8", + "colab": { + "base_uri": "https://localhost:8080/" + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the number of seconds:200\n", + "200 seconds is 3 minutes and 20 seconds.\n" + ] + } + ], + "source": [ + "#Asks the user for a total number of seconds\n", + "total_seconds = int(input(\"Enter the number of seconds:\"))\n", + "\n", + "# Calculate minutes and remaining seconds\n", + "minutes = total_seconds // 60\n", + "remaining_seconds = total_seconds % 60\n", + "\n", + "# Print the result\n", + "print(f\"{total_seconds} seconds is {minutes} minutes and {remaining_seconds} seconds.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DrPKscesgLHV" + }, + "source": [ + "# Ex 10: Using Math Module\n", + "\n", + "Write a program that asks the user for a number and then prints out the sine, cosine, and tangent of that number." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "id": "-SKlKM6wgLHV", + "outputId": "0735c1ac-c9bd-4cbe-aa87-2aa67945c54a", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 280 + } + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter sine number:45\n", + "Enter cosine number:45\n", + "Enter tangent number:45\n", + "0.8509035245341184\n" + ] + }, + { + "output_type": "error", + "ename": "AttributeError", + "evalue": "module 'math' has no attribute 'cosine'", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\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 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m#Calculate cosine, and tangent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcosine_value\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcosine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_cosine\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 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcosine_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'math' has no attribute 'cosine'" + ] + } + ], + "source": [ + "import math\n", + "num_sine = float(input(\"Enter sine number:\"))\n", + "num_cosine = float(input(\"Enter cosine number:\"))\n", + "num_tangent = float(input(\"Enter tangent number:\"))\n", + "sine_value = math.sin(num_sine)\n", + "print(sine_value)\n", + "\n", + "#Calculate cosine, and tangent\n", + "cosine_value = math.cosine(num_cosine)\n", + "print(cosine_value)\n", + "\n", + "tangent_value = math.tangent(num_tangent)\n", + "print(tangent_value)\n", + "\n", + "#Print the results of sine, cosine and tangent\n", + "print(f\"sine of {num} is {sine_value}\")\n", + "print(f\"cosine of {num} is {cosine_value}\")\n", + "print(f\"tangent of {num} is {tangent_value}\")\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "SSIPZZf98Pr7" + }, + "execution_count": null, + "outputs": [] + } + ], + "metadata": { "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "x9xQXOnRa0MS", - "outputId": "64294c78-0769-4728-f781-9204a1aa51e3" - }, - "outputs": [], - "source": [ - "usd_amount = input (\"Enter the amount in USD: \")\n", - "bp_amount = int(usd_amount) *0.82\n", - "print(\"{} usd is equivalent to {} british pound\".format(usd_amount, bp_amount))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ePfECwUIpli4" - }, - "source": [ - "# Ex 3: Temperature Converter" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Y60hUWDsp3pK" - }, - "source": [ - "Write a program that convert temperature in Feranhite to celsius. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "G9PbO6qPoUfn" - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "a2kNRO9Vp7Qz" - }, - "source": [ - "# Ex 4: Daily Maths" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "lItKon9qqEUn" - }, - "source": [ - "You are working in a retail shop. Write a program that computes the total cost for a customer that bought 3 differnt types of fruits : apple, orange, mango. Ask the user to enter the quantity and price for each type. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "mIQFjH6wqDma" - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "31fXlmEQqgvW" - }, - "source": [ - "# Ex 5 : Greeting" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "zvnmj8MGrJIw" - }, - "source": [ - "Write a program to greet the customer who provides the user name. Your program should ask the name of the customer and print \"Hello + customer name\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "Z6ynxZMarIXh" - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Ex 6: Random Number\n", - "Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x^y." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "from random import randint\n", - "x = randint(1,10)\n", - "print('A random number between 1 and 10: ', x)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Ex 7: Greet many times.\n", - "Write a program that generates a random number between 1 and 10 and prints your name that many times." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Ex 8: Computing. \n", - "Write a program that asks the user to enter two numbers, x and y, and computes |x-y|/(x+y)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Ex 9: Computing\n", - "\n", - "Write a program that asks the user for a number of seconds and prints out how many minutes and seconds that is. For instance, 200 seconds is 3 minutes and 20 seconds." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Ex 10: Using Math Module\n", - "\n", - "Write a program that asks the user for a number and then prints out the sine, cosine, and tangent of that number." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import math\n", - "num = float(input(\"Enter a number:\"))\n", - "sine_value = math.sin(num)\n", - "print(sine_value)\n" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "include_colab_link": true, - "name": "Ex1_Python.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" + "name": "Ex1_Python.ipynb", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.1" + } }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.1" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file diff --git a/SharonWai/Ex1_Python_SharonWai.ipynb b/SharonWai/Ex1_Python_SharonWai.ipynb new file mode 100644 index 0000000..659ab5f --- /dev/null +++ b/SharonWai/Ex1_Python_SharonWai.ipynb @@ -0,0 +1,605 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "thgO68_HZ0Aa" + }, + "source": [ + "# Introduction" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PIh3i0c_Z_yP" + }, + "source": [ + "This section introduces the Python Programming uisng Colab. Follow the instructions and complete the exercises." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gCw6SfeFZ78l" + }, + "source": [ + "# Ex 1 : Check Version" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6Vm-27iLabjm" + }, + "source": [ + "Using the below cell, Import the required library and check the version of the python in your environment." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "PJDYHB4AXVdc" + }, + "outputs": [], + "source": [ + "import numpy as np\n", + "import pandas as pd\n" + ] + }, + { + "cell_type": "code", + "source": [ + "import sys\n", + "sys.version" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + }, + "id": "p-x9fgmCg1Cd", + "outputId": "587f9cde-e95b-4fb8-c84e-d87acc71ca1b" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'3.11.12 (main, Apr 9 2025, 08:55:54) [GCC 11.4.0]'" + ], + "application/vnd.google.colaboratory.intrinsic+json": { + "type": "string" + } + }, + "metadata": {}, + "execution_count": 2 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5qTEiwGlawf5" + }, + "source": [ + "# Ex2 : Convert Currency." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XH8K1p0ypVCg" + }, + "source": [ + "Write a program that asks the user for the amount in usd and converts it to British pounds. The exchange rate is from usd to BP is given as 0.82" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "x9xQXOnRa0MS", + "outputId": "6b2f6c9e-4532-4dc1-ae56-f73772c0e360" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the amount in USD: 1500\n", + "1500 usd is equivalent to 1230.0 british pound\n" + ] + } + ], + "source": [ + "usd_amount = input (\"Enter the amount in USD: \")\n", + "bp_amount = int(usd_amount) *0.82\n", + "print(\"{} usd is equivalent to {} british pound\".format(usd_amount, bp_amount))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ePfECwUIpli4" + }, + "source": [ + "# Ex 3: Temperature Converter" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y60hUWDsp3pK" + }, + "source": [ + "Write a program that convert temperature in Feranhite to celsius." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "G9PbO6qPoUfn", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "78edf862-52cc-477a-a644-d016feef6282" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the Feranhite degree 100\n", + "100 Feranhite is equivalent to 37.77777777777778 Celsius\n" + ] + } + ], + "source": [ + "Feranhite_degree = input (\"Enter the Feranhite degree \")\n", + "Celsius_degree = int (Feranhite - 32) * 5/9\n", + "print (\"{} Feranhite is equivalent to {} Celsius\".format(Feranhite_degree, Celsius_degree))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "a2kNRO9Vp7Qz" + }, + "source": [ + "# Ex 4: Daily Maths" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lItKon9qqEUn" + }, + "source": [ + "You are working in a retail shop. Write a program that computes the total cost for a customer that bought 3 differnt types of fruits : apple, orange, mango. Ask the user to enter the quantity and price for each type." + ] + }, + { + "cell_type": "code", + "source": [ + "#Ask customers to provide quantity and price of fruits: apple, orange, and mango\n", + "apple_quantity = float(input(\"Enter the quantity of apples(qty): \"))\n", + "apple_price = float(input(\"Enter the price of each apple ($): \"))\n", + "orange_quantity = float(input(\"Enter the quantity of oranges(qty): \"))\n", + "orange_price = float(input(\"Enter the price of each orange ($): \"))\n", + "mango_quantity = float(input(\"Enter the quantity of mangoes(qty): \"))\n", + "mango_price = float(input(\"Enter the price of each mango ($): \"))\n", + "\n", + "#Not allowed customer's inputs of negative value\n", + "if any(value<0 for value in [apple_quantity, apple_price, orange_quantity, orange_price, mango_quantity, mango_price]):\n", + " print(\"Error: Quantities and prices cannot be negative.\")\n", + "\n", + "#Calculate cost for each fruit\n", + "apple_cost = apple_quantity * apple_price\n", + "orange_cost = orange_quantity * orange_price\n", + "mango_cost = mango_quantity * mango_price\n", + "\n", + "#Calculate total cost\n", + "total_cost = apple_cost + orange_cost + mango_cost\n", + "\n", + "#Print the price for apple, orange and mango\n", + "print (f\"Apples:{apple_quantity} x {apple_price} = $ {apple_cost:}\")\n", + "print (f\"Oranges:{orange_quantity} x {orange_price} = $ {orange_cost:}\")\n", + "print (f\"Mangoes:{mango_quantity} x {mango_price} = $ {mango_cost:}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6lx_tcXijC8b", + "outputId": "7dc8b32a-f8a0-409f-8d17-b243d66f2e7d" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the quantity of apples(qty): 2\n", + "Enter the price of each apple ($): 3\n", + "Enter the quantity of oranges(qty): 3\n", + "Enter the price of each orange ($): 3\n", + "Enter the quantity of mangoes(qty): 5\n", + "Enter the price of each mango ($): 5\n", + "Apples:2.0 x 3.0 = $ 6.0\n", + "Oranges:3.0 x 3.0 = $ 9.0\n", + "Mangoes:5.0 x 5.0 = $ 25.0\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "r988Fb9b2gqx" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "31fXlmEQqgvW" + }, + "source": [ + "# Ex 5 : Greeting" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zvnmj8MGrJIw" + }, + "source": [ + "Write a program to greet the customer who provides the user name. Your program should ask the name of the customer and print \"Hello + customer name\"" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "id": "Z6ynxZMarIXh", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "154e3bf2-31db-4ec0-e168-f8761a2bedd6" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Welcome \n", + "Hello\n", + "Enter Customer name: Alex\n", + "Customer name is:Alex \n" + ] + } + ], + "source": [ + "print (\"Welcome \")\n", + "print (\"Hello\")\n", + "customer_name = input(\"Enter Customer name: \")\n", + "print(f\"Customer name is:{customer_name} \")" + ] + }, + { + "cell_type": "markdown", + "source": [], + "metadata": { + "id": "enf_hrmGnD0y" + } + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oe8oJ3k2gLHT" + }, + "source": [ + "# Ex 6: Random Number\n", + "Write a program that generates a random number, x, between 1 and 50, a random number y between 2 and 5, and computes x^y." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_EaI6GJTgLHT", + "outputId": "ff5f697d-3fe2-4e22-da1f-81c6c20e0374" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A random number between 1 and 10: 2\n", + "A random number between 2 and 5: 2\n", + "x^y: 4\n" + ] + } + ], + "source": [ + "from random import randint\n", + "x = randint(1,10)\n", + "print('A random number between 1 and 10: ', x)\n", + "\n", + "y = randint(2,5)\n", + "print(\"A random number between 2 and 5:\", y)\n", + "\n", + "print(\"x^y:\", x**y)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Nk_pK1xUgLHT" + }, + "source": [ + "# Ex 7: Greet many times.\n", + "Write a program that generates a random number between 1 and 10 and prints your name that many times." + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oy7n2PUGgLHU", + "outputId": "72ff6bbb-0112-4c3d-8575-32084f874617" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "1. Sharon Wai\n", + "2. Sharon Wai\n", + "3. Sharon Wai\n", + "4. Sharon Wai\n", + "5. Sharon Wai\n", + "6. Sharon Wai\n", + "7. Sharon Wai\n", + "1. Sharon Wai\n" + ] + } + ], + "source": [ + "import random\n", + "import random\n", + "\n", + "#Define my name to print\n", + "your_name = \"Sharon Wai\"\n", + "\n", + "#Generate a random number between 1 and 10\n", + "result = random.randint(1,10)\n", + "\n", + "#To print name many times.\n", + "for i in range(result):\n", + " print(f\"{i+1}. {name}\")\n", + "result = random.randint(1,10)\n", + "\n", + "#To print name many times.\n", + "for i in range(result):\n", + " print(f\"{i+1}. {name}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "70DTA1t-gLHU" + }, + "source": [ + "# Ex 8: Computing.\n", + "Write a program that asks the user to enter two numbers, x and y, and computes |x-y|/(x+y)." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 109 + }, + "id": "3sQ1yFe5gLHU", + "outputId": "d3ba7576-d8d4-437d-de62-aafdb0c51399" + }, + "outputs": [ + { + "output_type": "error", + "ename": "IndentationError", + "evalue": "expected an indented block after 'if' statement on line 5 (, line 6)", + "traceback": [ + "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m6\u001b[0m\n\u001b[0;31m print(\"Error: Division by zero is not allowed (x + y = 0.)\")\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block after 'if' statement on line 5\n" + ] + } + ], + "source": [ + "# Ask the user to enter two numbers\n", + "x = float(input(\"Enter the first number (x):\"))\n", + "y = float (input(\"Enter the second number (y):\"))\n", + "\n", + "if x + y == 0:\n", + "print(\"Error: Division by zero is not allowed (x + y = 0.)\")\n", + "else:\n", + "\n", + " result = abs(x-y)/(x+y)\n", + " print(f\"The result of abs(x-y)/(x+y) is: {result}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g-h6PkfggLHU" + }, + "source": [ + "# Ex 9: Computing\n", + "\n", + "Write a program that asks the user for a number of seconds and prints out how many minutes and seconds that is. For instance, 200 seconds is 3 minutes and 20 seconds." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vZIj1RnygLHV", + "outputId": "0e8e23de-97dc-45d0-dd13-562dd570a8e8" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the number of seconds:200\n", + "200 seconds is 3 minutes and 20 seconds.\n" + ] + } + ], + "source": [ + "#Asks the user for a total number of seconds\n", + "total_seconds = int(input(\"Enter the number of seconds:\"))\n", + "\n", + "# Calculate minutes and remaining seconds\n", + "minutes = total_seconds // 60\n", + "remaining_seconds = total_seconds % 60\n", + "\n", + "# Print the result\n", + "print(f\"{total_seconds} seconds is {minutes} minutes and {remaining_seconds} seconds.\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DrPKscesgLHV" + }, + "source": [ + "# Ex 10: Using Math Module\n", + "\n", + "Write a program that asks the user for a number and then prints out the sine, cosine, and tangent of that number." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 289 + }, + "id": "-SKlKM6wgLHV", + "outputId": "0735c1ac-c9bd-4cbe-aa87-2aa67945c54a" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter sine number:45\n", + "Enter cosine number:45\n", + "Enter tangent number:45\n", + "0.8509035245341184\n" + ] + }, + { + "output_type": "error", + "ename": "AttributeError", + "evalue": "module 'math' has no attribute 'cosine'", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\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 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;31m#Calculate cosine, and tangent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0mcosine_value\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcosine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_cosine\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 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcosine_value\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mAttributeError\u001b[0m: module 'math' has no attribute 'cosine'" + ] + } + ], + "source": [ + "import math\n", + "num_sine = float(input(\"Enter sine number:\"))\n", + "num_cosine = float(input(\"Enter cosine number:\"))\n", + "num_tangent = float(input(\"Enter tangent number:\"))\n", + "sine_value = math.sin(num_sine)\n", + "print(sine_value)\n", + "\n", + "#Calculate cosine, and tangent\n", + "cosine_value = math.cosine(num_cosine)\n", + "print(cosine_value)\n", + "\n", + "tangent_value = math.tangent(num_tangent)\n", + "print(tangent_value)\n", + "\n", + "#Print the results of sine, cosine and tangent\n", + "print(f\"sine of {num} is {sine_value}\")\n", + "print(f\"cosine of {num} is {cosine_value}\")\n", + "print(f\"tangent of {num} is {tangent_value}\")\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "SSIPZZf98Pr7" + }, + "execution_count": null, + "outputs": [] + } + ], + "metadata": { + "colab": { + "name": "Ex1_Python.ipynb", + "provenance": [], + "include_colab_link": true + }, + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}