{
"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 (=), and less than or equal to ()\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 greater than or equal to {b}\")\n",
" else:\n",
" print(f\"{a} is less than {b}\")\n",
" \n",
" # Less than or equal to (= 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