diff options
author | hardythe1 | 2015-04-07 15:58:05 +0530 |
---|---|---|
committer | hardythe1 | 2015-04-07 15:58:05 +0530 |
commit | c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131 (patch) | |
tree | 725a7d43dc1687edf95bc36d39bebc3000f1de8f /sample_notebooks/VikasPrasad/chapter1_1.ipynb | |
parent | 62aa228e2519ac7b7f1aef53001f2f2e988a6eb1 (diff) | |
download | Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.gz Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.tar.bz2 Python-Textbook-Companions-c7fe425ef3c5e8804f2f5de3d8fffedf5e2f1131.zip |
added books
Diffstat (limited to 'sample_notebooks/VikasPrasad/chapter1_1.ipynb')
-rwxr-xr-x | sample_notebooks/VikasPrasad/chapter1_1.ipynb | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/sample_notebooks/VikasPrasad/chapter1_1.ipynb b/sample_notebooks/VikasPrasad/chapter1_1.ipynb new file mode 100755 index 00000000..9e1549a9 --- /dev/null +++ b/sample_notebooks/VikasPrasad/chapter1_1.ipynb @@ -0,0 +1,166 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "1: Overview of Data Structures and Algorithms" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "1, 20" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class Thermostat:\n", + "\t\"\"\"A simple thermostat class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + " #to treat them as non-public part\n", + "\t_currentTemp = 0.0\n", + "\t_desiredTemp = 0.0\n", + "\n", + "\tdef furnace_on(self):\n", + "\t\t#method body goes here\n", + "\t\tpass\n", + "\n", + "\tdef furnace_off(self):\n", + "\t\t#method bisy goes here\n", + "\t\tpass\n", + "#end class Thermostat" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 1 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "2, 22" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#demonstrates basic OOP syntax\n", + "\n", + "class BankAccount:\n", + "\t\"\"\"A simple bank account class\"\"\"\n", + "\t#since private instance variables don't exist in Python, hence using a convention: name prefixed with an underscore,\n", + " #to treat them as non-public part\n", + "\t_balance = 0.0\t#account balance\n", + "\n", + "\tdef __init__(self, openingBalance):\t#special method to cerate objects with instances customized to a specific initial state\n", + "\t\tself._balance = openingBalance\n", + "\n", + "\tdef deposit(self, amount):\t#makes deposit\n", + "\t\tself._balance = self._balance + amount\n", + "\n", + "\tdef withdraw(self, amount):\t#makes withdrawl\n", + "\t\tself._balance = self._balance - amount\n", + "\n", + "\tdef display(self):\t#displays balance\n", + "\t\tprint 'Balance=', self._balance\n", + "#end class BankAccount\n", + "\n", + "ba1 = BankAccount(100.00)\t#create account\n", + "\n", + "print 'Before transactions, ',\n", + "ba1.display()\t#display balance\n", + "\n", + "ba1.deposit(74.35)\t#make deposit\n", + "ba1.withdraw(20.00)\t#make withdrawl\n", + "\n", + "print 'After transactions, ',\n", + "ba1.display()\t#display balance\n", + "#end" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Before transactions, Balance= 100.0\n", + "After transactions, Balance= 154.35\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "3, 25" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "#creating string objects\n", + "str1 = ''\n", + "str2 = 'George'\n", + "\n", + "#verifying results\n", + "print str1\n", + "print str2\n", + "\n", + "#naming string object\n", + "str3 = 'amanuensis'\n", + "\n", + "#verifying results\n", + "print str3\n", + "\n", + "#accessing specific characters from string using [] operator\n", + "ch1 = str2[3]\n", + "\n", + "#verifying results\n", + "print ch1\n", + "\n", + "#finding and printing number of characters in a string\n", + "print len(str1)" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "George\n", + "amanuensis\n", + "r\n", + "0\n" + ] + } + ], + "prompt_number": 3 + } + ], + "metadata": {} + } + ] +}
\ No newline at end of file |