{
"cells": [
{
"cell_type": "markdown",
"id": "3ba50323",
"metadata": {},
"source": [
"# Lecture 7 - IF ELSE Statements\n",
"Mar 2, 2023 - Morning"
]
},
{
"cell_type": "markdown",
"id": "a3f38ecf",
"metadata": {},
"source": [
"The IF statement creates a condition. If that condition is true, then the indented code is executed."
]
},
{
"cell_type": "markdown",
"id": "07b9220d",
"metadata": {},
"source": [
"## 7.1 Use IF and Else\n",
"If marks are greater than 59, print PASS, otherwise print FAIL"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fc5d0f74",
"metadata": {},
"outputs": [],
"source": [
"marks = 59"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "02b33661",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FAIL\n"
]
}
],
"source": [
"if marks > 59 :\n",
" print(\"PASS\")\n",
"else:\n",
" print(\"FAIL\")"
]
},
{
"cell_type": "markdown",
"id": "b8aa401e",
"metadata": {},
"source": [
"## 7.2 Convert the above code to a function\n",
"give this function a name of grade"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a17adfe0",
"metadata": {},
"outputs": [],
"source": [
"def grade(marks):\n",
" if marks > 59:\n",
" print(\"PASS\")\n",
" else:\n",
" print(\"FAIL\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "06c17cce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"FAIL\n"
]
}
],
"source": [
"# Call the function\n",
"grade(40)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "35edac63",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"PASS\n"
]
}
],
"source": [
"a = grade(80)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "81d0a9f1",
"metadata": {},
"outputs": [],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"id": "2ba33d11",
"metadata": {},
"source": [
"## 7.3 Function with return\n",
"Create a function that will return results instead just printing
\n",
"### Why return?\n",
"The above function does not store the results and does not return anything, it just prints. When we call it, we cannot store its result in a variable. If we add a return statement to it, we shall then be able to store its results."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2836213c",
"metadata": {},
"outputs": [],
"source": [
"def grade(marks):\n",
" \n",
" if marks > 59:\n",
" result = \"PASS\"\n",
" \n",
" else:\n",
" result = \"FAIL\"\n",
" \n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3385f3e6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'FAIL'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grade(40)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d04aa320",
"metadata": {},
"outputs": [],
"source": [
"a = grade(40)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "773951c7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'FAIL'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "markdown",
"id": "21f0cfcf",
"metadata": {},
"source": [
"## 7.4 if, elif, else\n",
"If we have more than two results, then use the following statements
\n",
"if
\n",
"elif
\n",
"else
"
]
},
{
"cell_type": "markdown",
"id": "de3b91e8",
"metadata": {},
"source": [
"Suppose we have a grading system of the following grade:
\n",
"marks greater than 80 will have \"A\" grade
\n",
"marks below 80, but greater than 59, \"B\" grade
\n",
"below 59 will have \"F\""
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "1cbb3055",
"metadata": {},
"outputs": [],
"source": [
"if marks > 80:\n",
" result = \"A\"\n",
"elif marks > 59:\n",
" result = \"B\"\n",
"else:\n",
" result = \"F\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "af98c7d2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"F\n"
]
}
],
"source": [
"print(result)"
]
},
{
"cell_type": "markdown",
"id": "f6d11d62",
"metadata": {},
"source": [
"## 7.5 Convert the above code a function\n",
"name the function as grade2"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "984ed842",
"metadata": {},
"outputs": [],
"source": [
"def grade2(marks):\n",
" if marks > 80:\n",
" result = \"A\"\n",
" elif marks > 59:\n",
" result = \"B\"\n",
" else:\n",
" result = \"F\"\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "7edd34c3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'A'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grade2(98)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9e4c6294",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'B'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grade2(72)"
]
},
{
"cell_type": "markdown",
"id": "25111b67",
"metadata": {},
"source": [
"## 7.6 Create a list and call grade2\n",
"Create a list of marks and apply grade2 to each element of the list\n",
"Also create an empty list, name it grades.\n",
"Append the grade from grade2 function to it using .append() method"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "68ba4b56",
"metadata": {},
"outputs": [],
"source": [
"# Create a list of marks\n",
"marks = [56,76,98,87,65,87]\n",
"\n",
"# Create empty list\n",
"grades = []\n",
"\n",
"for i in range(0, len(marks)):\n",
" result = grade2(marks[i])\n",
" grades.append(result)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "cc568eba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['F', 'B', 'A', 'A', 'B', 'A']"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"grades"
]
},
{
"cell_type": "markdown",
"id": "2a34a3ac",
"metadata": {},
"source": [
"## 7.7 Pakistan income tax"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "2908eb99",
"metadata": {},
"outputs": [],
"source": [
"income = 3600000\n",
"\n",
"# If income is below 600000, tax is zero\n",
"if income < 600000 :\n",
" tax = 0\n",
"\n",
"# If income is below 1.2 million, then 2.5% of the exceeding 600000\n",
"elif income < 1200000 : \n",
" tax = (income - 600000) * 2.5 / 100\n",
"\n",
"# Third slab\n",
"elif income < 2400000 :\n",
" tax = (income - 1200000) * 12.5/100 + 15000\n",
"\n",
"# Fourth slab\n",
"elif income < 3600000 :\n",
" tax = (income - 2400000) * 20/100 + 165000\n",
" \n",
"# 5th slab\n",
"elif income < 6000000 :\n",
" tax = (income - 3600000) * 25/100 + 405000\n",
" \n",
"# 6th slab\n",
"elif income < 12000000 :\n",
" tax = (income - 6000000) * 32.5/100 + 1005000\n",
"\n",
"else :\n",
" tax = (income - 12000000) * 35/100 + 2955000"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "c2d2e2b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"405000.0\n"
]
}
],
"source": [
"print(tax)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "a2954375",
"metadata": {},
"outputs": [],
"source": [
"def pktax(income) :\n",
" # If income is below 600000, tax is zero\n",
" if income < 600000 :\n",
" tax = 0\n",
"\n",
" # If income is below 1.2 million, then 2.5% of the exceeding 600000\n",
" elif income < 1200000 : \n",
" tax = (income - 600000) * 2.5 / 100\n",
"\n",
" # Third slab\n",
" elif income < 2400000 :\n",
" tax = (income - 1200000) * 12.5/100 + 15000\n",
"\n",
" # Fourth slab\n",
" elif income < 3600000 :\n",
" tax = (income - 2400000) * 20/100 + 165000\n",
"\n",
" # 5th slab\n",
" elif income < 6000000 :\n",
" tax = (income - 3600000) * 25/100 + 405000\n",
"\n",
" # 6th slab\n",
" elif income < 12000000 :\n",
" tax = (income - 6000000) * 32.5/100 + 1005000\n",
"\n",
" else :\n",
" tax = (income - 12000000) * 35/100 + 2955000\n",
" return tax"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "23cab8e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pktax(500000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "24b4b79a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}