{
"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": 1,
"id": "a73f06ae-7505-42f6-9d4a-1ccc1be9c07b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example 1:\n",
"Comparing 10 and 5:\n",
"10 is not equal to 5\n",
"10 is not equal to 5\n",
"10 is greater than 5\n",
"10 is not less than 5\n",
"10 is greater than or equal to 5\n",
"10 is greater than 5\n",
"\n",
"Example 2:\n",
"Comparing 7 and 7:\n",
"7 is equal to 7\n",
"7 is equal to 7\n",
"7 is not greater than 7\n",
"7 is not less than 7\n",
"7 is greater than or equal to 7\n",
"7 is less than or equal to 7\n",
"\n",
"Example 3:\n",
"Comparing 3 and 8:\n",
"3 is not equal to 8\n",
"3 is not equal to 8\n",
"3 is not greater than 8\n",
"3 is less than 8\n",
"3 is less than 8\n",
"3 is less than or equal to 8\n"
]
}
],
"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": 2,
"id": "f3c6b876-5528-4490-8bac-badbe390c58c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 is less than or equal to 5, or its odd number\n",
"2 is less than or equal to 5, or its odd number\n",
"3 is less than or equal to 5, or its odd number\n",
"4 is less than or equal to 5, or its odd number\n",
"5 is less than or equal to 5, or its odd number\n",
"6 is greater than 5 and its even number\n",
"7 is less than or equal to 5, or its odd number\n",
"8 is greater than 5 and its even number\n",
"9 is less than or equal to 5, or its odd number\n",
"10 is greater than 5 and its even number\n"
]
}
],
"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": 3,
"id": "643ed6a8-a097-4250-b6f7-10cf7f9db377",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You need to get a license first\n"
]
}
],
"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": 4,
"id": "d07e365f-275e-4dbd-af59-add3d350dbcf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test 1:\n",
"Checking temperature: 31°F\n",
"It's freezing - water will turn to ice.\n",
"\n",
"Test 2:\n",
"Checking temperature: 75°F\n",
"It's pleasant - perfect weather!\n",
"\n",
"Test 3:\n",
"Checking temperature: 92°F\n",
"It's hot - time for shorts and t-shirts.\n"
]
}
],
"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 [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}