summaryrefslogtreecommitdiff
path: root/sample_notebooks/VikasPrasad/VikasPrasad_version_backup/Chapter_01_1.ipynb
blob: b5226e07afa8ce6dcba988b299bac3142c1e89e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
 "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": [
      "Example 1: Page 20"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Thermostat:\n",
      "\t\"\"\"A simple thermostat class\"\"\"\n",
      "    \n",
      "\t#since private instance variables don't exist in Python,\n",
      "    #hence using a convention: name prefixed with an underscore, 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 body goes here\n",
      "\t\tpass\n",
      "    \n",
      "#end class Thermostat"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example 2: Page 22"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#demonstrates basic OOP syntax\n",
      "\n",
      "class BankAccount:\n",
      "\t\"\"\"A simple bank account class\"\"\"\n",
      "\t\n",
      "\tdef __init__(self, openingBalance):\t#special method to create objects\n",
      "        #with instances customized to a specific initial state\n",
      "\t\t\n",
      "        #since private instance variables don't exist in Python,\n",
      "        #hence using a convention: name prefixed with an underscore, to treat them as non-public part\n",
      "\t    self._balance = openingBalance\t#account balance\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",
      "        \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": [
      "Example 3: Page 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": {}
  }
 ]
}