{ "cells": [ { "cell_type": "markdown", "id": "073156f3-d677-4b8d-8d4a-ff573201dd49", "metadata": {}, "source": [ "## Operators" ] }, { "cell_type": "markdown", "id": "bcd214de", "metadata": {}, "source": [ "### Comparison Operators" ] }, { "cell_type": "markdown", "id": "71764afc", "metadata": {}, "source": [ "Python comparison operators are used to compare two values and return a boolean value (True or False).\n", "\n", "**The six primary comparison operators are:**\n", "\n", "equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=)." ] }, { "cell_type": "code", "execution_count": null, "id": "a73f06ae-7505-42f6-9d4a-1ccc1be9c07b", "metadata": {}, "outputs": [], "source": [ "# Python program to demonstrate comparison operators\n", "\n", "def compare_numbers(a, b):\n", " print(f\"Comparing {a} and {b}:\")\n", " \n", " # Equal to (==)\n", " if a == b:\n", " print(f\"{a} is equal to {b}\")\n", " else:\n", " print(f\"{a} is not equal to {b}\")\n", " \n", " # Not equal to (!=)\n", " if a != b:\n", " print(f\"{a} is not equal to {b}\")\n", " else:\n", " print(f\"{a} is equal to {b}\")\n", " \n", " # Greater than (>)\n", " if a > b:\n", " print(f\"{a} is greater than {b}\")\n", " else:\n", " print(f\"{a} is not greater than {b}\")\n", " \n", " # Less than (<)\n", " if a < b:\n", " print(f\"{a} is less than {b}\")\n", " else:\n", " print(f\"{a} is not less than {b}\")\n", " \n", " # Greater than or equal to (>=)\n", " if a >= b:\n", " print(f\"{a} is greater than or equal to {b}\")\n", " else:\n", " print(f\"{a} is less than {b}\")\n", " \n", " # Less than or equal to (<=)\n", " if a <= b:\n", " print(f\"{a} is less than or equal to {b}\")\n", " else:\n", " print(f\"{a} is greater than {b}\")\n", "\n", "\n", "print(\"Example 1:\")\n", "compare_numbers(10, 5)\n", "\n", "print(\"\\nExample 2:\")\n", "compare_numbers(7, 7)\n", "\n", "print(\"\\nExample 3:\")\n", "compare_numbers(3, 8)" ] }, { "cell_type": "markdown", "id": "a6cda3b9", "metadata": {}, "source": [ "### Logical Operators" ] }, { "cell_type": "code", "execution_count": null, "id": "f3c6b876-5528-4490-8bac-badbe390c58c", "metadata": {}, "outputs": [], "source": [ "#Logical operators with for loop\n", "\n", "numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "\n", "for num in numbers:\n", " if num > 5 and num % 2 == 0:\n", " print(f\"{num} is greater than 5 and its even number\")\n", " elif num <= 5 or num % 2 != 0:\n", " print(f\"{num} is less than or equal to 5, or its odd number\")" ] }, { "cell_type": "code", "execution_count": null, "id": "643ed6a8-a097-4250-b6f7-10cf7f9db377", "metadata": {}, "outputs": [], "source": [ "# Logical operators with if-else\n", "\n", "age = 19\n", "has_license = False\n", "\n", "if age >= 18 and has_license:\n", " print(\"You can drive a car\")\n", "elif age >= 18 and not has_license:\n", " print(\"You need to get a license first\")\n", "else:\n", " print(\"You're too young to drive\")" ] }, { "cell_type": "code", "execution_count": null, "id": "38e4382a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3aa55ca9", "metadata": {}, "outputs": [], "source": [ "# logical \"in\" operator\n", "\n", "customers = [\"Alice\", \"Bob\", \"Charlie\", \"David\"]\n", "\n", " # Check if a customer is in the list\n", "print(\"Bob\" in customers) # Output: True\n", "print(\"Eve\" in customers) # Output: False" ] }, { "cell_type": "markdown", "id": "fc160bae", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d07e365f-275e-4dbd-af59-add3d350dbcf", "metadata": {}, "outputs": [], "source": [ "def check_temperature(temp):\n", " \"\"\"\n", " Classifies a temperature based on different ranges.\n", " \n", " Parameter:\n", " temp - temperature value in Fahrenheit\n", " \"\"\"\n", " print(f\"Checking temperature: {temp}°F\")\n", " \n", " if temp < 32:\n", " print(\"It's freezing - water will turn to ice.\")\n", " \n", " # Traditional way using 'and'\n", " if temp >= 32 and temp <= 50:\n", " print(\"Method 1: It's cold - wear a heavy jacket.\")\n", " \n", " # Python's elegant chained comparison\n", " if 32 <= temp <= 50:\n", " print(\"Method 2: It's cold - wear a heavy jacket.\")\n", " \n", " # Traditional way for multiple conditions\n", " if temp > 50 and temp < 70:\n", " print(\"Method 1: It's cool - a light jacket will do.\")\n", " \n", " # Chained comparison for multiple conditions\n", " if 50 < temp < 70:\n", " print(\"Method 2: It's cool - a light jacket will do.\")\n", " \n", " # Even longer chains work perfectly\n", " if 70 <= temp <= 85:\n", " print(\"It's pleasant - perfect weather!\")\n", " \n", " # You can mix different comparison operators\n", " if 85 < temp <= 95:\n", " print(\"It's hot - time for shorts and t-shirts.\")\n", " \n", " if temp > 95:\n", " print(\"It's extremely hot - stay hydrated and seek shade.\")\n", "\n", "# Test with different temperatures\n", "print(\"Test 1:\")\n", "check_temperature(31)\n", "\n", "print(\"\\nTest 2:\")\n", "check_temperature(75)\n", "\n", "print(\"\\nTest 3:\")\n", "check_temperature(92)" ] } ], "metadata": { "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.14.2" } }, "nbformat": 4, "nbformat_minor": 5 }