{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyOkfoKsBaYtq1r/kJxQeMnk", "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": [ "# An **if else** Python statement evaluates whether an expression is true or false. If a condition is true, the “if” statement executes. Otherwise, the “else” statement executes. Python if else statements help coders control the flow of their programs.\n", "\n", "When you’re writing a program, you may want a block of code to run only when a certain condition is met. That’s where conditional statements come in. Conditional statements allow you to control the flow of your program more effectively." ], "metadata": { "id": "O8aRlFEWHfSr" } }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UID84B5UHd4s", "outputId": "12ea604d-5f82-4685-9da7-fbe721997bbf" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "a is 0\n", "b is greater than a\n", "b and c are greater than a\n", "b is smaller than a\n", "b is smaller than a\n", "y is less than z\n", "a is greater than 10\n", "a is equal to 20\n", "Price: $2.10\n", "Price: $1.75\n", "This user has a tab over $20 that needs to be paid.\n", "Price: $2.10\n", "Price: $2.00\n", "5 is less than 10\n", "Statement after if statement\n", "i is greater than 15\n", "statement after if statement\n", "value of variable a is greater than 40\n", "a is smaller than 25\n", " a is greater than b\n", "a is greater\n", "16\n", "27\n", "price is less than 500\n", "price = 50\n", "quantity = 5\n", "No if block executed.\n", "price is greater than 100\n", "price is 100\n", "price is less than 100\n" ] } ], "source": [ "a = 0\n", "\n", "if a > 0:\n", " print(\"a is greater than 0\")\n", "elif a < 0:\n", " print(\"a is less than 0\")\n", "else:\n", " print(\"a is 0\")\n", "\n", "a = 10\n", "b = 20\n", "\n", "if b > a:\n", " print(\"b is greater than a\")\n", "\n", "a = 10\n", "b = 20\n", "c = 30\n", "\n", "if b > a and c > a:\n", " print(\"b and c are greater than a\") #Prints this message\n", "\n", "a = 100\n", "b = 20\n", "\n", "if b > a:\n", " print(\"b is greater than a\")\n", "else:\n", " print(\"b is smaller than a\")\n", "\n", "a = 100\n", "b = 20\n", "\n", "print(\"b is greater than a\") if (b > a) else print(\"b is smaller than a\")\n", "\n", "x = 100\n", "y = 200\n", "z = 300\n", "\n", "if y < x:\n", "\tprint('y is less than x')\n", "elif y < z:\n", "\tprint('y is less than z')\n", "\n", "a = 20\n", "\n", "if a > 10:\n", " print(\"a is greater than 10\")\t#Prints this message\n", " if a < 20:\n", " print(\"a is less than 20\")\n", " else:\n", " print(\"a is equal to 20\")\n", "\n", "sandwich_order = \"Bacon Roll\"\n", "\n", "if sandwich_order == \"Ham Roll\":\n", "\tprint(\"Price: $1.75\")\n", "elif sandwich_order == \"Cheese Roll\":\n", "\tprint(\"Price: $1.80\")\n", "elif sandwich_order == \"Bacon Roll\":\n", "\tprint(\"Price: $2.10\")\n", "else:\n", "\tprint(\"Price: $2.00\")\n", "\n", "# Python if Statement\n", "\n", "# if Statement\n", "\n", "sandwich_order = \"Ham Roll\"\n", "\n", "if sandwich_order == \"Ham Roll\":\n", "\tprint(\"Price: $1.75\")\n", "\n", "# if else Statement\n", "\n", "tab = 29.95\n", "\n", "if tab > 20:\n", "\tprint(\"This user has a tab over $20 that needs to be paid.\")\n", "else:\n", "\tprint(\"This user's tab is below $20 that does not require immediate payment.\")\n", "\n", "# elif Statement\n", "\n", "sandwich_order = \"Bacon Roll\"\n", "\n", "if sandwich_order == \"Ham Roll\":\n", "\tprint(\"Price: $1.75\")\n", "elif sandwich_order == \"Cheese Roll\":\n", "\tprint(\"Price: $1.80\")\n", "elif sandwich_order == \"Bacon Roll\":\n", "\tprint(\"Price: $2.10\")\n", "else:\n", "\tprint(\"Price: $2.00\")\n", "\n", "# Nested if Statement\n", "\n", "sandwich_order = \"Other Filled Roll\"\n", "\n", "if sandwich_order != \"Other Filled Roll\":\n", "\tif sandwich_order == \"Ham Roll\":\n", "\t print(\"Price: $1.75\")\n", "if sandwich_order == \"Cheese Roll\":\n", "\tprint(\"Price: $1.80\")\n", "elif sandwich_order == \"Bacon Roll\":\n", "\tprint(\"Price: $2.10\")\n", "\n", "else:\n", "\tprint(\"Price: $2.00\")\n", "\n", "a = 5\n", "if (a <10):\n", " print (\"5 is less than 10\")\n", "print (\"Statement after if statement\")\n", "\n", "\n", "i = 20;\n", "if (i < 15):\n", " print (\"i is smaller than 15\")\n", "else:\n", " print (\"i is greater than 15\")\n", "print (\"statement after if statement\")\n", "\n", "\n", "a = 50\n", "if (a == 20):\n", " print (\"value of variable a is 20\")\n", "elif (a == 30):\n", " print (\"value of variable a is 30\")\n", "elif (a == 40):\n", " print (\"value of variable a is 40\")\n", "else:\n", " print (\"value of variable a is greater than 40\")\n", "\n", "\n", "a = 20\n", "if (a == 20):\n", "# First if statement\n", " if (a < 25):\n", " print (\"a is smaller than 25\")\n", " else:\n", " print (\"a is greater than 25\")\n", "else:\n", " print(\"a is not equal to 20\")\n", "\n", "\n", "a = 4\n", "b = 2\n", "if a>b: print(\" a is greater than b\")\n", "\n", "a = 4\n", "b = 2\n", "print(\"a is greater\") if a>b else print (\"b is greater\")\n", "\n", "\n", "x = lambda n: n**2 if n%2 == 0 else n**3\n", "print(x(4))\n", "print(x(3))\n", "\n", "\n", "price = 50\n", "\n", "if price > 100:\n", " print(\"price is less than 100\")\n", "\n", "price = 50\n", "quantity = 5\n", "\n", "if price*quantity > 500:\n", " print(\"price*quantity is less than 500\")\n", " print(\"price = \", price)\n", " print(\"quantity = \", quantity)\n", "\n", "price = 50\n", "quantity = 5\n", "if price*quantity > 500:\n", " print(\"price is less than 500\")\n", " print(\"price = \", price)\n", " print(\"quantity = \", quantity)\n", "\n", "\n", "price = 50\n", "quantity = 5\n", "if price*quantity > 100:\n", " print(\"price is less than 500\")\n", " print(\"price = \", price)\n", " print(\"quantity = \", quantity)\n", "print(\"No if block executed.\")\n", "\n", "price = 100\n", "\n", "if price >= 100:\n", " print(\"price is greater than 100\")\n", "\n", "if price == 100:\n", " print(\"price is 100\")\n", "\n", "if price > 100:\n", " print(\"price is less than 100\")\n", "\n", "price = 50\n", "\n", "if price >= 100:\n", " print(\"price is greater than 100\")\n", "else:\n", " print(\"price is less than 100\")" ] }, { "cell_type": "markdown", "source": [ "# **What is Python If Statement?**\n", "Python if Statement is used for decision-making operations. It contains a body of code which runs only when the condition given in the if statement is true. If the condition is false, then the optional else statement runs which contains some code for the else condition.\n", "\n", "When you want to justify one condition while the other condition is not true, then you use Python if else statement.\n", "\n", "Python if Statement Syntax:\n", "\n", "if expression\n", " Statement\n", "else\n", " Statement" ], "metadata": { "id": "TLB_BoXhHgGL" } }, { "cell_type": "code", "source": [ "price = 100\n", "\n", "if price >= 100:\n", " print(\"price is greater than 100\")\n", "elif price == 100:\n", " print(\"price is 100\")\n", "elif price <= 100:\n", " print(\"price is less than 100\")\n", "\n", "price = 50\n", "quantity = 5\n", "amount = price*quantity\n", "\n", "if amount > 100:\n", " if amount >= 500:\n", " print(\"Amount is greater than 500\")\n", " else:\n", " if amount <= 500 and amount >= 400:\n", " print(\"Amount is between 400 and 500\")\n", " elif amount <= 400 and amount >= 300:\n", " print(\"Amount is between 300 and 400\")\n", " else:\n", " print(\"Amount is between 200 and 300\")\n", "elif amount == 100:\n", " print(\"Amount is 100\")\n", "else:\n", " print(\"Amount is less than 100\")\n", "\n", "#\n", "#Example file for working with conditional statement\n", "#\n", "def main():\n", "\tx,y =8,4\n", "\n", "\tif(x < y):\n", "\t\tst= \"x is less than y\"\n", "\telse:\n", "\t\tst= \"x is greater than y\"\n", "\tprint (st)\n", "\n", "if __name__ == \"__main__\":\n", "\tmain()\n", "\n", "#\n", "#Example file for working with conditional statement\n", "#\n", "def main():\n", "\tx,y =8,8\n", "\n", "\tif(x < y):\n", "\t\tst= \"x is less than y\"\n", "\telse:\n", "\t\tst= \"x is greater than y\"\n", "\tprint(st)\n", "\n", "if __name__ == \"__main__\":\n", "\tmain()\n", "\n", "#Example file for working with conditional statement\n", "#\n", "def main():\n", "\tx,y =8,8\n", "\tif(x < y):\n", "\t\tst= \"x is less than y\"\n", "\telif (x == y):\n", "\t\tst= \"x is same as y\"\n", "\telse:\n", "\t\tst=\"x is greater than y\"\n", "\tprint(st)\n", "if __name__ == \"__main__\":\n", "\tmain()\n", "\n", "\n", "def main():\n", "\tx,y = 10,8\n", "\tst = \"x is less than y\" if (x < y) else \"x is greater than or equal to y\"\n", "\tprint(st)\n", "if __name__ == \"__main__\":\n", "\tmain()\n", "\n", "total = 100\n", "#country = \"US\"\n", "country = \"AU\"\n", "if country == \"US\":\n", " if total <= 50:\n", " print(\"Shipping Cost is $50\")\n", "elif total <= 100:\n", " print(\"Shipping Cost is $25\")\n", "elif total <= 150:\n", "\t print(\"Shipping Costs $5\")\n", "else:\n", " print(\"FREE\")\n", "if country == \"AU\":\n", "\t if total <= 50:\n", "\t print(\"Shipping Cost is $100\")\n", "else:\n", "\t print(\"FREE\")\n", "\n", "def main():\n", "\tx,y =2,8\n", "\n", "\tif(x < y):\n", "\t\tst= \"x is less than y\"\n", "\tprint(st)\n", "\n", "if __name__ == \"__main__\":\n", "\tmain()\n", "\n", "\n", "x = int(input())\n", "if x > 0:\n", " print(x)\n", "else:\n", " print(-x)\n", "\n", "\n", "x = int(input())\n", "if x < 0:\n", " x = -x\n", "print(x)\n", "\n", "x = int(input())\n", "print(abs(x))\n", "\n", "x = int(input())\n", "y = int(input())\n", "if x > 0:\n", " if y > 0:\n", " # x is greater than 0, y is greater than 0\n", " print(\"Quadrant I\")\n", " else:\n", " # x is greater than 0, y is less or equal than 0\n", " print(\"Quadrant IV\")\n", "else:\n", " if y > 0:\n", " # x is less or equal than 0, y is greater than 0\n", " print(\"Quadrant II\")\n", " else:\n", " # x is less or equal than 0, y is less or equal than 0\n", " print(\"Quadrant III\")\n", "\n", "\n", "print(bool(-10)) # True\n", "print(bool(0)) # False - zero is the only false number\n", "print(bool(10)) # True\n", "\n", "print(bool('')) # False - empty string is the only false string\n", "print(bool('abc')) # True\n", "\n", "num = 3\n", "if num > 0:\n", " print(num, \"is a positive number.\")\n", " print(\"This is always printed.\")\n", "num = -1\n", "if num > 0:\n", " print(num, \"is a positive number.\")\n", " print(\"This is also always printed.\")\n", "\n", "num = 3\n", "# Try these two variations as well.\n", "# num = -5\n", "# num = 0\n", "if num >= 0:\n", " print(\"Positive or Zero\")\n", "else:\n", " print(\"Negative number\")\n", "\n", "num = 3.4\n", "# Try these two variations as well:\n", "# num = 0\n", "# num = -4.5\n", "if num > 0:\n", " print(\"Positive number\")\n", "elif num == 0:\n", " print(\"Zero\")\n", "else:\n", " print(\"Negative number\")\n", "\n", "# Example of if-else statement\n", "age = 18\n", "\n", "if age >= 18:\n", " print(\"You are eligible to vote.\")\n", "else:\n", " print(\"You are not eligible to vote yet.\")\n", "\n", "# Example of nested if statement\n", "x = 10\n", "y = 5\n", "\n", "if x > 5:\n", " print(\"x is greater than 5\")\n", "\n", " if y > 2:\n", " print(\"y is greater than 2\")\n", " else:\n", " print(\"y is not greater than 2\")\n", "\n", "else:\n", " print(\"x is not greater than 5\")\n", "\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Rs_vluwJHgS7", "outputId": "a7e7d7fa-761f-42b8-dd78-b2ae87a24a8a" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "price is greater than 100\n", "Amount is between 200 and 300\n", "x is greater than y\n", "x is greater than y\n", "x is same as y\n", "x is greater than or equal to y\n", "Shipping Cost is $25\n", "x is less than y\n", "5\n", "5\n", "3\n", "3\n", "2\n", "2\n", "5\n", "0\n", "Quadrant IV\n", "True\n", "False\n", "True\n", "False\n", "True\n", "3 is a positive number.\n", "This is always printed.\n", "Positive or Zero\n", "Positive number\n", "You are eligible to vote.\n", "x is greater than 5\n", "y is greater than 2\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **Types of Control Flow in Python**\n", "Several types of control flow are given below:\n", "\n", "a. The if statement\n", "\n", "b. The if-else statement\n", "\n", "c.The nested-if statement\n", "\n", "d. The if-elif-else ladder\n", "\n", "Python's if-elif-else conditional statement syntax\n", "How to build multi-condition decision trees\n", "Practical applications with number classification and grading systems\n", "Comparison operators and logical expressions\n", "Best practices for writing clean conditional code\n", "Common pitfalls and how to avoid them\n", "Testing conditional logic with various inputs\n", "\n" ], "metadata": { "id": "0ToYz7snHgc8" } }, { "cell_type": "code", "source": [ "# Example of if-else-else ladder\n", "x = 15\n", "\n", "if x > 10:\n", " print(\"x is greater than 10\")\n", "elif x > 5:\n", " print(\"x is greater than 5 but not 10\")\n", "else:\n", " print(\"x is not greater than 5\")\n", "\n", "# Shorthand version using ternary conditional operator\n", "x = 8\n", "\n", "result = \"x is greater than 5\" if x > 5 else \"x is not greater than 5\"\n", "\n", "print(result)\n", "\n", "x = 10\n", "y = 20\n", "\n", "if x > y:\n", " print(\"x is greater than y\")\n", "else:\n", " print(\"y is greater than x\")\n", "\n", "number = int(input(\"Enter a number: \"))\n", "\n", "if number > 10:\n", " print(\"Number is greater than 10\")\n", "\n", "\n", "number = int(input(\"Enter a number: \"))\n", "if number > 10:\n", " print(\"statement 1\")\n", " print(\"statement 2\")\n", " print(\"statement 3\")\n", "print(\"Executes every time you run the program\")\n", "print(\"Program ends here\")\n", "\n", "radius = int(input(\"Enter radius: \"))\n", "if radius >= 0:\n", " print(\"Circumference = \", 2 * 3.14 * radius)\n", " print(\"Area = \", 3.14 * radius ** 2 )\n", "else:\n", " print(\"Please enter a positive number\")\n", "\n", "radius = int(input(\"Enter radius: \"))\n", "if radius >= 0:\n", " print(\"Circumference = \", 2 * 3.14 * radius)\n", " print(\"Area = \", 3.14 * radius ** 2 )\n", "\n", "else:\n", " print(\"Please enter a positive number\")\n", "\n", "\n", "radius = int(input(\"Enter radius: \"))\n", "if radius >= 0:\n", " print(\"Circumference = \", 2 * 3.14 * radius)\n", " print(\"Area = \", 3.14 * radius ** 2 )\n", "\n", "else:\n", " print(\"Please enter a positive number\")\n", "\n", "gre_score = int(input(\"Enter your GRE score: \"))\n", "per_grad = int(input(\"Enter percent secured in graduation: \"))\n", "\n", "if per_grad > 70:\n", " # outer if block\n", " if gre_score > 150:\n", " # inner if block\n", " print(\"Congratulations you are eligible for loan\")\n", "else:\n", " print(\"Sorry, you are not eligible for loan\")\n", "\n", "score = int(input(\"Enter your marks: \"))\n", "\n", "if score >= 90:\n", " print(\"Excellent ! Your grade is A\")\n", "else:\n", " if score >= 80:\n", " print(\"Great ! Your grade is B\")\n", " else:\n", " if score >= 70:\n", " print(\"Good ! Your grade is C\")\n", " else:\n", " if score >= 60:\n", " print(\"Your grade is D. You should work hard on you subjects.\")\n", " else:\n", " print(\"You failed in the exam\")\n", "\n", "\n", "x = 1\n", "if x > 0:\n", " print(\"Hello, World!\")\n", "\n", "\n", "x = 3\n", "if x % 2 == 0:\n", " print(\"Hello\")\n", "else:\n", " print(\"World\")\n", "\n", "x = 3\n", "if x == 1:\n", " print(\"A\")\n", "elif x == 2:\n", " print(\"B\")\n", "else:\n", " print(\"C\")\n", "\n", "x = 1\n", "y = 2\n", "if x == 1:\n", " if y > 0:\n", " print(\"A\")\n", " else:\n", " print(\"B\")\n", "else:\n", " print(\"B\")\n", "\n", "num = 5\n", "\n", "if num > 0:\n", " print(\"The number is positive.\")\n", "\n", "num = -5\n", "\n", "if num > 0:\n", " print(\"The number is positive.\")\n", "else:\n", " print(\"The number is negative.\")\n", "\n", "num = 4\n", "\n", "if num % 2 == 0:\n", " print(\"The number is even.\")\n", "else:\n", " print(\"The number is odd.\")\n", "\n", "score = 85\n", "\n", "if score >= 90:\n", " grade = \"A\"\n", "elif score >= 80:\n", " grade = \"B\"\n", "elif score >= 70:\n", " grade = \"C\"\n", "elif score >= 60:\n", " grade = \"D\"\n", "else:\n", " grade = \"F\"\n", "\n", "print(\"Your grade is:\", grade)\n", "\n", "year = 2000\n", "\n", "if year % 4 == 0:\n", " if year % 100 == 0:\n", " if year % 400 == 0:\n", " print(year, \"is a leap year.\")\n", " else:\n", " print(year, \"is not a leap year.\")\n", " else:\n", " print(year, \"is a leap year.\")\n", "else:\n", " print(year, \"is not a leap year.\")\n", "\n", "\n", "string = \"hello, world\"\n", "char = \"w\"\n", "\n", "if char in string:\n", " print(\"The string contains the character\", char)\n", "else:\n", " print(\"The string does not contain the character\", char)\n", "\n", "num = int(input(\"Enter a number: \"))\n", "\n", "if num > 0:\n", " print(\"The number is positive.\")\n", "else:\n", " print(\"The number is negative.\")\n", "\n", "\n", "# Prompt the user to enter a number\n", "number = float(input(\"Enter a number: \"))\n", "\n", "# Check if the number is positive, negative, or zero\n", "if number > 0:\n", " print(\"Positive\")\n", "elif number < 0:\n", " print(\"Negative\")\n", "else:\n", " print(\"Zero\")\n", "\n", "weight = float(input(\"Enter your weight (kg): \"))\n", "height = float(input(\"Enter your height (m): \"))\n", "\n", "bmi = weight / (height ** 2)\n", "\n", "print(f\"Your BMI is: {bmi:.2f}\")\n", "\n", "if bmi < 18.5:\n", " print(\"Category: Underweight\")\n", " print(\"Recommendation: Consider consulting a healthcare provider\")\n", "elif bmi < 25:\n", " print(\"Category: Normal weight\")\n", " print(\"Recommendation: Maintain your current lifestyle\")\n", "elif bmi < 30:\n", " print(\"Category: Overweight\")\n", " print(\"Recommendation: Consider a balanced diet and exercise\")\n", "else:\n", " print(\"Category: Obese\")\n", " print(\"Recommendation: Consult a healthcare provider for guidance\")\n", "\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rwhykMvuHgmL", "outputId": "afde3340-0645-4fdb-fd46-152884b7ccf1" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "x is greater than 10\n", "x is greater than 5\n", "y is greater than x\n", "Enter a number: 5\n", "Enter a number: 2\n", "Executes every time you run the program\n", "Program ends here\n", "Enter radius: 6\n", "Circumference = 37.68\n", "Area = 113.04\n", "Enter radius: 6\n", "Circumference = 37.68\n", "Area = 113.04\n", "Enter radius: 3\n", "Circumference = 18.84\n", "Area = 28.26\n", "Enter your GRE score: 2\n", "Enter percent secured in graduation: 3\n", "Sorry, you are not eligible for loan\n", "Enter your marks: 6\n", "You failed in the exam\n", "Hello, World!\n", "World\n", "C\n", "A\n", "The number is positive.\n", "The number is negative.\n", "The number is even.\n", "Your grade is: B\n", "2000 is a leap year.\n", "The string contains the character w\n", "Enter a number: 5\n", "The number is positive.\n", "Enter a number: 3\n", "Positive\n", "Enter your weight (kg): 20\n", "Enter your height (m): 30\n", "Your BMI is: 0.02\n", "Category: Underweight\n", "Recommendation: Consider consulting a healthcare provider\n" ] } ] }, { "cell_type": "markdown", "source": [ "# **How to Use the if Statement in Python**\n", "The if statement allows you to execute a block of code if a certain condition is true. Here's the basic syntax:\n", "\n", "if condition:\n", " # code to execute if condition is true\n", "The condition can be any expression that evaluates to a Boolean value (True or False). If the condition is True, the code block indented below the if statement will be executed. If the condition is False, the code block will be skipped.\n", "\n", "Here's an example of how to use an if statement to check if a number is positive:" ], "metadata": { "id": "oVyA9AiQHgvU" } } ] }