dotfiles/.config/Code/User/History/401290ef/wEcI.ipynb
RafayAhmad7548 4f46de8d00 update
2024-09-09 16:59:28 +05:00

1335 lines
34 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "l0E95atrLpN8"
},
"source": [
"Prepared By Talha Tariq"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z6wATfzDt_tU"
},
"source": [
"# Introduction to Data Science Lab 03"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IeSpe8cot_tV"
},
"source": [
"## Table of Content\n",
"\n",
"* Revision and Tasks\n",
" * Loops\n",
" * Lists\n",
" * Dictionaries\n",
" * Tuples\n",
" * Set\n",
" * Lambda Function\n",
"* Functions\n",
"* Class\n",
"* Objects\n",
"* Inheritance\n",
"* Encapsulation\n",
"* Polymorphism\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "q0fgNFhpt_tc",
"outputId": "712e2867-d3cb-4f8d-b0af-586e4c18d50b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10.0\n"
]
}
],
"source": [
"import math\n",
"\n",
"x=100\n",
"y=math.sqrt(x)\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UONpMhF4L9f_"
},
"source": [
"## Loops"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "87taihG1t_tt"
},
"source": [
"Loops are a way to execute a block of code multiple times. There are two main types of loops: while loops and for loops."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0281aIHat_tt",
"outputId": "059159c8-e55d-4655-bb12-631400df578d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n"
]
}
],
"source": [
"i = 0\n",
"while i <= len('Program'):\n",
" print(i)\n",
" i += 1 # equivalent to i = i + 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 69
},
"id": "4cCOysfWL9gA",
"outputId": "560e46c7-279c-409a-838c-64bea8d321c4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"for p in range(2,10):\n",
" print(p)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RZaABIklt_tt",
"outputId": "2a048093-7f16-4736-c975-363f45fbb6c4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"a\n",
"a\n",
"a\n",
"a\n"
]
}
],
"source": [
"for x in \"bananarama\":\n",
" if x=='a':\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "apoQnvIqt_tw"
},
"source": [
"## Lists\n",
"A list is a collection of values enclosed in square brackets []. It can hold elements of different types.\n",
"\n",
"### List Characteristics:\n",
"\n",
"* Lists are ordered collections.\n",
"* Lists are mutable (can be changed after creation).\n",
"* Lists can contain duplicate elements."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7SPioMZ-t_tw",
"outputId": "daa550d7-3a8f-4668-8152-08c6d085c54d"
},
"outputs": [
{
"data": {
"text/plain": [
"['apple', 'banana']"
]
},
"execution_count": 139,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = [1, 2, 3, 'apple', 'banana'] # creating a list\n",
"\n",
"my_list #print a list\n",
"my_list[3] # print values from index 3 to 4 (4 exclusive)\n",
"my_list[2:5] # print values from index 2 to 5 (5 exclusive)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d0NX9A9Gt_tw",
"outputId": "b7eae649-f493-46c1-d353-9c73f267e9a8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"apple\n",
"banana\n"
]
}
],
"source": [
"for i in my_list: # iterate list by loop\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zgmgR91rt_tw",
"outputId": "248e79f0-b71c-4b21-e64c-3ad06746ca9d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16, 25]\n"
]
}
],
"source": [
"numbers = [1, 2, 3, 4, 5]\n",
"squared = [x**2 for x in numbers]\n",
"print(squared) # Output: [1, 4, 9, 16, 25]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "du2lL74Gt_tx",
"outputId": "84da410a-2478-447b-b332-ddf193044330"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9, 6, 5, 4, 3, 2, 1, 1]\n"
]
}
],
"source": [
"numbers = [3, 1, 4, 1, 5, 9, 2, 6]\n",
"numbers.sort()\n",
"numbers.reverse()\n",
"print(numbers)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dT8ZbYSWt_tx"
},
"source": [
"Further operations are here:\n",
"\n",
"* append() : Used for appending and adding elements to the end of the List.\n",
"\n",
"* copy() : It returns a shallow copy of a list. \n",
"\n",
"* clear() : This method is used for removing all items from the list. \n",
"\n",
"* count() : These methods count the elements. \n",
"\n",
"* extend() : Adds each element of the iterable to the end of the List. \n",
"\n",
"* index() : Returns the lowest index where the element appears. \n",
"\n",
"* insert() : Inserts a given element at a given index in a list. \n",
"\n",
"* pop() : Removes and returns the last value from the List or the given index value.\n",
"\n",
"* remove() : Removes a given object from the List. \n",
"\n",
"* reverse() : Reverses objects of the List in place. \n",
"\n",
"* sort() : Sort a List in ascending, descending, or user-defined order.\n",
"\n",
"* min() : Calculates the minimum of all the elements of the List. \n",
"\n",
"* max() : Calculates the maximum of all the elements of the List. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kjXGGatYIMJI",
"outputId": "2040b4b6-cb92-4922-a707-c4992346206d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"array('i', [2, 5, 7, 4, 5, 1, 11, 2, 5])\n"
]
}
],
"source": [
"# Array\n",
"import array as arr\n",
"data_array = arr.array(\"i\", [2,5,7,4,5,1,11,2,5])\n",
"print(data_array)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "hduhQq7mIOGR",
"outputId": "e38ad26e-f636-4d2f-9917-021202edbcb0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1, 2, 4, 5, 7, 11}\n"
]
}
],
"source": [
"# Set\n",
"# Does not allow duplicate values.\n",
"data_set = {2,5,7,4,5,1,11}\n",
"print(data_set)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tJD2HDKCIQI7",
"outputId": "0337c92b-fad7-483b-ac06-8d6f9989647b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('apple', 'banana', 'cherry')\n"
]
}
],
"source": [
"# Tuple\n",
"# Tuple items are ordered, unchangeable, and allow duplicate values.\n",
"data_tuple = (\"apple\", \"banana\", \"cherry\")\n",
"print(data_tuple)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "axQ4OAJ_t_tx"
},
"source": [
"# Dictionaries\n",
"Dictionaries allow you to associate values with unique keys, enabling efficient data retrieval and manipulation. Dictionaries are unordered collections of key-value pairs. Each key is unique and associated with a value. Dictionaries are created using curly braces {} and key-value pairs separated by colons :."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Yy7_C-0qt_ty"
},
"source": [
"## Creating and Accessing Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sZaAaG-Zt_tz"
},
"outputs": [],
"source": [
"# Creating a dictionary\n",
"student = {\n",
" \"name\": \"John\",\n",
" \"age\": 20,\n",
" \"major\": \"Computer Science\"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8z9eAbt5t_tz",
"outputId": "a534f67e-502e-4d02-e037-8a13d52d02a5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"John\n",
"20\n"
]
}
],
"source": [
"print(student[\"name\"])\n",
"print(student[\"age\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MJbPDTUnt_tz",
"outputId": "5318a980-109c-4733-aea0-9852cf541cab"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'John', 'age': 20, 'major': 'Computer Science'}\n"
]
}
],
"source": [
"print(student)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "PJGKBMqmt_t0"
},
"source": [
"### Manipulating Dictionaries\n",
"#### Modifying Values:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "s-xWE6D5t_t1",
"outputId": "b7471b2a-55c5-4abf-b40e-3646875ee02a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"26\n"
]
}
],
"source": [
"student[\"age\"] = 26\n",
"print(student[\"age\"])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zxNZ1sk9t_t1"
},
"source": [
"#### Adding New Key-Value Pairs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UsMucR5ut_t1",
"outputId": "7d4b9a69-e59b-40ef-8c34-7e11b0b9f95d"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'John', 'age': 26, 'major': 'Computer Science', 'city': 'New York'}\n"
]
}
],
"source": [
"student[\"city\"] = \"New York\"\n",
"print(student)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0ZQlR2Kat_t1"
},
"source": [
"#### Deleting Key-Value Pairs:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "h99OrEmmt_t1",
"outputId": "00bee6dc-189e-4818-bc27-352c3fd3eea1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'John', 'major': 'Computer Science', 'city': 'New York'}\n"
]
}
],
"source": [
"del student[\"age\"]\n",
"print(student)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yAkZik3Ht_t2",
"outputId": "d8203db3-fe2f-4ccf-bdab-e757e09cc180"
},
"outputs": [
{
"data": {
"text/plain": [
"'New York'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"student.pop(\"city\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6RgEieyGt_t2",
"outputId": "400dc240-d1a9-468a-9d56-35f8356ab247"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'John', 'major': 'Computer Science'}\n"
]
}
],
"source": [
"print(student)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-jI_fO6Mt_t2"
},
"source": [
"#### Dictionary Methods:\n",
"* keys(): Returns a list of all keys in the dictionary.\n",
"* values(): Returns a list of all values in the dictionary.\n",
"* items(): Returns a list of tuples, each containing a key-value pair."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ouj3J5AVt_t2",
"outputId": "0fe355ac-4de0-4cb2-bd87-7c8f620a7786"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dict_keys(['name', 'major'])\n",
"dict_values(['John', 'Computer Science'])\n",
"dict_items([('name', 'John'), ('major', 'Computer Science')])\n"
]
}
],
"source": [
"print(student.keys())\n",
"print(student.values())\n",
"print(student.items())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "UUjkefO3t_t3",
"outputId": "a690a8f6-6368-4df4-c084-77a0d6211e5a"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"machanic\n",
"electrician\n",
"True\n",
"23.5\n"
]
}
],
"source": [
"tools={\"machanic\":[\"Hammer\", \"Screwdriver\", \"etc\"],\n",
" \"electrician\": [\"tester\",\"wires\", \"holders\"],\n",
" True:False,\n",
" 23.5:\"b\"}\n",
"for m in tools:\n",
" print(m)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7yEkvUo_t_t3",
"outputId": "0ad044e6-8b81-4044-abe3-94353d48cc17"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Hammer', 'Screwdriver', 'etc']\n",
"['tester', 'wires', 'holders']\n",
"False\n",
"b\n"
]
}
],
"source": [
"for m in tools:\n",
" print(tools[m])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AXA4jrEOL9hM"
},
"source": [
"## Functions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WaRms-QfL9hN"
},
"source": [
"Python functions are defined using the `def` keyword. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "kiMDUr58L9hN"
},
"outputs": [],
"source": [
"def sign(x):\n",
" if x > 0:\n",
" return 'positive'\n",
" elif x < 0:\n",
" return 'negative'\n",
" else:\n",
" return 'zero'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "k2uRgLt1t_tu",
"outputId": "86620e7a-8e9e-40ee-d04a-5df715e94879"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"negative\n"
]
}
],
"source": [
"print(sign(-5))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "U-QJFt8TL9hR"
},
"source": [
"We will often define functions to take optional keyword arguments, like this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PfsZ3DazL9hR",
"outputId": "8880063d-5898-4083-bb89-ccd4249ed0db"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Talha!\n",
"HELLO, TALHA!\n"
]
}
],
"source": [
"def hello(name, loud=False):\n",
" if loud:\n",
" print('HELLO, {}'.format(name.upper()))\n",
" else:\n",
" print('Hello, {}!'.format(name))\n",
"\n",
"hello('Talha')\n",
"hello('Talha!', loud=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MLoP2sXQt_tv",
"outputId": "b7338b6d-f9c3-4545-bcd3-6392d4d7eb5b"
},
"outputs": [
{
"data": {
"text/plain": [
"36"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def square(x):\n",
" return x ** 2\n",
"\n",
"def multiply(a, b):\n",
" return a * b\n",
"\n",
"# Functions can be composed.\n",
"square(multiply(3, 2))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wj-ueQGzt_tv",
"outputId": "644cecee-971a-491d-d89c-76e6e55bca5b"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" (25, 625)\n"
]
}
],
"source": [
"def multi_values(x):\n",
" y=x ** 2\n",
" z=y ** 2\n",
"\n",
" return y,z\n",
"\n",
"b=multi_values(5)\n",
"print(' {}'.format(b))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ypNdpEy4t_tx"
},
"source": [
"# Task 1\n",
"## Email Classification\n",
"Develope a tool which will classify an email.\n",
"\n",
"### Tasks:\n",
"* Create a function classify_emails that takes a list of emails as an argument.\n",
"* Inside the function, categorize emails into \"Important,\" \"Promotions,\" and \"Spam\" based on keywords and content analysis.\n",
"* Loop through the emails and identify keywords to determine their category.\n",
"* If an email contains keywords indicating importance, assign it as \"Important.\"\n",
"* If an email contains keywords related to promotions or deals, assign it as \"Promotions.\"\n",
"* If an email is suspected to be spam based on certain keywords or patterns, assign it as \"Spam.\"\n",
"* Return three lists of emails for each category.\n",
"* Print the lists."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ae2EhiU5t_tx"
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tOLKXX56K760"
},
"source": [
"# Task 2\n",
"## Online Bookstore Inventory\n",
"You are developing an inventory management system for an online bookstore. The system should allow the bookstore to keep track of books, their quantities, prices, and generate reports.\n",
"\n",
"## Tasks:\n",
"* Create a list to represent the bookstore's inventory. Each item in the list will be a dictionary containing book details: title, author, quantity, and price.\n",
"* Implement a loop that allows the bookstore to add books to the inventory.\n",
"Ask for the book's title, author, quantity, and price.\n",
"Append a dictionary with the book details to the inventory list.\n",
"* Ask users to input a book title they want to search for.\n",
"Search the inventory list for the book title and display its details if found.\n",
"* Allow the bookstore to update the quantity of a book in the inventory.\n",
"Ask for the book's title and the new quantity.\n",
"Update the quantity in the corresponding dictionary.\n",
"* Implement a function to calculate the total revenue based on the quantities sold and prices of each book.\n",
"Calculate the overall revenue for the entire inventory.\n",
"* Print a comprehensive report showing all books in the inventory along with their details: title, author, quantity, and price.\n",
"* Check the inventory for books with a quantity of zero.\n",
"Print an alert message for any out-of-stock books."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_UjrcDe4LSsd"
},
"outputs": [],
"source": [
"# YOUR CODE HERE\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UstEegygDmF_"
},
"source": [
"#Lambda Function\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Tc4btWjCIj8L"
},
"source": [
"A lambda function is a small anonymous function.\n",
"\n",
"A lambda function can take any number of arguments, but can only have one expression."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uswh8PsxInuC",
"outputId": "0dac0f36-fb14-4a94-ec2c-9e8957727f08"
},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# simple function\n",
"def add(a):\n",
" return a + 10\n",
"\n",
"add(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Le20eRKODk2t",
"outputId": "30f74cdb-90b2-468b-cd9d-8115747f2180"
},
"outputs": [
{
"data": {
"text/plain": [
"30"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = lambda a, b : a * b\n",
"x(5, 6)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "P3dzKd_ELVcJ"
},
"source": [
"# Task 3\n",
"\n",
"You're working on a student management system that needs to determine whether a student passed or failed a course based on their grade.\n",
"\n",
"Write a lambda function that takes a numerical grade as input and returns \"Pass\" if the grade is 50 or above, and \"Fail\" otherwise. Use the lambda function to process a list of grades and create a list of pass/fail statuses.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "spMiXjjOBdU7"
},
"outputs": [],
"source": [
"# Write your code here!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vZ2Q3BAMIzO-"
},
"source": [
"## Python Class\n",
"A class is a blueprint for creating objects. It serves as a template that defines the attributes and behaviors of an object.\n",
"It encapsulates data (attributes) and methods (functions) that operate on that data. Also, promote code reusability and maintainability.\n",
"# Syntax:\n",
"class ClassName:\n",
"\n",
" def functionName:\n",
"# Objects\n",
"Objects are instances of a class, created using the class name followed by parentheses. Objects encapsulate the attributes and methods defined in the class.\n",
"\n",
"# Syntax:\n",
"object_name = ClassName()\n",
"\n",
"my_car = Car(\"Toyota\", \"Camry\", 2022)\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wuTTu2kZI0th"
},
"outputs": [],
"source": [
"class Person:\n",
" def __init__(self, name, age):\n",
" self.name = name\n",
" self.age = age\n",
"\n",
" def print_name(self):\n",
" print(self.name)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0m5M3ICdI140",
"outputId": "f9ae769a-a8ae-4492-b008-f7dbb9d230f3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Talha\n",
"29\n",
"Talha\n"
]
}
],
"source": [
"p1 = Person(\"Talha\", 29)\n",
"\n",
"print(p1.name)\n",
"print(p1.age)\n",
"p1.print_name()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fZUIcLzdEJAv"
},
"source": [
"# Task 4\n",
"Create a class called BankAccount which represents a bank account, having as attributes: accountNumber, name, balance. Create a constructor with parameters: accountNumber, name, balance.\n",
"\n",
"=>Create a Deposit( ) method which manages the deposit actions.\n",
"\n",
"=>Create a withdrawal( ) method which manages the withdrawal actions.\n",
"\n",
"=>Create a bankFees( ) method to apply the bank fees with a percentage of 5% of the balance account.\n",
"\n",
"=>Create a display( ) method to display account details."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aqE84MEKFvPR"
},
"source": [
"# Inheritance:\n",
"Inheritance is a mechanism where a new class (subclass) can inherit properties and behaviors\n",
"from an existing class (superclass). This promotes code reusability and allows for the creation\n",
"of specialized classes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YBXLxTRsF2wV",
"outputId": "7d5717ad-8c40-44e6-961a-bac4c0e21e45"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Animal speaks\n",
"Dog barks\n"
]
}
],
"source": [
"class Animal:\n",
" def speak(self):\n",
" return \"Animal speaks\"\n",
"class Dog(Animal):\n",
" def bark(self):\n",
" return \"Dog barks\"\n",
"dog = Dog()\n",
"print(dog.speak())\n",
"print(dog.bark())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2e9ubMEXGkB7"
},
"source": [
"# Encapsulation:\n",
"Encapsulation refers to the bundling of data (attributes) and methods that operate on the data\n",
"within a single unit (class). It hides the internal state of an object from the outside world and\n",
"allows controlled access to it through methods.\n",
"# Syntax\n",
"class ClassName:\n",
"\n",
" def init (self, parameters):\n",
" self. attribute_name = value # Private attribute\n",
" def get_attribute_name(self):\n",
" return self. attribute_name"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "P7Ud5KMQHK3K",
"outputId": "2e3d562c-8911-4fe9-d889-14737eeed701"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Toyota\n",
"Corolla\n"
]
}
],
"source": [
"# Example:\n",
"class Car:\n",
" def __init__(self, make, model):\n",
" self. make = make\n",
" self. model = model\n",
" def get_make(self):\n",
" return self. make\n",
" def get_model(self):\n",
" return self. model\n",
"car = Car(\"Toyota\", \"Corolla\")\n",
"print(car.get_make())\n",
"print(car.get_model())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "h3xk3kVRHnRX"
},
"source": [
"# Polymorphism:\n",
"Polymorphism allows objects of different classes to be treated as objects of a common super\n",
"class. It enables flexibility by allowing methods to behave differently based on the object they\n",
"operate on.\n",
"\n",
"## Syntax:\n",
"class SuperClassName:\n",
"\n",
" def method_name(self):\n",
" Method definition\n",
" pass\n",
"\n",
" class SubClassName1(SuperClassName):\n",
" def method_name(self):\n",
" Overridden method definition\n",
" pass\n",
" class SubClassName2(SuperClassName):\n",
" def method_name(self):\n",
" Overridden method definition\n",
" Pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-v1uvvo4Ibcu",
"outputId": "04aa90ec-445d-4e17-f514-bc80af801c74"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dog barks\n",
"Cat meows\n"
]
}
],
"source": [
"class Animal:\n",
" def speak(self):\n",
" return \"Animal speaks\"\n",
"class Dog(Animal):\n",
" def speak(self):\n",
" return \"Dog barks\"\n",
"class Cat(Animal):\n",
" def speak(self):\n",
" return \"Cat meows\"\n",
"\n",
"def make_sound(animal):\n",
" print(animal.speak())\n",
"dog = Dog()\n",
"cat = Cat()\n",
"\n",
"make_sound(dog) # Output: Dog barks\n",
"make_sound(cat) # Output: Cat meows"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VKy8ShYa-PVm"
},
"source": [
"#Task 5\n",
"Create a base class Shape with an abstract method area(). Then, create two subclasses Rectangle and Circle that inherit from Shape. Implement the area() in both subclasses to calculate the area of a rectangle and a circle, respectively. Finally, create a function calculate_total_area() that takes a list of shapes and calculates the total area of all shapes.\n",
"allshapes."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aIrawkI4J4n4"
},
"source": [
"# Happy Coding :)\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"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.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}