|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 4, |
| 6 | + "id": "63f34d97", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# Example :- \n", |
| 11 | + "class BankAccount:\n", |
| 12 | + " def __init__(self,balance):\n", |
| 13 | + " self.__balance = balance # private data (double underscore)\n", |
| 14 | + " def deposit(self,amount):\n", |
| 15 | + " if amount > 0:\n", |
| 16 | + " self.__balance += amount\n", |
| 17 | + " print(f'Deposit Amount : {amount}/-') # method public\n", |
| 18 | + " def withdraw(self,amount): # method public\n", |
| 19 | + " if amount > 0 and amount <= self.__balance:\n", |
| 20 | + " self.__balance -= amount\n", |
| 21 | + " print(f'withdraw {amount}/-')\n", |
| 22 | + " def get_balance(self): # method public\n", |
| 23 | + " return self.__balance \n", |
| 24 | + " " |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "id": "fde6173d", |
| 31 | + "metadata": {}, |
| 32 | + "outputs": [ |
| 33 | + { |
| 34 | + "name": "stdout", |
| 35 | + "output_type": "stream", |
| 36 | + "text": [ |
| 37 | + "Deposit Amount : 900/-\n", |
| 38 | + "10900\n", |
| 39 | + "withdraw 7800/-\n", |
| 40 | + "3100\n", |
| 41 | + "3100\n" |
| 42 | + ] |
| 43 | + } |
| 44 | + ], |
| 45 | + "source": [ |
| 46 | + "# create a account\n", |
| 47 | + "account = BankAccount(10000)\n", |
| 48 | + "account.deposit(900)\n", |
| 49 | + "print(account.get_balance())\n", |
| 50 | + "account.withdraw(670)\n", |
| 51 | + "print(account.__balance) # can't access private data directly" |
| 52 | + ] |
| 53 | + } |
| 54 | + ], |
| 55 | + "metadata": { |
| 56 | + "kernelspec": { |
| 57 | + "display_name": "Python 3", |
| 58 | + "language": "python", |
| 59 | + "name": "python3" |
| 60 | + }, |
| 61 | + "language_info": { |
| 62 | + "codemirror_mode": { |
| 63 | + "name": "ipython", |
| 64 | + "version": 3 |
| 65 | + }, |
| 66 | + "file_extension": ".py", |
| 67 | + "mimetype": "text/x-python", |
| 68 | + "name": "python", |
| 69 | + "nbconvert_exporter": "python", |
| 70 | + "pygments_lexer": "ipython3", |
| 71 | + "version": "3.13.7" |
| 72 | + } |
| 73 | + }, |
| 74 | + "nbformat": 4, |
| 75 | + "nbformat_minor": 5 |
| 76 | +} |
0 commit comments