{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "# simple if\n", "x = 7\n", "if x > 5:\n", " print(\"x is greater than 5\")\n", "\n", "'''\n", "No parentheses are needed around the condition. (But if added means also its fine!)\n", "A colon : follows the if statement. (mandatory)\n", "Indentation (usually 4 spaces) is used to indicate the block of code to run.\n", "'''" ], "metadata": { "id": "T2ieoglx5sSK", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "b2f318b2-b882-4f57-a5f7-a5e8c887ad50" }, "execution_count": 1, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "x is greater than 5\n" ] } ] }, { "cell_type": "code", "source": [ "# But if there is only one line in the if, no need to write in next line\n", "y = 50\n", "if y % 5 == 0: print(f\"{y} is divisible by 5!\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "mX3_CvNar21G", "outputId": "0059e7c7-e3cf-41f1-c36d-0a50b5fe1763" }, "execution_count": 2, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "50 is divisible by 5!\n" ] } ] }, { "cell_type": "code", "source": [ "# if else\n", "num = int(input())\n", "if num % 2 == 0: print(\"Even number\")\n", "else: print(\"Odd number\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AfCOGuxXr23l", "outputId": "982ff209-f8fc-481f-8eba-68a4c89e6958" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2025\n", "Odd number\n" ] } ] }, { "cell_type": "code", "source": [ "# multiple statements in single line but WITHOUT Indentation\n", "age = 7\n", "\n", "if age >= 18: print(f\"You are {age} years old.\"); print(\"You are eligible to vote!\") # multiple statements separated with ;\n", "else:\n", " print(f\"You are {age} years old.\");\n", " print(\"You are a minor.\")\n", " years = 18 - age\n", " print(f\"You will be eligible to vote after {years} years.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TQpC9fotr26F", "outputId": "cbad3778-fc79-4d03-eba1-754988e05115" }, "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "You are 7 years old.\n", "You are a minor.\n", "You will be eligible to vote after 11 years.\n" ] } ] }, { "cell_type": "code", "source": [ "# if-elif-else\n", "marks = int(input(\"Enter your marks out of 100: \"))\n", "if marks > 100 or marks < 0:\n", " print(\"Invalid Marks.\")\n", "elif marks >= 90:\n", " print(\"Grade: A\")\n", "elif marks >= 80:\n", " print(\"Grade: B\")\n", "elif marks >= 70:\n", " print(\"Grade: C\")\n", "elif marks >= 60:\n", " print(\"Grade: D\")\n", "elif marks >= 50:\n", " print(\"Grade: E\")\n", "else:\n", " print(\"Grade: F\")\n", "\n", "'''\n", "The program checks from top to bottom.\n", "As soon as a condition is True, it runs that block and skips the rest.\n", "'''" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "E4DFVZMRr28k", "outputId": "6a302059-152f-437d-ce6a-82c44b85a3f2" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter your marks out of 100: 97\n", "Grade: A\n" ] } ] }, { "cell_type": "code", "source": [ "# Nested if else\n", "num = 7\n", "if num >= 0:\n", " if num % 2 == 0:\n", " print(\"The number is positive and even.\")\n", " else:\n", " print(\"The number is positive and odd.\")\n", "else:\n", " if num % 2 == 0:\n", " print(\"The number is negative and even.\")\n", " else:\n", " print(\"The number is negative and odd.\")\n", "\n", "'''\n", "First, it checks if the number is positive or negative.\n", "Inside each case, it then checks if the number is even or odd.\n", "'''" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "RvI2CYJmr2_G", "outputId": "522f5442-c87b-4093-b69b-4f0fa00e1faf" }, "execution_count": 6, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The number is positive and odd.\n" ] } ] }, { "cell_type": "code", "source": [ "# Single line if else\n", "a = 7\n", "b = 18\n", "print(a if a > b else b) # greatest of two numbers" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "fhp94SsOr3Bt", "outputId": "800f8d48-1bdf-4b11-b815-9997a2ced8b9" }, "execution_count": 7, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "18\n" ] } ] }, { "cell_type": "code", "source": [ "# if-else with not for Negation\n", "is_raining = False\n", "if not is_raining:\n", " print(\"It is not raining today.\")\n", "else:\n", " print(\"Take an umbrella.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XcdM9NxWzm0u", "outputId": "26510005-3e67-4af2-d329-3c8b59707bea" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "It is not raining today.\n" ] } ] }, { "cell_type": "code", "source": [ "# FizzBuzz Program with logical mistake\n", "\n", "num = 15\n", "\n", "if num % 3 == 0:\n", " print(\"Fizz\")\n", "elif num % 5 == 0:\n", " print(\"Buzz\")\n", "elif num % 3 == 0 and num % 5 == 0:\n", " print(\"FizzBuzz\")\n", "else:\n", " print(num)\n", "\n", "'''\n", "First if condition (num % 3 == 0): This checks if the number is divisible by 3.\n", "For numbers like 3, 6, 9, 12, it prints \"Fizz\". This part works fine.\n", "\n", "Second elif condition (num % 5 == 0): This checks if the number is divisible by 5.\n", "If the number is 5, 10, 20, it prints \"Buzz\". This is also correct.\n", "\n", "Third elif condition (num % 3 == 0 and num % 5 == 0): This checks if the number is divisible by both 3 and 5.\n", "Here's the mistake: This condition will never execute because of the order of the conditions.\n", "If a number is divisible by both 3 and 5, the first if (num % 3 == 0) will already be true, and the program will never reach the third elif block.\n", "\n", "Example: For num = 15, the first if condition will be True, so \"Fizz\" will be printed, and the program will stop there, without checking the third condition.\n", "'''" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bFVWiHHdr3EF", "outputId": "afe6b863-d234-47f9-b5df-bf16dbe3ae9f" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Fizz\n" ] } ] }, { "cell_type": "code", "source": [ "# Corrected Version\n", "num = 7\n", "\n", "# Check if divisible by both 3 and 5 first\n", "if num % 3 == 0 and num % 5 == 0:\n", " print(\"FizzBuzz\")\n", "# If not, check if divisible by 3\n", "elif num % 3 == 0:\n", " print(\"Fizz\")\n", "# If not, check if divisible by 5\n", "elif num % 5 == 0:\n", " print(\"Buzz\")\n", "# If none of the above, print the number\n", "else:\n", " print(num)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JN06kXt2r3Gn", "outputId": "930a87e7-8d33-4561-c8fa-93885237e0b7" }, "execution_count": 10, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "7\n" ] } ] }, { "cell_type": "code", "source": [ "# Single Line FizzBuzz\n", "num = 10\n", "print(\"Fizz\" if num % 3 == 0 else \"Buzz\" if num % 5 == 0 else num)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MPVcD7CQr3JI", "outputId": "5dcf81b4-df78-4d47-cf3f-913f4067cb03" }, "execution_count": 11, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Buzz\n" ] } ] }, { "cell_type": "code", "source": [ "# Check Whether a Number is a Perfect Square\n", "import math\n", "\n", "num = 49\n", "\n", "if num < 0:\n", " print(\"Negative numbers cannot be perfect squares.\")\n", "else:\n", " square_root = math.sqrt(num)\n", " if square_root == int(square_root):\n", " print(f\"{num} is a Perfect Square.\")\n", " else:\n", " print(f\"{num} is Not a Perfect Square.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wPYtBMYur3Lt", "outputId": "5bdcba17-b028-46a3-da44-6908b4cc6576" }, "execution_count": 12, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "49 is a Perfect Square.\n" ] } ] }, { "cell_type": "code", "source": [ "# Leap Year Checker - using nested if else\n", "year = 2025\n", "\n", "if year % 4 == 0:\n", " if year % 100 == 0:\n", " if year % 400 == 0:\n", " print(f\"{year} is a Leap Year.\")\n", " else:\n", " print(f\"{year} is Not a Leap Year.\")\n", " else:\n", " print(f\"{year} is a Leap Year.\")\n", "else:\n", " print(f\"{year} is Not a Leap Year.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dn10yDuzr3OQ", "outputId": "12456a1d-f7af-41e2-f8c8-97a2ec90162c" }, "execution_count": 13, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2025 is Not a Leap Year.\n" ] } ] }, { "cell_type": "code", "source": [ "# Leap Year Checker - using if elif else\n", "year = 2100\n", "\n", "if year % 4 == 0 and year % 100 != 0:\n", " print(f\"{year} is a Leap Year.\")\n", "elif year % 400 == 0:\n", " print(f\"{year} is a Leap Year.\")\n", "else:\n", " print(f\"{year} is Not a Leap Year.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2M3XCS7Zr3Qm", "outputId": "e197e3da-d8d5-437a-b8ea-e7d6907dfaab" }, "execution_count": 14, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2100 is Not a Leap Year.\n" ] } ] }, { "cell_type": "code", "source": [ "# Leap Year Checker - using if else with Chained Conditions\n", "year = 2024\n", "\n", "if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):\n", " print(f\"{year} is a Leap Year.\")\n", "else:\n", " print(f\"{year} is Not a Leap Year.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rxoV0VAWr3TN", "outputId": "df5bd3c1-adac-4565-bab2-9f1ca855688f" }, "execution_count": 15, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2024 is a Leap Year.\n" ] } ] }, { "cell_type": "code", "source": [ "# Leap Year Checker - one line if else\n", "year = 2000\n", "print(f\"{year} is a Leap Year.\" if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else f\"{year} is Not a Leap Year.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "MXAdhWgmxcsR", "outputId": "df83d2f0-0441-4e42-e06b-b97f0eb7182e" }, "execution_count": 16, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "2000 is a Leap Year.\n" ] } ] }, { "cell_type": "code", "source": [ "'''\n", "Purchase Amount: The user is prompted to enter the total purchase amount.\n", "\n", "Discount Calculation: The program uses if-elif-else to determine the applicable discount based on the amount.\n", "\n", "If the purchase amount is less than ₹500, there’s no discount.\n", "If the amount is between ₹500 and ₹999, a 10% discount is applied.\n", "A 20% discount is given for amounts between ₹1000 and 1999.\n", "If the amount is ₹2000 or more, a 30% discount is applied.\n", "\n", "Final Price: After calculating the discount, the program displays the final price after applying the discount.\n", "'''" ], "metadata": { "id": "FFevhgFgxnJp" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "amount = float(input(\"Enter your purchase amount: ₹\"))\n", "\n", "if amount < 50:\n", " discount = 0\n", " print(f\"Your purchase amount is ₹{amount:.2f}. You get no discount.\")\n", "elif amount >= 500 and amount <= 999:\n", " discount = 0.10\n", " print(f\"Your purchase amount is ₹{amount:.2f}. You get a 10% discount.\")\n", "elif amount >= 1000 and amount <= 1999:\n", " discount = 0.20\n", " print(f\"Your purchase amount is ₹{amount:.2f}. You get a 20% discount.\")\n", "else:\n", " discount = 0.30\n", " print(f\"Your purchase amount is ₹{amount:.2f}. You get a 30% discount.\")\n", "\n", "dis = (amount * discount)\n", "bill = amount - dis\n", "print(f\"The discounted amount is ₹{(dis):.2f}.\")\n", "print(f\"Your final price after discount is: ₹{bill:.2f}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "krNFD0qUxnMh", "outputId": "52965cf9-e4f7-4281-fede-6d93efd358b0" }, "execution_count": 17, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter your purchase amount: ₹1800\n", "Your purchase amount is ₹1800.00. You get a 20% discount.\n", "The discounted amount is ₹360.00.\n", "Your final price after discount is: ₹1440.00\n" ] } ] }, { "cell_type": "code", "source": [ "# A simple Quiz Application\n", "score = 0\n", "\n", "a1 = input(\"The `**` operator in Python is used for multiplication. (True/False): \")\n", "if a1 == \"false\" or a1 == \"False\" or a1 == \"FALSE\" or a1 == \"F\" or a1 == \"f\":\n", " score += 1\n", "\n", "a2 = input(\"In Python, the `int()` function can convert a string containing a number into an integer. (True/False): \")\n", "if a2 == \"true\" or a2 == \"True\" or a2 == \"TRUE\" or a2 == \"T\" or a2 == \"T\":\n", " score += 1\n", "\n", "a3 = input(\"In Python, the `input()` function always returns a string. (True/False): \")\n", "if a3 == \"true\" or a3 == \"True\" or a3 == \"TRUE\" or a3 == \"T\" or a3 == \"T\":\n", " score += 1\n", "\n", "print(f\"\\nYour final score is: {score} / 3.\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9J2Xotp31bHK", "outputId": "81be2371-e6ee-4eca-b197-0329a68060b7" }, "execution_count": 18, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The `**` operator in Python is used for multiplication. (True/False): False\n", "In Python, the `int()` function can convert a string containing a number into an integer. (True/False): T\n", "In Python, the `input()` function always returns a string. (True/False): True\n", "\n", "Your final score is: 3 / 3.\n" ] } ] } ] }