-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3f8a7a
commit 1fb448a
Showing
3 changed files
with
500 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
{ | ||
"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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Components:\n", | ||
"1. Class\n", | ||
"2. Object\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Class:\n", | ||
"\n", | ||
"It is a structure or a building block (a code block) representing or defining the attributes (features and behaviour) of similar parameters.\n", | ||
"\n", | ||
"### Definition:\n", | ||
"Using `class` keyword.\n", | ||
"Name of a class should start with an uppercase letter.\n", | ||
"\n", | ||
"Syntax: `class Class_name`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class Student:\n", | ||
" roll_no = int()\n", | ||
" name = str()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Object:\n", | ||
"\n", | ||
"It is an instance of a class.\n", | ||
"\n", | ||
"Syntax: `obj_name = Class_name()`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"student1 = Student() # creating a new object\n", | ||
"\n", | ||
"student1.roll_no = 1\n", | ||
"student1.name = 'Prabhu'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Prabhu\n", | ||
"1\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(student1.name)\n", | ||
"print(student1.roll_no)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## *self* Keyword:\n", | ||
"\n", | ||
"Calls the attributes for current instance or object.\n", | ||
"\n", | ||
"Syntax: `self.attribute_name`" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Defining attributes in a class:\n", | ||
"\n", | ||
"Attributes or Properties are defined using a constructor.\n", | ||
"\n", | ||
"### Constructor:\n", | ||
"Executes a set of code whenever a new object/instance is created.\n", | ||
"\n", | ||
"Defined by \\_\\_init\\_\\_ method." | ||
] | ||
}, | ||
{ | ||
"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", | ||
" print('Studying....')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"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}')" | ||
] | ||
}, | ||
{ | ||
"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", | ||
" print('Studying....')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Roll No: 2, Name: Rahul\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"st2 = Student(2, 'Rahul')\n", | ||
"print(f'Roll No: {st2.roll_no}, Name: {st2.name}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## destructor:\n", | ||
"Delete the current instance of class or object.\n", | ||
"\n", | ||
"Use \\_\\_del\\_\\_ method" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.