summaryrefslogtreecommitdiff
path: root/Programming_in_C/.ipynb_checkpoints/Chapter_16-checkpoint.ipynb
blob: 535875b5d5c4df51830ee030df68c483b7d3e9f4 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 16: Input and Output Operations in Python"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 16.1, Page number: 350"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "def main():\n",
      "\n",
      "        #Variable Declaration\n",
      "        c='X'\n",
      "        s=\"abcdefghijklmnopqrstuvwxyz\"\n",
      "        i=425\n",
      "        j=17\n",
      "        u=0xf179\n",
      "        l=75000\n",
      "        L=0x1234567812345678\n",
      "        f=12.978\n",
      "        d=-97.4583\n",
      "        cp=c\n",
      "        ip=i\n",
      "        \n",
      "\n",
      "        #Print Integers\n",
      "        print(\"Integers:\\n\")\n",
      "        print(\"%i %o %x %u\" %(i,i,i,i))\n",
      "        print(\"%x %X %#x %#X\" %(i,i,i,i))\n",
      "        print(\"%+i % i %07i %.7i\" %(i,i,i, i))\n",
      "        print(\"%i %o %x %u\" %(j,j,j,j))\n",
      "        print(\"%i %o %x %u\" %(u,u,u,u))\n",
      "        print(\"%ld %lo %lx %lu\" %(l,l,l,l))\n",
      "        print(\"%li %lo %lx %lu\" %(L,L,L, L))\n",
      "\n",
      "        #Print Floats & Doubles\n",
      "        print(\"\\nFloats and Doubles:\")\n",
      "        print(\"%f %e %g\"%( f, f, f))\n",
      "        print(\"%.2f %.2e\"%( f, f))\n",
      "        print(\"%.0f %.0e\"%( f, f))\n",
      "        print(\"%7.2f %7.2e\"%( f, f))\n",
      "        print(\"%f %e %g\"%( d, d, d))\n",
      "        print(\"%.*f\"%( 3, d))\n",
      "        print(\"%*.*f\" %(8, 2, d))\n",
      "\n",
      "        #Print Characters\n",
      "        print(\"\\nCharacters:\")\n",
      "        print(\"%c\"%(c))\n",
      "        print(\"%3c%3c\"%( c, c))\n",
      "        print(\"{0:x} \".format(ord(c))) \n",
      "\n",
      "        #Print Strings\n",
      "        print(\"\\nStrings:\")\n",
      "        print(\"%s\"%(s))\n",
      "        print(\"%.5s\"%(s))\n",
      "        print(\"%30s\"%(s))\n",
      "        print(\"%20.5s\"%(s))\n",
      "        print(\"%-20.5s\"%(s))\n",
      "\n",
      "        #Print variables pointers\n",
      "        print(\"\\nPointers:\")\n",
      "        print(\"{0:x} {1:x}\\n\".format(int(ip),ord(cp)))\n",
      "\n",
      "        print(\"This%n is fun.\")\n",
      "        print(\"c1 = %i, c2 = %i\\n\"%(4, 12))\n",
      "\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Integers:\n",
        "\n",
        "425 651 1a9 425\n",
        "1a9 1A9 0x1a9 0X1A9\n",
        "+425  425 0000425 0000425\n",
        "17 21 11 17\n",
        "61817 170571 f179 61817\n",
        "75000 222370 124f8 75000\n",
        "1311768465173141112 110642547402215053170 1234567812345678 1311768465173141112\n",
        "\n",
        "Floats and Doubles:\n",
        "12.978000 1.297800e+01 12.978\n",
        "12.98 1.30e+01\n",
        "13 1e+01\n",
        "  12.98 1.30e+01\n",
        "-97.458300 -9.745830e+01 -97.4583\n",
        "-97.458\n",
        "  -97.46\n",
        "\n",
        "Characters:\n",
        "X\n",
        "  X  X\n",
        "58 \n",
        "\n",
        "Strings:\n",
        "abcdefghijklmnopqrstuvwxyz\n",
        "abcde\n",
        "    abcdefghijklmnopqrstuvwxyz\n",
        "               abcde\n",
        "abcde               \n",
        "\n",
        "Pointers:\n",
        "1a9 58\n",
        "\n",
        "This%n is fun.\n",
        "c1 = 4, c2 = 12\n",
        "\n"
       ]
      }
     ],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 16.2, Page number: 362"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "import sys\n",
      "\n",
      "def main():\n",
      "        \n",
      "        while (True):\n",
      "                #c=raw_input()              #User Input\n",
      "                c=\"this is a sample text\"   #Dummy text\n",
      "                print(c);         #Display Result\n",
      "                \n",
      "                #Un-comment this while executing from terminal/command line\n",
      "                #if(c==\"EOF\"):     #Check Exit condition\n",
      "                sys.exit()\n",
      "        \n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SystemExit",
       "evalue": "",
       "output_type": "pyerr",
       "traceback": [
        "An exception has occurred, use %tb to see the full traceback.\n",
        "\u001b[1;31mSystemExit\u001b[0m\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "this is a sample text\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": [
        "To exit: use 'exit', 'quit', or Ctrl-D."
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Program 16.3, Page number: 366"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "def main():\n",
      "\n",
      "        #Variable Declaration/ User Input\n",
      "        print(\"Enter name of the file to be copied: \")\n",
      "        inName=\"source.txt\"\n",
      "        #inName=raw_input()\n",
      "\n",
      "        print(\"Enter name of the output file: \")\n",
      "        outName=\"target.txt\"\n",
      "        #outName=raw_input()\n",
      "\n",
      "        \n",
      "        try:\n",
      "            inn=open(inName,\"r\")         \n",
      "        except:# Exception:\n",
      "                print(\"cant open {0} for reading\".format(inName))\n",
      "                sys.exit()\n",
      "\n",
      "        try:\n",
      "               out=open(outName,\"w\") \n",
      "        except:# Exception:\n",
      "                print(\"cant open {0} for writing\".format(outName))\n",
      "                sys.exit()\n",
      "\n",
      "        string=inn.read()               #Read content from File-1\n",
      "        out.write(string)               #Write content to File-2\n",
      "\n",
      "        inn.close()\n",
      "        out.close()\n",
      "\n",
      "        print(\"File has been copied.\\n\");\n",
      "\n",
      "if __name__=='__main__':\n",
      "        main()\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "SystemExit",
       "evalue": "",
       "output_type": "pyerr",
       "traceback": [
        "An exception has occurred, use %tb to see the full traceback.\n",
        "\u001b[0;31mSystemExit\u001b[0m\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Enter name of the file to be copied: \n",
        "Enter name of the output file: \n",
        "cant open source.txt for reading\n"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stderr",
       "text": [
        "To exit: use 'exit', 'quit', or Ctrl-D.\n"
       ]
      }
     ],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}