Skip to content

Commit

Permalink
Updates for OOPS
Browse files Browse the repository at this point in the history
  • Loading branch information
Aniruddh-0701 committed Jul 22, 2021
1 parent 2d3cc65 commit 6b1438e
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 142 deletions.
156 changes: 78 additions & 78 deletions OOPS.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# OOPS:\n",
"\n",
"Object Oriented Programming or OOP is working with classes and objects. It involves interaction between objects having various attributes.\n",
"\n",
"Python is a object oriented programming language. This meand that all the types are representation or instance of a type class."
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Components:\n",
"1. Class\n",
"2. Object\n"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Class:\n",
"\n",
Expand All @@ -33,75 +32,75 @@
"Name of a class should start with an uppercase letter.\n",
"\n",
"Syntax: `class Class_name`"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"class Student:\n",
" roll_no = int()\n",
"class Students:\r\n",
" roll_no = int()\r\n",
" name = str()"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Object:\n",
"\n",
"It is an instance of a class.\n",
"\n",
"Syntax: `obj_name = Class_name()`"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"student1 = Student() # creating a new object\n",
"\n",
"student1.roll_no = 1\n",
"student1 = Students() # creating a new object\r\n",
"\r\n",
"student1.roll_no = 1\r\n",
"student1.name = 'Prabhu'"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"source": [
"print(student1.name)\r\n",
"print(student1.roll_no)"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"Prabhu\n",
"1\n"
]
}
],
"source": [
"print(student1.name)\n",
"print(student1.roll_no)"
]
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## *self* Keyword:\n",
"\n",
"Calls the attributes for current instance or object.\n",
"\n",
"Syntax: `self.attribute_name`"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Defining attributes in a class:\n",
"\n",
Expand All @@ -111,92 +110,92 @@
"Executes a set of code whenever a new object/instance is created.\n",
"\n",
"Defined by \\_\\_init\\_\\_ method."
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class Student:\n",
" def __init__(self): # default constructor\n",
" self.roll_no = 0\n",
" self.name = 'Name'\n",
"# print('__init__ file')\n",
" \n",
" def study():\n",
"class Student:\r\n",
" def __init__(self): # default constructor\r\n",
" self.roll_no = 0\r\n",
" self.name = 'Name'\r\n",
"# print('__init__ file')\r\n",
" \r\n",
" def study(self):\r\n",
" print('Studying....')"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"source": [
"st1 = Student()\r\n",
"st1.roll_no = 1\r\n",
"st1.name = 'Ravi'\r\n",
"print(f'Roll No: {st1.roll_no}, Name: {st1.name}')"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"__init__ file\n",
"Roll No: 1, Name: Ravi\n"
]
}
],
"source": [
"st1 = Student()\n",
"st1.roll_no = 1\n",
"st1.name = 'Ravi'\n",
"print(f'Roll No: {st1.roll_no}, Name: {st1.name}')"
]
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"class Student:\n",
" def __init__(self, rn = 0, st_name = 'Name'): # Parametric Constructor\n",
" self.roll_no = rn\n",
" self.name = st_name\n",
"# print('__init__ file')\n",
" \n",
" def study():\n",
"class Student_details:\r\n",
" def __init__(self, rn = 0, st_name = 'Name'): # Parametric Constructor\r\n",
" self.roll_no = rn\r\n",
" self.name = st_name\r\n",
"# print('__init__ file')\r\n",
" \r\n",
" def study(self):\r\n",
" print('Studying....')"
]
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"source": [
"st2 = Student_details(2, 'Rahul')\r\n",
"print(f'Roll No: {st2.roll_no}, Name: {st2.name}')"
],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"Roll No: 2, Name: Rahul\n"
]
}
],
"source": [
"st2 = Student(2, 'Rahul')\n",
"print(f'Roll No: {st2.roll_no}, Name: {st2.name}')"
]
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## destructor:\n",
"Delete the current instance of class or object.\n",
"\n",
"Use \\_\\_del\\_\\_ method"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Some other methods:\n",
"\n",
Expand All @@ -211,11 +210,11 @@
"0. \\_\\_eq\\_\\_ - equal\n",
"5. \\_\\_getitem\\_\\_ - get a key from a iterable\n",
"6. \\_\\_setitem\\_\\_ - set a value to the given key of iterable"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Private variables and methods:\n",
"\n",
Expand All @@ -227,37 +226,38 @@
"But if needed, use the following syntax:\n",
"\n",
"`obj._classname__attribute`"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Calling a method in another method inside the class:\n",
"\n",
"Use self keyword to call the function."
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Definining run time class attributes:"
]
],
"metadata": {}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Methods of attributes:"
]
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [],
"outputs": [],
"source": []
"metadata": {}
}
],
"metadata": {
Expand All @@ -281,4 +281,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
Loading

0 comments on commit 6b1438e

Please sign in to comment.