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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 01: Page 156"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"enter the number of terms in the sequence5\n",
"a_n=1/n\n",
"when n= 5 a_n is\n",
"1/ 1 , 1/ 2 , 1/ 3 , 1/ 4 , 1/ 5 , \n",
"\n",
"1.0 , 0.5 , 0.333333333333 , 0.25 , 0.2 ,\n"
]
}
],
"source": [
"#To generate a sequence a_n=1/n\n",
"i=1.0 #floating point division\n",
"n=input(\"enter the number of terms in the sequence\");\n",
"print \"a_n=1/n\"\n",
"print \"when n=\",n,\"a_n is\"\n",
"for i in range(1,n+1): #iteration till the number of terms specified by the user\n",
" a=1.0/i\n",
" print \"1/\",i,\",\",\n",
"print \"\\n\"\n",
"for i in range(1,n+1): #iteration till the number of terms specified by the user\n",
" a=1.0/i\n",
" print a,\",\",\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 02: Page 157"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number of terms in the sequence to generate the geometric progression5\n",
"the list of terms b 0 , b 1 , b 2 , b 3 , b 4 , b 5 , begins with 1 -1 1 -1 1 -1 \n",
"the list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 2 10 50 250 1250 6250 \n",
"the list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 6.0 2.0 0.666666666667 0.222222222222 0.0740740740741 0.0246913580247\n"
]
}
],
"source": [
"n=input(\"Enter the number of terms in the sequence to generate the geometric progression\");\n",
"i=1\n",
"print\"the list of terms\",\n",
"for i in range (n+1):print\"b\",i,\",\",\n",
"print \"begins with\", \n",
"for i in range (n+1): #iterate for the number of terms given as input\n",
" b_n=(-1)**i\n",
" print b_n,\n",
"print\"\\n\",\"the list of terms\",\n",
"for i in range (n+1):print\"c\",i,\",\",\n",
"print \"begins with\", \n",
"for i in range (n+1): #iterate for the number of terms given as input\n",
" c_n=2*(5**i)\n",
" print c_n,\n",
"print\"\\n\",\"the list of terms\",\n",
"for i in range (n+1):print\"c\",i,\",\",\n",
"print \"begins with\",\n",
"for i in range (n+1): #iterate for the number of terms given as input\n",
" d_n=6.0*((1.0/3.0)**i)\n",
" print d_n, #prints the fraction values in decimals. Floating point division\n",
"\n",
" \n",
" \n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 03: Page 157"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number terms in the sequence5\n",
"The list of terms s 0 , s 1 , s 2 , s 3 , s 4 , begins with -1 3 7 11 15 \n",
"The list of terms t 0 , t 1 , t 2 , t 3 , t 4 , begins with 7 4 1 -2 -5\n"
]
}
],
"source": [
"n=input(\"Enter the number terms in the sequence\");\n",
"s_n=-1+4*n\n",
"t_n=7-3*n\n",
"i=0\n",
"print \"The list of terms\",\n",
"for i in range(n):\n",
" print \"s\",i,\",\",\n",
"print \"begins with\",\n",
"for i in range(n):\n",
" print -1+4*i,\n",
"print \"\\nThe list of terms\",\n",
"for i in range(n):\n",
" print \"t\",i,\",\",\n",
"print \"begins with\",\n",
"for i in range(n):\n",
" print 7-3*i,\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 05: Page 158"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[ 1 ] 5\n",
"a[ 2 ] 8\n",
"a[ 3 ] 11\n"
]
}
],
"source": [
"a=[2,0,0,0] #assigning a[0]=2 (Given)\n",
"\n",
"for i in range(1,4):#iteration to run till a[3]\n",
" a[i]=a[i-1]+3\n",
" print \"a[\",i,\"]\",a[i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 06: Page 158"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a[ 2 ] 2\n",
"a[ 3 ] -3\n"
]
}
],
"source": [
"a=[3,5,0,0] #assingning a[0],a[1] to the given values\n",
"\n",
"for i in range(2,4): # iterations to find the successive values. If values are to be found for further terms the for loop \"stop\" has to be modified\n",
" a[i]=a[i-1]-a[i-2]\n",
" print \"a[\",i,\"]\",a[i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 07: Page 158 "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fibonacci series is\n",
"f[ 2 ]=f[ 1 ]+f[ 0 ]= 1\n",
"f[ 3 ]=f[ 2 ]+f[ 1 ]= 2\n",
"f[ 4 ]=f[ 3 ]+f[ 2 ]= 3\n",
"f[ 5 ]=f[ 4 ]+f[ 3 ]= 5\n",
"f[ 6 ]=f[ 5 ]+f[ 4 ]= 8\n"
]
}
],
"source": [
"f=[0,1,0,0,0,0,0] #assingning a[0],a[1] to the given values\n",
"print \"Fibonacci series is\"\n",
"for i in range(2,7): # iterations to find the successive values. If values are to be found for further terms the for loop \"stop\" has to be modified\n",
" f[i]=f[i-1]+f[i-2]\n",
" print \"f[\",i,\"]=f[\",i-1,\"]+f[\",i-2,\"]=\",f[i]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 08: Page 159 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the number5\n",
"The factorial of 5 is 120\n"
]
}
],
"source": [
"n=1\n",
"result=0\n",
"number=input(\"Enter the number\");\n",
"for i in range(1,number):\n",
" n=n+i*n \n",
"print \"The factorial of\",number,\"is\",n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 18: Page 164"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the upper limit for the operation j^25\n",
"Enter the lower limit for the operation j^21\n",
"The square of terms form 1 to n 1 ^2+ 2 ^2+ 3 ^2+ 4 ^2+ 5 ^2+ = 55\n"
]
}
],
"source": [
"#finding the summation of j^2\n",
"up=input(\"Enter the upper limit for the operation j^2\");\n",
"low=input(\"Enter the lower limit for the operation j^2\");\n",
"sum=0\n",
"print \"The square of terms form 1 to n\",\n",
"for j in range (low,up+1): #summation. Iteration from lower to upper limit.\n",
" print j,\"^2+\",\n",
" j=j**2 #square function is computed as '**'\n",
" sum=sum+j\n",
"print \"=\",sum\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 19: Page 164"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The value for the sequence (-1)^ 4 + (-1)^ 5 + (-1)^ 6 + (-1)^ 7 + (-1)^ 8 + = 1\n"
]
}
],
"source": [
"k=4 #lower limit\n",
"sum=0\n",
"print \"The value for the sequence\",\n",
"for k in range (4,8+1,1): #8+1 , 8 is the upper limit, in python to make for loop run till the limit equal to upper limit we give a +1.\n",
" print \"(-1)^\",k,\"+\",\n",
" sum=sum+((-1)**k)\n",
"print \"=\",sum\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 21: Page 165"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the upper limit for the inner summation3\n",
"Enter the lower limit for the inner summation1\n",
"Enter the upper limit for the outer summation4\n",
"Enter the lower limit for the outer summation1\n",
"60\n"
]
}
],
"source": [
"\n",
"globals()['j']=0\n",
"i=0\n",
"globals()['s']=0\n",
"upj=input(\"Enter the upper limit for the inner summation\");\n",
"lowj=input(\"Enter the lower limit for the inner summation\");\n",
"upi=input(\"Enter the upper limit for the outer summation\");\n",
"lowi=input(\"Enter the lower limit for the outer summation\");\n",
"for i in range (lowj,upj+1):\n",
" j=j+i\n",
"for l in range(lowi,upi+1):\n",
" s=s+(j*l)\n",
"print s\n",
" \n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Example 13: Page 161"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10\n"
]
}
],
"source": [
"#To print series 1 once, 2 twice, 3 thrice and so on\n",
"a=[]\n",
"i=1\n",
"for i in range(1,10+1): #for loop to initialise the number\n",
" for j in range(1,i+1):#for loop to iterate to make the count\n",
" print i,\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##Example 22: Page 166"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum of values of s for all the members of the set { 0 2 4 } is 6\n"
]
}
],
"source": [
"s=0 #initialise it to zero to store the results\n",
"globals()['res']=0 #difining result as global variable since it has to be accessed outside the loop\n",
"print \"Sum of values of s for all the members of the set {\",\n",
"for s in range (0,4+1,2): #iterate for terms 0,4,6\n",
" print s,\n",
" res=res+s\n",
"print \"} is\",res\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 02: Page 178"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the elements of matrix 1\n",
"number of rows, m = 3\n",
"number of columns, n = 3\n",
"('entry in row: ', 1, ' column: ', 1)\n",
"1\n",
"('entry in row: ', 1, ' column: ', 2)\n",
"0\n",
"('entry in row: ', 1, ' column: ', 3)\n",
"-1\n",
"('entry in row: ', 2, ' column: ', 1)\n",
"2\n",
"('entry in row: ', 2, ' column: ', 2)\n",
"2\n",
"('entry in row: ', 2, ' column: ', 3)\n",
"-3\n",
"('entry in row: ', 3, ' column: ', 1)\n",
"3\n",
"('entry in row: ', 3, ' column: ', 2)\n",
"4\n",
"('entry in row: ', 3, ' column: ', 3)\n",
"0\n",
"Enter the elements of matrix 2\n",
"number of rows, m = 3\n",
"number of columns, n = 3\n",
"('entry in row: ', 1, ' column: ', 1)\n",
"3\n",
"('entry in row: ', 1, ' column: ', 2)\n",
"4\n",
"('entry in row: ', 1, ' column: ', 3)\n",
"-1\n",
"('entry in row: ', 2, ' column: ', 1)\n",
"1\n",
"('entry in row: ', 2, ' column: ', 2)\n",
"-3\n",
"('entry in row: ', 2, ' column: ', 3)\n",
"0\n",
"('entry in row: ', 3, ' column: ', 1)\n",
"-1\n",
"('entry in row: ', 3, ' column: ', 2)\n",
"1\n",
"('entry in row: ', 3, ' column: ', 3)\n",
"2\n",
"Addition of \n",
" matrix 1 [[1, 0, -1], [2, 2, -3], [3, 4, 0]] and \n",
" matrix 2 [[3, 4, -1], [1, -3, 0], [-1, 1, 2]] is \n",
"[[4, 4, -2], [3, -1, -3], [2, 5, 2]]\n"
]
}
],
"source": [
"def getmat(): #function to get the matrix elements\n",
" m = int(input('number of rows, m = '))\n",
" n = int(input('number of columns, n = '))\n",
" matrix = []; columns = []\n",
"\n",
" for i in range(0,m):\n",
" matrix.append([])\n",
" for j in range(0,n):\n",
" matrix[i].append(0)\n",
" print ('entry in row: ',i+1,' column: ',j+1)\n",
" matrix[i][j] = int(input())\n",
" return (matrix)\n",
"\n",
"def matrixADD(m1,m2): #function to add the matrix.\n",
" z=[]\n",
" for i in range (len(m1)):\n",
" tem = []\n",
" for j in range (len(m2)):\n",
" x=m1[i][j]+m2[i][j]\n",
" tem.append(x)\n",
" z.append(tem)\n",
" return z \n",
" \n",
"\n",
"\n",
"mat1=[]\n",
"mat2=[]\n",
"Z=[]\n",
"print \"Enter the elements of matrix 1\"\n",
"mat1=getmat() #function call\n",
"print \"Enter the elements of matrix 2\"\n",
"mat2=getmat() #function call\n",
"print \"Addition of \\n matrix 1\",mat1,\"and \\n matrix 2\",mat2,\"is \\n\",matrixADD(mat1,mat2) #function call to add \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 03: Page 179"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The multiplication of the two matrices AB is\n",
"[14, 4]\n",
"[8, 9]\n",
"[7, 13]\n",
"[8, 2]\n"
]
}
],
"source": [
"\n",
"# Program to multiply two matrices using nested loops\n",
"\n",
"# 3x3 matrix\n",
"X = [[1,0,4],\n",
" [2,1,1],\n",
" [3,1,0],\n",
" [0,2,2]]\n",
"# 3x4 matrix\n",
"Y = [[2,4],\n",
" [1,1],\n",
" [3,0]]\n",
"# result is 3x4\n",
"result = [[0,0],\n",
" [0,0],\n",
" [0,0,],\n",
" [0,0]]\n",
"\n",
"# iterate through rows of X\n",
"for i in range(len(X)):\n",
" # iterate through columns of Y\n",
" for j in range(len(Y[0])):\n",
" # iterate through rows of Y\n",
" for k in range(len(Y)):\n",
" result[i][j] += X[i][k] * Y[k][j]\n",
"print \"The multiplication of the two matrices AB is\"\n",
"\n",
"for r in result:\n",
" print(r)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 05: Page 181"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the elements of the matrix\n",
"number of rows = 2\n",
"number of columns = 3\n",
"('entry in row: ', 1, ' column: ', 1)\n",
"1\n",
"('entry in row: ', 1, ' column: ', 2)\n",
"2\n",
"('entry in row: ', 1, ' column: ', 3)\n",
"3\n",
"('entry in row: ', 2, ' column: ', 1)\n",
"4\n",
"('entry in row: ', 2, ' column: ', 2)\n",
"5\n",
"('entry in row: ', 2, ' column: ', 3)\n",
"6\n",
"The transpose is\n",
"[1, 4]\n",
"[2, 5]\n",
"[3, 6]\n",
"[8, 2]\n"
]
}
],
"source": [
"# Program to transpose a matrix using nested loop\n",
"# iterate through rows\n",
"def mattrans(X,result):\n",
" print \"The transpose is\"\n",
" for i in range(len(X)):\n",
" # iterate through columns\n",
" for j in range(len(X[0])):\n",
" result[j][i] = X[i][j]\n",
" for r in result:\n",
" print(r)\n",
" \n",
"def getmat():\n",
" row = int(input('number of rows = '))\n",
" col = int(input('number of columns = '))\n",
" matrix = []; columns = []\n",
"\n",
" for i in range(0,row):\n",
" matrix.append([])\n",
" for j in range(0,col):\n",
" matrix[i].append(0)\n",
" print ('entry in row: ',i+1,' column: ',j+1)\n",
" matrix[i][j] = int(input())\n",
" for c in range(col):\n",
" for r in range(row):\n",
" result[c][r]=0\n",
" mattrans(matrix,result)\n",
" \n",
"\n",
"\n",
"print \"Enter the elements of the matrix\"\n",
"getmat()\n",
"#mattrans(mat1)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|