diff --git a/Conditionals.ipynb b/Conditionals.ipynb new file mode 100644 index 0000000..49fa27a --- /dev/null +++ b/Conditionals.ipynb @@ -0,0 +1,1209 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyPBfGCysx4LTf2dJMvnLG/H", + "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": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "source": [ + "#Conditionals:\n" + ], + "metadata": { + "id": "Td-c3ot9gCHK" + } + }, + { + "cell_type": "markdown", + "source": [ + "Statements in python scripts are executed sequentially from top to bottom.\n", + "\n", + "If the processing logic require so, the sequential flow of execution can be altered in two way:\n", + "\n", + "1) Conditional execution: a block of one or more statements will be executed if a certain expression is true\n", + "\n", + "2) Repetitive execution: a block of one or more statements will be repetitively executed as long as a certain expression is true.We use if,else,elif statements.The comparison and logical operators are used ." + ], + "metadata": { + "id": "Kh2VxeTPgkZ9" + } + }, + { + "cell_type": "markdown", + "source": [ + "# If Condition:\n", + "---------------\n" + ], + "metadata": { + "id": "ddprpq9Ii54V" + } + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "N824u6-1ftwt" + }, + "outputs": [], + "source": [ + "#syntax:\n", + " if condition:\n", + " statements-1\n", + " statements-2" + ] + }, + { + "cell_type": "code", + "source": [ + "a = 3\n", + "if a > 0:\n", + " print('A is a positive number' )\n", + "\n", + " # A is a positive number" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uq6M5I3pf6gS", + "outputId": "86179e9d-d420-4e18-cb50-035d0b64b3f7" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is a positive number\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#If Else:\n", + "-----------\n" + ], + "metadata": { + "id": "FUE8Rn-IkLMI" + } + }, + { + "cell_type": "markdown", + "source": [ + "if condition is true the first block will be executed, if not the else condition will run." + ], + "metadata": { + "id": "--03OOGlkXGW" + } + }, + { + "cell_type": "code", + "source": [ + "# syntax\n", + "if condition:\n", + " this part of code runs for truthy conditions\n", + "else:\n", + " this part of code runs for false conditions" + ], + "metadata": { + "id": "6berSQYnf6kJ" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a = 3\n", + "if a < 0:\n", + " print('A is a negative number')\n", + "else:\n", + " print('A is a positive number')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "CoyvKJC1f6nl", + "outputId": "320f198e-170c-4ff7-8423-f70b1a424125" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is a positive number\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "a = -3\n", + "if a < 0:\n", + " print('A is a negative number')\n", + "else:\n", + " print('A is a positive number')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "z2gYirQPf60H", + "outputId": "00f7fe0f-81c6-41a4-dfdd-7144ecdd2ced" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is a negative number\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#If Elif Else:\n", + "--------------\n", + "In our day to day life, we make decisions not taking one or two conditions but multiple conditions.\n", + "\n", + "As similar is also full of conditions.We use *elif* when we have multiple conditions." + ], + "metadata": { + "id": "qI2gR9M0k2gp" + } + }, + { + "cell_type": "code", + "source": [ + "# syntax\n", + "if condition:\n", + " code\n", + "elif condition:\n", + " code\n", + "else:\n", + " code\n" + ], + "metadata": { + "id": "ddPeCgtpf64x" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a = 0\n", + "if a > 0:\n", + " print('A is a positive number')\n", + "elif a < 0:\n", + " print('A is a negative number')\n", + "else:\n", + " print('A is zero')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "38WFBWAKl98Z", + "outputId": "79ecde7a-b284-4f84-ea16-d1ec9919c6ad" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is zero\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#Short Hand:\n", + "------------\n", + "# syntax\n", + "code if condition else code" + ], + "metadata": { + "id": "OIAD5j61mFmD" + } + }, + { + "cell_type": "code", + "source": [ + "a = 3\n", + "print('A is positive') if a > 0 else print('A is negative') # first condition met, 'A is positive' will be printed" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "9Wbh097yl9_s", + "outputId": "2897aff0-6766-4587-dc33-53e807068b70" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is positive\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "# Nested Conditions:\n", + "--------------------\n", + "Conditions can be nested" + ], + "metadata": { + "id": "H73q8BvVmZow" + } + }, + { + "cell_type": "code", + "source": [ + "# syntax\n", + "if condition:\n", + " code\n", + " if condition:\n", + " code" + ], + "metadata": { + "id": "9OTRBUO8l-Dg" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a = 0\n", + "if a > 0:\n", + " if a % 2 == 0:\n", + " print('A is a positive and even integer')\n", + " else:\n", + " print('A is a positive number')\n", + "\n", + "elif a == 0:\n", + " print('A is zero')\n", + "else:\n", + " print('A is a negative number')" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "2Bg11bGWl-HD", + "outputId": "48ad39e7-1f96-4921-edfe-2545f0be6338" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is zero\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "#If Condition and Logical Operators:\n", + "-----------------------------------" + ], + "metadata": { + "id": "-m2SuIzinTpM" + } + }, + { + "cell_type": "code", + "source": [ + "# syntax\n", + "if condition and condition:\n", + " code" + ], + "metadata": { + "id": "cRZnIvb_l-Kj" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "a = 0\n", + "if a > 0 and a % 2 == 0:\n", + " print('A is an even and positive integer')\n", + "elif a > 0 and a % 2!= 0:\n", + " print(\"A is a positive integer\")\n", + "\n", + "elif a == 0:\n", + " print('A is zero')\n", + "\n", + "else:\n", + " print('A is negative')\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RMW5k9ONl-QH", + "outputId": "669389a9-c5f3-4f0f-ace7-0557dec192c4" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "A is zero\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine if a number is even or odd\n", + "\n", + "num = int(input(\"Enter a number: \"))\n", + "if num % 2 == 0:\n", + " print(\"The number is even.\")\n", + "\n", + "else:\n", + " print(\"The number is odd.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "t97FnPk0l-SE", + "outputId": "8e5330b9-2c5b-4a08-dc13-b16b614f54c9" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 12\n", + "The number is even.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Check if a student has passed an exam , where passing score is 50\n", + "\n", + "score = int(input(\"Enter the exam score: \"))\n", + "if score >= 50:\n", + " print(\"Congratulations .You have passed the exam.\")\n", + "else:\n", + " print(\"oh! sorry .You have not passed the exam.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "RXl2ETQ4bfH7", + "outputId": "b1b150f8-c529-404c-8ead-03f63ae8009b" + }, + "execution_count": 3, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the exam score: 34\n", + "oh! sorry .You have not passed the exam.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine the largest of three numbers.\n", + "\n", + "a = int(input(\"Enter the first number: \"))\n", + "b = int(input(\"Enter the second number: \"))\n", + "c = int(input(\"Enter the third number: \"))\n", + "\n", + "if a >= b and a >= c:\n", + " print(\"The largest number is:\", a)\n", + "\n", + "elif b >= a and b >= c:\n", + " print(\"The largest number is:\",b)\n", + "\n", + "else:\n", + " print(\"The largest number is:\",c)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "AS5xhRCFbfLX", + "outputId": "5098932a-c0de-467d-93ae-186b44c22bda" + }, + "execution_count": 4, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the first number: 23\n", + "Enter the second number: 34\n", + "Enter the third number: 44\n", + "The largest number is: 44\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Check if a year is a leap year.\n", + "\n", + "year = int(input(\"Enter a yaer: \"))\n", + "\n", + "if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n", + " print(\"It's a leap year.\")\n", + "\n", + "else:\n", + " print(\"It's not a leap year.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "6W0X7z-qbfPA", + "outputId": "f16b1c0c-b3c0-438f-86e3-637a227e24a2" + }, + "execution_count": 5, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a yaer: 1998\n", + "It's not a leap year.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine if a number is within a specified range (e.g., between 1 and 100).\n", + "\n", + "num = int(input(\"Enter a number: \"))\n", + "\n", + "if 1 <= num <= 100:\n", + " print(\"The number is within the specified range.\")\n", + "\n", + "else:\n", + " print(\"The number is outside of the specified range.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "MYyocfwneFSO", + "outputId": "5204cb5d-5585-4aa0-b39b-8970b26df2fa" + }, + "execution_count": 6, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 199\n", + "The number is outside of the specified range.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Check if a character is vowel.\n", + "\n", + "char = input(\"Enter a character: \").lower()\n", + "\n", + "if char in ('a','e','i','o','u'):\n", + " print(\"It's a vowel\")\n", + "\n", + "else:\n", + " print(\"It's a consonant\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "J9OrBzuceFVy", + "outputId": "ffff7c2b-5b60-4b84-9987-66aa3b8bc56c" + }, + "execution_count": 7, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a character: a\n", + "It's a vowel\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Determine the grade for a given score (A, B, C, D,E or F).\n", + "\n", + "score = int(input(\"Enter your score: \"))\n", + "\n", + "if 90 <= score <= 100:\n", + " grade = 'A'\n", + "\n", + "elif 80 <= score < 90 :\n", + " grade = 'B'\n", + "\n", + "elif 70 <= score < 80 :\n", + " grade = 'C'\n", + "\n", + "elif 60 <= score < 70 :\n", + " grade = 'D'\n", + "\n", + "elif 50 <= score < 60 :\n", + " grade = 'E'\n", + "\n", + "else:\n", + " grade = 'F'\n", + "\n", + "print(\"Your grade is :\", grade)\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "1VYocxHKeFZY", + "outputId": "59d5c7fd-3a4f-4e44-ce01-3ce8cab80c21" + }, + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter your score: 45\n", + "Your grade is : F\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Check if a number is a prime number.\n", + "\n", + "num = int(input(\"Enter a number: \"))\n", + "\n", + "is_prime =True\n", + "if num <= 1:\n", + " is_prime = False\n", + "\n", + "else:\n", + " for i in range(2,int(num**0.5) + 1):\n", + " if num % i == 0:\n", + " is_prime = False\n", + " break\n", + "if is_prime:\n", + " print(\"It's a prime number.\")\n", + "\n", + "else:\n", + " print(\"It's not a prime number.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "a131hEmceFk5", + "outputId": "9eb7615e-244b-4ad5-af6b-0d8af9a29e52" + }, + "execution_count": 9, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 23\n", + "It's a prime number.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine if a triangle is equilateral, isosceles, or scalene\n", + "print(\"===========================================\")\n", + "a = int(input(\"Enter the leangth of side A: \"))\n", + "b = int(input(\"Enter the leangth of side B: \"))\n", + "c = int(input(\"Enter the leangth of side C: \"))\n", + "print(\"===========================================\")\n", + "\n", + "if a == b == c:\n", + " print(\"It's an equilateral triangle.\")\n", + "\n", + "elif a == b or b == c or a == c:\n", + " print(\"It's an isoscales triangle.\")\n", + "\n", + "else:\n", + " print(\"It's a scalene triangle.2\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "s5u1F8zdeFox", + "outputId": "0514d467-c50b-4247-fc98-48bceef56584" + }, + "execution_count": 11, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "===========================================\n", + "Enter the leangth of side A: 34\n", + "Enter the leangth of side B: 33\n", + "Enter the leangth of side C: 33\n", + "===========================================\n", + "It's an isoscales triangle.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Check if a number is a perfect square.\n", + "\n", + "num = int(input(\"Enter a number: \"))\n", + "\n", + "if num < 0:\n", + " print(\"It's not a perfect square.\")\n", + "\n", + "elif num == 0 or int(num**0.5)**2 == num :\n", + " print(\"It's a perfect square.\")\n", + "\n", + "else:\n", + " print(\"It's not a perfect square.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "49ba_j5PbfSr", + "outputId": "683aea75-f6f2-4003-9b79-599949b84803" + }, + "execution_count": 12, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 16\n", + "It's a perfect square.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine the season based on a given month.\n", + "\n", + "month = input(\"Enter a month (e.g., January, February, etc.): \").capitalize()\n", + "if month in (\"March\", \"April\", \"May\"):\n", + " season = \"Summer\"\n", + "elif month in (\"June\", \"July\", \"August\"):\n", + " season = \"Rainny\"\n", + "elif month in (\"September\", \"October\", \"November\"):\n", + " season = \"winter\"\n", + "else:\n", + " season = \"Springr\"\n", + "\n", + "print(\"________________________________________\")\n", + "print(f\"The season for {month} is {season}.\")\n", + "print(\"________________________________________\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Py2r6-NubfWC", + "outputId": "558504cf-009f-46e7-bd7e-f20c04356fc4" + }, + "execution_count": 14, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a month (e.g., January, February, etc.): december\n", + "________________________________________\n", + "The season for December is Springr.\n", + "________________________________________\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Check if a year is a leap year using a ternary operator\n", + "print(\"****************************************\")\n", + "year = int(input(\"Enter a year: \"))\n", + "print(\"****************************************\")\n", + "\n", + "is_leap_year = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n", + "\n", + "result = \"Leap year\" if is_leap_year else \"Common year\"\n", + "\n", + "print(f\"{year} is a {result}.\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "55_1QWvSlJPf", + "outputId": "5063e88c-5de0-43d6-919a-4d6cd85bbb6a" + }, + "execution_count": 15, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "****************************************\n", + "Enter a year: 1999\n", + "****************************************\n", + "1999 is a Common year.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Determine if a number is a multiple of both 3 and 5.\n", + "\n", + "num = int(input(\"Entera number : \"))\n", + "\n", + "is_multiple = (num % 3 == 0) and (num % 5 == 0)\n", + "\n", + "if is_multiple:\n", + " print(f\"{num} is a multiple of both 3 and 5.\")\n", + "\n", + "else:\n", + " print(f\"{num} is not a multiple of both 3 and 5.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "4CsNbZ9xlJTC", + "outputId": "5b5122b4-e1e6-492a-ff9f-a5a40f542c74" + }, + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Entera number : 15\n", + "15 is a multiple of both 3 and 5.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine the quadrant of a point on a Cartesian plane\n", + "\n", + "print(\"----------------------------------------------\")\n", + "x = float(input(\"Enter the x-coordinate: \"))\n", + "y = float(input(\"Enter the y-coordinate: \"))\n", + "print(\"----------------------------------------------\")\n", + "\n", + "if x > 0 and y > 0:\n", + " quadrant = \"Quadrant - I\"\n", + "elif x < 0 and y > 0:\n", + " quadrant = \"Quadrant - II\"\n", + "elif x < 0 and y < 0:\n", + " quadrant = \"Quadrant - III\"\n", + "elif x > 0 and y < 0:\n", + " quadrant = \"Quadrant - IV\"\n", + "else:\n", + " quadrant = \"Origin (Quadrant I and II boundray)\"\n", + "\n", + "print(f\"The point ({x},{y}) is in {quadrant}\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "i2hNIDHpbfZq", + "outputId": "40118233-0682-4989-e3cd-35f8d963646c" + }, + "execution_count": 18, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "----------------------------------------------\n", + "Enter the x-coordinate: -33\n", + "Enter the y-coordinate: 11\n", + "----------------------------------------------\n", + "The point (-33.0,11.0) is in Quadrant - II\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Check if a string is a palindrome (reads the same forwards and backwards).\n", + "\n", + "string = input(\"Enter a string: \").lower().replace(\" \",\"\")\n", + "\n", + "is_palindrome = string == string[::-1]\n", + "\n", + "if is_palindrome:\n", + " print(\"It's a palindrome.\")\n", + "\n", + "else:\n", + " print(\"It's not a palindrome\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pOqcgzFIl-VX", + "outputId": "bf0e24d1-9407-47df-9240-37a2c11f2de7" + }, + "execution_count": 19, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a string: tommot\n", + "It's a palindrome.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Determine if a given number is a palindrome (e.g., 121, 1331).\n", + "\n", + "def is_palindrome(num):\n", + " return str(num) == str(num)[::-1]\n", + "\n", + "number = int(input(\"Enter a number: \"))\n", + "\n", + "if is_palindrome(number):\n", + " print(\"It's a palindrome.\")\n", + "else:\n", + " print(\"It's not a palindrome.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "-aF-ce43l-Z5", + "outputId": "710a574e-e3e4-43b5-967d-de8b0ead7501" + }, + "execution_count": 21, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 123456789987654321\n", + "It's a palindrome.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "#Determine the type of a triangle (acute, obtuse, or right) based on side lengths\n", + "\n", + "a = float(input(\"Enter the length of side A: \"))\n", + "b = float(input(\"Enter the length of side B: \"))\n", + "c = float(input(\"Enter the length of side C: \"))\n", + "\n", + "sides = [a, b, c]\n", + "\n", + "sides.sort()\n", + "if sides[0]**2 + sides[1]**2 > sides[2]**2:\n", + " triangle_type = \"Acute\"\n", + "elif sides[0]**2 + sides[1]**2 < sides[2]**2:\n", + " triangle_type = \"Obtuse\"\n", + "else:\n", + " triangle_type = \"Right\"\n", + "\n", + "print(f\"It's an {triangle_type} triangle.\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kUa66ZWnrZ1s", + "outputId": "4ff09bad-e07c-4052-97f7-8ba4fd599d4e" + }, + "execution_count": 22, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter the length of side A: 3\n", + "Enter the length of side B: 4\n", + "Enter the length of side C: 5\n", + "It's an Right triangle.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine if a number is positive, negative, or zero using a lambda function.\n", + "\n", + "check_sign = lambda num:\"Positive\" if num > 0 else \"Nagative\" if num < 0 else \"Zero\"\n", + "\n", + "num = float(input(\"Enter a number: \"))\n", + "result = check_sign(num)\n", + "\n", + "print(f\"The number is {result}\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "obVu-Y47rZ5E", + "outputId": "be7862e1-91c9-4bf8-bbaf-c7ccdf3eb3ac" + }, + "execution_count": 23, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: -12\n", + "The number is Nagative\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine if a string is a valid email address.\n", + "\n", + "import re\n", + "\n", + "email = input(\"Enter an email address: \")\n", + "pattern = r'^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*(\\.\\w{2,3})+$'\n", + "is_valid_email = re.match(pattern, email)\n", + "\n", + "if is_valid_email:\n", + " print(\"It's a valid email address.\")\n", + "\n", + "else:\n", + " print(\"It's not a valid email address.\")" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "h1C2nYvDrZ83", + "outputId": "651bb75e-1588-4077-cb79-c3744f1a67e6" + }, + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter an email address: soumyasankar99@gmail.com\n", + "It's a valid email address.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Determine the age group based on age (e.g., child, teenager, adult).\n", + "\n", + "age = int(input(\"Enter your age: \"))\n", + "if age < 18:\n", + " age_group = \"Child\"\n", + "elif age < 13:\n", + " age_group = \"Teenager\"\n", + "else:\n", + " age_group = \"Adult\"\n", + "print(f\"You are in the {age_group} age group.\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "IoCywHC2tVMb", + "outputId": "b938db1f-ae7e-461f-d8c7-f45c9a1bf821" + }, + "execution_count": 25, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter your age: 23\n", + "You are in the Adult age group.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Check if a given number is a perfect number (sum of its divisors excluding itself equals the number).\n", + "\n", + "def is_perfect_number(num):\n", + " divisors_sum = sum([i for i in range(1, num) if num % i == 0])\n", + " return divisors_sum == num\n", + "\n", + "number = int(input(\"Enter a number: \"))\n", + "if is_perfect_number(number):\n", + " print(\"It's a perfect number.\")\n", + "else:\n", + " print(\"It's not a perfect number.\")\n", + "\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "A7-J0aBdtU-V", + "outputId": "16cc477b-fe0d-4cf2-df9b-5c6d8a863acf" + }, + "execution_count": 26, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter a number: 23\n", + "It's not a perfect number.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "# Check if a given string is a valid ISBN-10 (International Standard Book Number).\n", + "\n", + "def is_valid_isbn_10(isbn):\n", + " isbn = isbn.replace(\"-\", \"\").replace(\" \", \"\")\n", + " if len(isbn) != 10:\n", + " return False\n", + " if not isbn[:-1].isdigit() or (isbn[-1] != 'X' and not isbn[-1].isdigit()):\n", + " return False\n", + "\n", + " total = 0\n", + " for i in range(9):\n", + " total += (i + 1) * int(isbn[i])\n", + " if isbn[9] == 'X':\n", + " total += 10\n", + " else:\n", + " total += int(isbn[9])\n", + "\n", + " return total % 11 == 0\n", + "\n", + "isbn = input(\"Enter an ISBN-10: \")\n", + "if is_valid_isbn_10(isbn):\n", + " print(\"It's a valid ISBN-10.\")\n", + "else:\n", + " print(\"It's not a valid ISBN-10.\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qne23EA_raAK", + "outputId": "58b73a0d-4a7f-4607-8fdd-5192c2a6343c" + }, + "execution_count": 27, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Enter an ISBN-10: \t978-3-16-148410-0\n", + "It's not a valid ISBN-10.\n" + ] + } + ] + } + ] +} \ No newline at end of file