-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunks_pyt123.json
More file actions
2348 lines (2348 loc) · 221 KB
/
chunks_pyt123.json
File metadata and controls
2348 lines (2348 loc) · 221 KB
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n \n \nPYTHON PROGRAMMING \n[R17A0554] \nLECTURE NOTES \nB.TECH III YEAR – II SEM (R17) \n(2019 -20) \n \n \n \n \nDEPARTMENT OF \nCOMPUTER SCIENCE AND ENGINEERING \n \nMALLA REDDY COLLEGE OF ENGINEERING & \nTECHNOLOGY \n(Autonomous Institution – UGC, Govt. of India) \nRecognized under 2(f) and 12 (B) of UGC ACT 1956 \n(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 1
},
{
"content": " 1956 \n(Affiliated to JNTUH, Hyderabad, Approved by AICTE - Accredited by NBA & NAAC – ‘A’ Grade - ISO 9001:2015 \nCertified) \nMaisammaguda, Dhulapally (Post Via. Hakimpet), Secunderabad – 500100, Telangana State, India \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 1
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n \n SYLLABUS \nMALLA REDDY COLLEGE OF ENGINEERING AND TECHNOLOGY \n \nIII Year B. Tech CSE -II SEM L T/P/D C \n 3 - / - / - 3 \nOPEN ELECTIVE III \n(R17A0554) PYTHON PROGRAMMING \nOBJECTIVES: \n To read and write simple Python programs. \n To develop Python programs with conditionals and loops. \n To define Python functions and call them. \n To use Python data structures –- li",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 2
},
{
"content": "als and loops. \n To define Python functions and call them. \n To use Python data structures –- lists, tuples, dictionaries. \n To do input/output with files in Python. \nUNIT I \nINTRODUC TION DATA, EXPRESSIONS, STATEMENTS \nIntroduction to Python and installation , data types: Int, float, Boolean, string, and list; variables, \nexpressions, statements, precedence of operators, comments; modules, functions --- function and its \nuse, flow of execut ion, parameters and arguments. \n \nUNIT II \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 2
},
{
"content": "s, functions --- function and its \nuse, flow of execut ion, parameters and arguments. \n \nUNIT II \nCONTROL FLOW, LOOPS \nConditionals: Boolean values and operators, conditional (if), alternative (if -else), chained conditional \n(if-elif-else); Iteration: while, for, break, continue. \n \nUNIT III \nFUNCTIONS, ARRAYS \nFruitf ul functions: return values, parameters, local and global scope, function composition, \nrecursion; Strings: string slices, immutability, string functions and methods, string",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 2
},
{
"content": " composition, \nrecursion; Strings: string slices, immutability, string functions and methods, string module; Python \narrays, Access the Elements of an Array, array methods. \n \nUNIT IV \nLISTS, TUPLES, DICTIONARIES \nLists: list operations, list slices, list methods, list loop, mutability, aliasing, cloning lists, list \nparameters, list comprehension; Tuples: tuple assignment, tuple as return value, tuple comprehension; \nDictionaries: operatio ns and methods, comprehension; \n \nUNIT V \nFILES, ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 2
},
{
"content": "e, tuple comprehension; \nDictionaries: operatio ns and methods, comprehension; \n \nUNIT V \nFILES, EXCEPTIONS, MODULES, PACKAGES \nFiles and exception: text files, reading and writing files, command line arguments, errors and \nexceptions, handling exceptions, modules (datetime, time, OS , calendar, math modul e), Explore \npackages. \n \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 2
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n \n OUTCOMES: Upon completion of the course, students will be able to \n Read, write, execute by hand simple Python programs. \n Structure simple Python programs for solving problems. \n Decompose a Python program into functions. \n Represent compound data using Python lists, tuples, dictionaries. \n Read and write data from/to files in Python Programs \n \n \nTEXT BOOKS \n1.Allen B. Downey, ``Th",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 3
},
{
"content": " \n Read and write data from/to files in Python Programs \n \n \nTEXT BOOKS \n1.Allen B. Downey, ``Think Python: How to Think Like a Computer Scientist‘‘, 2nd edition, \nUpdated for Python 3, Shroff/O‘Reilly Publis hers, 2016. \n2.R. Nageswara Rao, “Core Python Programming”, dreamtech \n3. Python Programming: A Modern Approach, Vamsi Kurama, Pearson \n \nREFERENCE BOOKS: \n1. Core Python Programming, W.Chun, Pearson. \n2. Introduction to Python, Kenneth A. Lambert, Cengage \n3. Learning Python, Mar",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 3
},
{
"content": " W.Chun, Pearson. \n2. Introduction to Python, Kenneth A. Lambert, Cengage \n3. Learning Python, Mark Lutz, Orielly \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 3
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n \n INDEX \n \nUNIT \nTOPIC \nPAGE NO \n \n \n \n \n \n \n \n \nI INTRODUCTION DATA, \nEXPRESSIONS, STATEMENTS 1 \nIntroduction to Python and installation 1 \ndata types : Int 6 \nfloat 7 \nBoolean 8 \nstring 8 \nList 10 \nvariables 11 \nexpressions 13 \nstatements 16 \nprecedence o f operators 17 \ncomments 18 \nmodules 19 \nfunctions ---- function and its use 20 \nflow of execution 21 \nparameters and argu",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 4
},
{
"content": "18 \nmodules 19 \nfunctions ---- function and its use 20 \nflow of execution 21 \nparameters and arguments 26 \n \n \n \nII CONTROL FLOW, LOOPS 35 \nConditionals : Boolean values and \noperators, 35 \nconditional (if) 36 \nalternative (if -else) 37 \nchained conditional (if -elif-else) 39 \nIteration: while, for, break, continue. 41 \n \n \n \n \n \nIII FUNCTIONS, ARRAYS 55 \nFruitful functions : return values 55 \nparameters 57 \nlocal and global scope 59 \nfunction composition 62 \nrecursi on 63 \nString",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 4
},
{
"content": "ues 55 \nparameters 57 \nlocal and global scope 59 \nfunction composition 62 \nrecursi on 63 \nStrings: string slices 64 \nimmutability 66 \nstring functions and methods 67 \nstring module 72 \nPython arrays 73 \nAccess the Elements of an Array 75 \nArray methods 76 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 4
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n \n \n \n \n \n \n \n \n \nIV LISTS, TUPLES, DICTIONARIES 78 \nLists 78 \nlist operations 79 \nlist slices 80 \nlist methods 81 \nlist loop 83 \nmutability 85 \naliasing 87 \ncloning lists 88 \nlist parameters 89 \nlist comprehension 90 \nTuples 91 \ntuple assignment 94 \ntuple as return value 95 \ntuple comprehension 96 \nDictionaries 97 \noperations and method s 97 \ncomprehension 102 \nV FILES, EXCEP",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 5
},
{
"content": "omprehension 96 \nDictionaries 97 \noperations and method s 97 \ncomprehension 102 \nV FILES, EXCEPTIONS, \nMODULES, PACKAGES 103 \nFiles and exception : text files 103 \nreading and writing files 104 \ncommand line arguments 109 \nerrors and exceptions 112 \nhandling exceptions 114 \nmodules (datetime, time, OS , calendar, \nmath module ) 121 \nExplore packages 134 \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 5
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n1 \n UNIT – I \nINTRODUCTION DATA, EXPRESSIONS, STATEMENTS \nIntroduction to Python and installation , data types: Int, float, Boolean, string, and list; \nvariables, expressions, statements, precedence of operators, comments; modules, functions --\n- function and its use, flow of execution, parameters and arguments. \nIntroduction to Python and installation : \nPython is a widely used general -purpos",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 6
},
{
"content": "nd arguments. \nIntroduction to Python and installation : \nPython is a widely used general -purpose, high level programming language. It was initially \ndesigned by Guido van Rossum in 1991 and developed by Python Software Foundation. It \nwas mainly developed for emphasis on code readability, and its syntax allows programme rs \nto express concepts in fewer lines of code. \nPython is a programming language that lets you work quickly and integrate systems more \nefficiently. \nThere are two majo",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 6
},
{
"content": "g language that lets you work quickly and integrate systems more \nefficiently. \nThere are two major Python versions - Python 2 and Python 3. \n \n• On 16 October 2000, Python 2.0 was released wit h many new features. \n• On 3rd December 2008, Python 3.0 was released with more testing and includes new \nfeatures. \n Beginning with Python programming: \n1) Finding an Interpreter: \nBefore we start Python programming, we need to have an interpreter to interpret a nd run our \nprograms. There ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 6
},
{
"content": "start Python programming, we need to have an interpreter to interpret a nd run our \nprograms. There are certain online interpreters like https://ide.geeksforgeeks.org/ , \nhttp://ideone.com/ or http://codepad.org/ that can be used to start Python without installing \nan interpreter. \nWindows : There are many interpreters available freely to run Python scripts like IDLE \n(Integrated Development Environment) which is installed when you install the python \nsoftware from http://python.org/ downloa",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 6
},
{
"content": "vironment) which is installed when you install the python \nsoftware from http://python.org/ downloads/ \n \n2) Writing first program: \n \n# Script Begins \nStatement1 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 6
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n2 \n Statement2 \nStatement3 \n# Script Ends \nDifferences between scripting language and programming language: \n \nWhy to use Python: \nThe following are the primary factors to use python in day-to-day life: \n1. Python is object -oriented \nStructure supports such concepts as polymorphism, operation overloading and \nmultiple inheritance. \n2. Indentation \nIndentation is one of t",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 7
},
{
"content": "sm, operation overloading and \nmultiple inheritance. \n2. Indentation \nIndentation is one of the greatest feature in python \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 7
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n3 \n 3. It’s free (open source) \nDownloading python and installing python is free and easy \n4. It’s Powerful \n Dynamic typing \n Built -in types and tools \n Library utilities \n Third party utilities (e.g. Numeric, NumPy, sciPy) \n Automatic memory management \n5. It’s Portable \n Python runs virtually every major platform used today \n As long as you have a compaitabl",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 8
},
{
"content": " \n Python runs virtually every major platform used today \n As long as you have a compaitable python interpreter installed, python \nprograms will run in exactly the same manner, irrespective of platform. \n6. It’s easy to use and learn \n No intermediate compile \n Python Programs are compiled automatically to an intermediate form called \nbyte code, which the interpreter then reads. \n This gives python the development speed of an interpreter without the \npe",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 8
},
{
"content": "ter then reads. \n This gives python the development speed of an interpreter without the \nperformance loss inherent in purely interpreted languages. \n Structure and syntax are pretty intuitive and easy to grasp. \n7. Interpreted Language \nPython is processed at runtime by python Interpreter \n8. Interactive Programming Language \nUsers can interact with the python interpreter directly for writing the programs \n9. Straight forward syntax \nThe formation of pyth",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 8
},
{
"content": "rpreter directly for writing the programs \n9. Straight forward syntax \nThe formation of python syntax is simple and straight forward which also makes it \npopular. \nInstallation: \nThere are many interpreters available freely to run Python scripts like IDLE (Integrated \nDevelopment Environment) which is installed when you install the python software \nfrom http://python.org/downloads/ \nSteps to be followed and remembered: \nStep 1: Select Version of Python to Install. \nStep",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 8
},
{
"content": "s/ \nSteps to be followed and remembered: \nStep 1: Select Version of Python to Install. \nStep 2: Download Python Executable Installer. \nStep 3: Run Executable Installer. \nStep 4: Verify Python Was Installed On Windows. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 8
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n4 \n Step 5: Verify Pip Was Installed. \nStep 6: Add Python Path to Environment Variables (Optional) \n \n \nWorking with Python \nPython Code Execution: \nPython’s traditional runtime execution model: Source code you type is translated to byte \ncode, which is then run by the Python Virtual Machine (PVM). Your code is automatically \ncompiled, but then it is interpreted. \n S",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 9
},
{
"content": "Virtual Machine (PVM). Your code is automatically \ncompiled, but then it is interpreted. \n Source Byte code Runtime \n \n \n \nSource code extension is .py \nByte code extension is .pyc (Compiled python code) \n \nThere are two modes for using the Python interpreter: \n• Interactive Mode \n• Script Mode \n \n m.py \n m.py c \n \n PVM ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 9
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n5 \n \nRunning Python in interactive mode: \nWithout passing python script file to the interpreter, directly execute code to Python prompt. \nOnce you’re inside the python interpreter, then you can start. \n>>> print(\"hello world\") \nhello world \n# Relevant output is displayed on subsequent lines without the >>> symbol \n>>> x=[0,1,2] \n# Quantities stored in memory are n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 10
},
{
"content": " subsequent lines without the >>> symbol \n>>> x=[0,1,2] \n# Quantities stored in memory are not displayed by default. \n>>> x \n#If a quantity is stored in memo ry, typing its name will display it. \n[0, 1, 2] \n>>> 2+3 \n5 \n \n \nThe chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python \ninterpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter \nreplies 8. \nRunning Python in script mode: \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 10
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n6 \n Alternatively, programmers can store Python script source code in a file with \nthe .py extension, and use the interpreter to execute the contents of the file. To execute the \nscript by the interpreter, you have to tell the interpreter the name of the file. For example, if \nyou have a script name MyFile.py and you're working on Unix, to run the script you have to \ntype",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 11
},
{
"content": "ave a script name MyFile.py and you're working on Unix, to run the script you have to \ntype: \npython MyFile.py \nWorking with the interactive mode is better when Python programmers deal with small \npieces of code as you can type and execute them immediately, but when the code is more \nthan 2-4 lines, using the script for coding can help to modify and use the code in future. \nExample: \n \nData types : \nThe data stored in memory can be of many types. For example,",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 11
},
{
"content": "n future. \nExample: \n \nData types : \nThe data stored in memory can be of many types. For example, a student roll number is \nstored as a numeric value and his or her address is stored as alphanumeric characters. Python \nhas various standard data types that are used to define the operations possible on them and \nthe storage method for each of them. \nInt: \nInt, or integer, is a whole number, positive or negative, without decimals, of unlimited \nlength. \n>>> print(24656354687654+2) \n2465635",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 11
},
{
"content": "itive or negative, without decimals, of unlimited \nlength. \n>>> print(24656354687654+2) \n24656354687656 \n>>> print(20) \n20 \n>>> print(0b10) \n2 \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 11
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n7 \n >>> print(0B10) \n2 \n>>> print(0X20) \n32 \n>>> 20 \n20 \n>>> 0b10 \n2 \n>>> a=10 \n>>> print(a) \n10 \n# To verify the type of any object in Python, use the type() function: \n>>> type(10) \n<class 'int'> \n>>> a=11 \n>>> print(type(a)) \n<class 'int'> \nFloat: \nFloat, or \"floating point number\" is a number, positive or negative, containing one or more \ndecimals. \nFloat can also be scientific ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 12
},
{
"content": "is a number, positive or negative, containing one or more \ndecimals. \nFloat can also be scientific numbers with an \"e\" to indicate the power of 10. \n>>> y=2.8 \n>>> y \n2.8 \n>>> y=2.8 \n>>> print(type(y)) \n<class 'float'> \n>>> type(.4) \n<class 'float'> \n>>> 2. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 12
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n8 \n 2.0 \nExample: \nx = 35e3 \ny = 12E4 \nz = -87.7e100 \nprint(type(x)) \nprint(type(y)) \nprint(type(z)) \nOutput: \n<class 'float'> \n<class 'float'> \n<class 'float'> \nBoolean: \nObjects of Boolean type may have one of two values, True or False: \n>>> type(True) \n<class 'bool'> \n>>> type(False) \n<class 'bool'> \nString: \n1. Strings in Python are identified as a contiguous set of c",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 13
},
{
"content": "pe(False) \n<class 'bool'> \nString: \n1. Strings in Python are identified as a contiguous set of characters represented in the \nquotation marks. Python allows for either pairs of single or double quotes. \n• 'hello' is the same as \"hello\". \n• Strings can be output to screen using the print function. For example: print(\"hello\"). \n>>> print(\"mrcet college\") \nmrcet college \n>>> type(\"mrcet college\") \n<class 'str'> ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 13
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n9 \n >>> print('mrcet college') \nmrcet college \n>>> \" \" \n' ' \nIf you want to include either type of quote character within the string, the simplest way is to \ndelimit the string with the other type. If a string is to contain a single quote, delimit it with \ndouble quotes and vice versa: \n>>> print(\"mrcet is an autonomous (') college\") \nmrcet is an autonomous (') college ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 14
},
{
"content": " versa: \n>>> print(\"mrcet is an autonomous (') college\") \nmrcet is an autonomous (') college \n>>> print('mrcet is an autonomous (\") college') \nmrcet is an autonomous (\") college \nSuppressing Special Character : \nSpecifying a backslash (\\) in front of the quote character in a string “escapes” it and causes \nPython to suppress its usual special meaning. It is then interpreted simply as a literal single \nquote character: \n>>> print(\"mrcet is an autonomous (\\') co",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 14
},
{
"content": "eted simply as a literal single \nquote character: \n>>> print(\"mrcet is an autonomous (\\') college\") \nmrcet is an autonomous (') college \n>>> print('mrcet is an autonomous (\\\") college') \nmrcet is an autonomous (\") college \nThe following is a table of escape sequences which cause Python to suppress the usual \nspecial interpretation of a character in a string: \n>>> print('a \\ \n....b') \na....b \n>>> print('a \\ \nb\\ \nc') ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 14
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n10 \n abc \n>>> print('a \\n b') \na \n b \n>>> print(\"mrcet \\n college\") \nmrcet \n college \n \nEscape \nSequence Usual Interpretation of \nCharacter(s) After Backslash “Escaped” Interpretation \n\\' Terminates string with single quote opening delimiter Literal single quote ( ') character \n\\\" Terminates string with double quote opening delimiter Literal double quote ( \") character \n\\newline Te",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 15
},
{
"content": "nates string with double quote opening delimiter Literal double quote ( \") character \n\\newline Terminates input line Newline is ignored \n\\\\ Introduces escape sequence Literal backslash ( \\) character \n \nIn Python (and almost all other common computer languages), a tab character can be \nspecified by the escape sequence \\t: \n>>> print(\"a \\tb\") \na b \nList: \n It is a general purpose most widely used in data structures \n List is a collection which is ordered and changeable and al",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 15
},
{
"content": " most widely used in data structures \n List is a collection which is ordered and changeable and allows duplicate members. \n(Grow and shrink as needed, sequence type, sortable). \n To use a list, you must declare it first. Do this using square brackets and separate \nvalues with commas. \n We can construct / create list in many ways. \nEx: \n>>> list1=[1,2,3,'A', 'B',7,8,[10,11]] \n>>> print(list1) \n[1, 2, 3, 'A', 'B', 7, 8, [10, 11]] ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 15
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n11 \n ---------------------- \n>>> x=list() \n>>> x \n[] \n-------------------------- \n>>> tuple1=(1,2,3,4) \n>>> x=list(tuple1) \n>>> x \n[1, 2, 3, 4] \nVariables : \nVariables are nothing but reserved memory locations to store values. This means that when \nyou create a variable you reserve some space in memory. \nBased on the data type of a variable, the interpreter allocates memo",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 16
},
{
"content": " some space in memory. \nBased on the data type of a variable, the interpreter allocates memory and decides what can \nbe stored in the reserved memory. Therefore, by assigning different data types to variables, \nyou can store integers, decimals or characters in these variables. \nRules for Python variables: \n• A variable name must start with a letter or the underscore character \n• A variable name cannot start with a number \n• A variable name can only contai",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 16
},
{
"content": " character \n• A variable name cannot start with a number \n• A variable name can only contain alpha -numeric characters and underscores (A-z, 0-9, \nand _ ) \n• Variable names are case-sensitive (age, Age and AGE are three different variables) \nAssigning Values to Variables: \nPython variables do not need explicit declaration to reserve memory space. The declaration \nhappens automatically when you assign a value to a variable. The equal sign (=) is used to \nas",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 16
},
{
"content": "appens automatically when you assign a value to a variable. The equal sign (=) is used to \nassign values to variables. \nThe operand to the left of the = operator is the name of the variable and the operand to the \nright of the = operator is the value stored in the variable. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 16
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n12 \n For example − \na= 100 # An integer assignment \nb = 1000.0 # A floating point \nc = \"John\" # A string \nprint (a) \nprint (b) \nprint (c) \nThis produces the following result − \n100 \n1000.0 \nJohn \nMultiple Assignment: \nPython allows you to assign a single value to several variables simultaneously. \nFor example : \na = b = c = 1 \nHere, a",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 17
},
{
"content": " a single value to several variables simultaneously. \nFor example : \na = b = c = 1 \nHere, an integer object is created with the value 1, and all three variables are assigned to the \nsame memory location. You can also assign multiple objects to multiple variables. \nFor example − \na,b,c = 1,2,\"mrcet“ \nHere, two integer objects with values 1 and 2 are assigned to variables a and b respectively, \nand one string object with the value \"john\" is assigned to the v",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 17
},
{
"content": "ables a and b respectively, \nand one string object with the value \"john\" is assigned to the variable c. \nOutput Variables: \nThe Python print statement is often used to output variables. \nVariables do not need to be declared with any particular type and can even change type after \nthey have been set. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 17
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n13 \n x = 5 # x is of type int \nx = \"mrcet \" # x is now of type str \nprint(x) \nOutput: mrcet \nTo combine both text and a variable, Python uses the “+” character: \nExample \nx = \"awesome\" \nprint(\"Python is \" + x) \nOutput \nPython is awesome \nYou can also use the + character to add a variable to another variable: \nExample \nx = \"Python is \" \ny = \"awesome\" \nz = x",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 18
},
{
"content": "acter to add a variable to another variable: \nExample \nx = \"Python is \" \ny = \"awesome\" \nz = x + y \nprint(z) \nOutput: \nPython is awesome \nExpressions: \nAn expression is a combination of values, variables, and operators. An expression is \nevaluated using assignment operator. \nExamples: Y=x + 17 \n>>> x=10 \n>>> z=x+20 \n>>> z \n30 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 18
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n14 \n \n>>> x=10 \n>>> y=20 \n>>> c=x+y \n>>> c \n30 \nA value all by itself is a simple expression, and so is a variable. \n>>> y=20 \n>>> y \n20 \nPython also defines expressions only contain identifiers, literals, and operators. So, \nIdentifiers: Any name that is used to define a class, function, variable module, or object is \nan identifier. \nLiterals: These are language -inde",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 19
},
{
"content": " function, variable module, or object is \nan identifier. \nLiterals: These are language -independent terms in Python and should exist independently in \nany programming language. In Python, there are the string literals, byte literals, integer \nliterals, floating point literals, and imaginary literals. \nOperators: In Python you can implement the following operations using the corresponding \ntokens. \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 19
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n15 \n \nOperator Token \nadd + \nsubtract - \nmultiply * \nInteger Division / \nremainder % \nBinary left shift << \nBinary right shift >> \nand & \nor \\ \nLess than < \nGreater than > \nLess than or equal to <= \nGreater than or equal to >= \nCheck equality == \nCheck not equal != ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 20
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n16 \n Some of the python expressions are: \nGenerator expression: \nSyntax: ( compute(var) for var in iterable ) \n \n>>> x = (i for i in 'abc') #tuple comprehension \n>>> x \n<generator object <genexpr> at 0x033EEC30> \n \n>>> print(x) \n<generator object <genexpr> at 0x033EEC30> \n \nYou might expect this to print as ('a', 'b', 'c') but it prints as <generator object <genexpr> \nat 0x02AAD710> ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 21
},
{
"content": "xpect this to print as ('a', 'b', 'c') but it prints as <generator object <genexpr> \nat 0x02AAD710> The result of a tuple comprehension is not a tuple: it is actually a \ngenerator. The only thing that you need to know now about a generator now is that you \ncan iterate over it, but ONLY ONCE. \n \nConditio nal expression: \nSyntax: true_value if Condition else false_value \n>>> x = \"1\" if True else \"2\" \n>>> x \n'1' \nStatements: \nA statement is an instruction that the Python interpreter can ex",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 21
},
{
"content": "lse \"2\" \n>>> x \n'1' \nStatements: \nA statement is an instruction that the Python interpreter can execute. We have normally two \nbasic statements , the assignment statement and the print statement. Some other kinds of \nstatements that are if statements, while statements, and for statements generally calle d as \ncontrol flows. \nExamples: \nAn assignment statement creates new variables and gives them values: \n>>> x=10 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 21
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n17 \n >>> college=\"mrcet\" \nAn print statement is something which is an input from the user, to be printed / displayed on \nto the screen (or ) monitor. \n>>> p rint(\"mrcet colege\") \nmrcet college \nPrecedence of Operators: \nOperator precedence affects how an expression is evaluated. \nFor example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher \nprecedence than +, so it",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 22
},
{
"content": " = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher \nprecedence than +, so it first multiplies 3*2 and then adds into 7. \nExample 1: \n>>> 3+4*2 \n11 \nMultiplication gets evaluated before the addition operation \n>>> (10+10)*2 \n40 \nParentheses () overriding the precedence of the arithmetic operators \nExample 2: \na = 20 \nb = 10 \nc = 15 \nd = 5 \ne = 0 \n \ne = (a + b) * c / d #( 30 * 15 ) / 5 \nprint(\"Value of (a + b) * c / d is \", e) \n \ne = ((a + b) * c) / d ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 22
},
{
"content": " c / d #( 30 * 15 ) / 5 \nprint(\"Value of (a + b) * c / d is \", e) \n \ne = ((a + b) * c) / d # (30 * 15 ) / 5 \nprint(\"Value of ((a + b) * c) / d is \", e) \n \ne = (a + b) * (c / d); # (30) * (15/5) ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 22
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n18 \n print(\"Value of (a + b) * (c / d) is \", e) \n \ne = a + (b * c) / d; # 20 + (150/5) \nprint(\"Value of a + (b * c) / d is \", e) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/opprec.py \nValue of (a + b) * c / d is 90.0 \nValue of ((a + b) * c) / d is 90.0 \nValue of (a + b) * (c / d) is 90.0 \nValue of a + (b * c) / d is 50.0 \n \nComments: \nSingle -line",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 23
},
{
"content": "\nValue of (a + b) * (c / d) is 90.0 \nValue of a + (b * c) / d is 50.0 \n \nComments: \nSingle -line comments begin s with a hash(#) symbol and is useful in mentioning that the \nwhole line should be considered as a comment until the end of line. \nA Multi line comment is useful when we need to comment on many lines. In python, triple \ndouble quote(“ “ “) and single quote(‘ ‘ ‘)are used for multi -line commenting. \nExample: \n \nOutput: \nC:/Users/MRCET/AppData/Local/Progra",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 23
},
{
"content": "‘)are used for multi -line commenting. \nExample: \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/comm.py \n30 \n \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 23
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n19 \n Modules: \nModules: Python module can be defined as a python program file which contains a python \ncode including python functions, class, or variables. In other words, we can say that our \npython code file saved with the extension (.py) is treated as the module. We may have a \nrunnable code inside the python module. A module in Python provides us the flexibili",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 24
},
{
"content": "ave a \nrunnable code inside the python module. A module in Python provides us the flexibility to \norganize the code in a logical way. To use the functionality of one module into another, we \nmust have to import the specific module. \nSyntax: \nimport <module -name> \nEvery module has its own functions, those can be accessed with . (dot) \nNote: In python we have help () \nEnter the name of any module, keyword, or topic to get help on writing Python programs",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 24
},
{
"content": "p () \nEnter the name of any module, keyword, or topic to get help on writing Python programs \nand using Python modules. To quit this help utility and return to the interpreter, just type \n\"quit\". \nSome of the modules like os, date, and calendar so on…… \n>>> import sys \n>>> print(sys.version) \n3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] \n>>> print(sys.version_info) \nsys.version_info(major=3, minor=8, micro=0, releaselevel='fina",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 24
},
{
"content": ")] \n>>> print(sys.version_info) \nsys.version_info(major=3, minor=8, micro=0, releaselevel='final', serial=0) \n>>> print(calendar.month(2021,5)) \n \n>>> print(calendar.isleap(2020)) \nTrue \n>>> print(calendar.isleap(2017)) \nFalse \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 24
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n20 \n Functions: \nFunctions and its use: Function is a group of related statements that perform a specific task. \nFunctions help break our program into smaller and modular chunks. As our program grows \nlarger and larger, functions make it more organized and manageable. It avoids repetition and \nmakes code reusable. \nBasically, we can divide functions into the following two types: \n1. Built -in f",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 25
},
{
"content": "s code reusable. \nBasically, we can divide functions into the following two types: \n1. Built -in functions - Functions that are built into Python. \nEx: abs(),all().ascii(),bool()………so on…. \ninteger = -20 \nprint('Absol ute value of -20 is:', abs(integer)) \nOutput: \nAbsolute value of -20 is: 20 \n2. User -defined functions - Functions defined by the users themselves. \ndef add_numbers(x ,y): \n sum = x + y \n return sum \n \nprint(\"The sum is\", add_numbers( 5, 20)) \nOutput: \nThe sum is ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 25
},
{
"content": " \n sum = x + y \n return sum \n \nprint(\"The sum is\", add_numbers( 5, 20)) \nOutput: \nThe sum is 25 \n \nFlow of Execution: \n1. The order in which statements are executed is called the flow of execution \n2. Execution always begins at the first statement of the program. \n3. Statements are executed one at a time, in order, from top to bottom. \n4. Function definitions do not alter the flow of execution of the program, but remember \nthat statements inside the function are not executed until t",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 25
},
{
"content": "execution of the program, but remember \nthat statements inside the function are not executed until the function is called. \n5. Function calls are like a bypass in the flow of execution. Instead of going to the next \nstatement, the flow jumps to the first line of the called function, executes all the \nstatements there, and then comes back to pick up where it left off. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 25
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n21 \n \nNote: When you read a program, d on’t read from top to bottom. Instead, follow the flow of \nexecution. This means that you will read the def statements as you are scanning from top to \nbottom, but you should skip the statements of the function definition until you reach a point \nwhere that f unction is called. \nExample: \n#example for flow of execution \n print(\"welcome\") \nfor x in ran",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 26
},
{
"content": " f unction is called. \nExample: \n#example for flow of execution \n print(\"welcome\") \nfor x in range(3): \n print(x) \nprint(\"Good morning college\") \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/flowof.py \nwelcome \n0 \n1 \n2 \nGood morning college \nThe flow/order of execution is: 2,3,4,3,4,3,4,5 \n \n------------------------------------------ \n \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 26
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n22 \n Output: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/flowof.py \nhi \nhello \nGood morning \nmrcet \ndone! \nThe flow/order of execution is: 2,5,6,7,2,3,4,7 ,8 \n \nParameters and arguments: \n \nParameters are passed during the definition of function while Arguments are passed during \nthe function call . \n \nExample: \n#here a and b are parameters \n \ndef add(a,b): #//function",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 27
},
{
"content": "during \nthe function call . \n \nExample: \n#here a and b are parameters \n \ndef add(a,b): #//function definition \n return a+b \n \n#12 and 13 are arguments \n#function call \nresult=add(12,13) \nprint(result) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/paraarg.py \n25 \nThere are three types of Python function arguments using which we can call a function. \n1. Default Arguments \n2. Keyword Arguments \n3. Variable -length Arguments \n \nSyntax: \ndef functionname()",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 27
},
{
"content": "lt Arguments \n2. Keyword Arguments \n3. Variable -length Arguments \n \nSyntax: \ndef functionname(): ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 27
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n23 \n statements \n . \n . \n . \nfunctionname() \n \nFunction definition consists of following components : \n \n1. Keyword def indicate s the start of function header. \n2. A function name to uniquely identify it. Functio n naming follows the same rules of writing \nidentifiers in Python . \n3. Parameters (arguments) through which we pass values to a function. They are optional. \n4. A colon (",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 28
},
{
"content": "Parameters (arguments) through which we pass values to a function. They are optional. \n4. A colon (:) to mark the end of function header. \n5. Optional documentation string (docstring) to describe what the function does. \n6. One or more valid python statements that make up the function body. Statements must have \nsame indentation level (usually 4 spaces). \n7. An optional return statement to return a value from the function. \n \nExample: \n \ndef hf(): \n hello world \nhf() \nIn the above",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 28
},
{
"content": " return a value from the function. \n \nExample: \n \ndef hf(): \n hello world \nhf() \nIn the above example we are just trying to execute the program by calling the function. So it \nwill not display any error and no output on to the screen but gets executed. \nTo get the statements of function need to be use print(). \n#calling function in python: \ndef hf(): \n print(\"hello world\") \nhf() \nOutput: \nhello world \n------------------------------- ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 28
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n24 \n def hf(): \n print(\"hw\") \n print(\"gh kfjg 66666\") \nhf() \nhf() \nhf() \nOutput: \nhw \ngh kfjg 66666 \nhw \ngh kfjg 66666 \nhw \ngh kfjg 66666 \n--------------------------------- \ndef add(x,y): \n c=x+y \n print(c) \nadd(5,4) \nOutput: \n9 \ndef add(x,y): \n c=x+y \n return c \nprint(add(5,4)) \nOutput: \n9 \n----------------------------------- ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 29
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n25 \n \ndef add_sub(x,y): \n c=x+y \n d=x-y \n return c,d \nprint(add_sub(10,5)) \nOutput: \n (15, 5) \nThe return statement is used to exit a function and go back to the place from where it was \ncalled . This statement can contain expression which gets evaluated and the value is returned. \nIf there is no expression in the statement or the return statement itself is not present inside a \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 30
},
{
"content": "f there is no expression in the statement or the return statement itself is not present inside a \nfunction, then the function will return the None object. \ndef hf(): \n return \"hw\" \nprint(hf()) \nOutput: \nhw \n---------------------------- \n \ndef hf(): \n return \"hw\" \nhf() \nOutput: \nC:/Users/MRCET/AppData/Local/Programs /Python/Python38 -32/pyyy/fu.py \n>>> ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 30
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n26 \n ------------------------------------- \ndef hello_f(): \n return \"hello college \" \nprint(hello_f().upper()) \nOutput: \nHELLOCOLLEGE \n# Passing Arguments \ndef hello(wish): \n return '{}'.format(wish) \nprint(hello(\" mrcet \")) \nOutput: \nmrcet \n------------------------------------------------ \nHere, the function wish () has two parameters. Since, we have called this function with two",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 31
},
{
"content": "---- \nHere, the function wish () has two parameters. Since, we have called this function with two \narguments, it runs smoothly and we do not get any error. If we call it with different number \nof arguments, the interpreter will give errors . \ndef wish(name,msg): \n \"\"\"This function greets to \n the person with the provided message\"\"\" \n print (\"Hello\",name + ' ' + msg) \nwish(\"MRCET\",\"Good morning!\") \nOutput: \nHello MRCET Good morning! \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 31
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n27 \n Below is a call to this function with one and no arguments along with their respective error \nmessages. \n>>> wish (\"MRCET \") # only one argument \nTypeError: wish () missing 1 required positional argument: 'msg' \n>>> wish () # no arguments \nTypeError: wish () missing 2 required positional arguments: 'name' and 'msg' \n \n---------------------------------------------- \ndef hello(wish,h",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 32
},
{
"content": "al arguments: 'name' and 'msg' \n \n---------------------------------------------- \ndef hello(wish,hello): \n return “hi” '{},{}'.format( wish,hello) \nprint(hello(\" mrcet \",\"college \")) \nOutput: \nhimrcet,college \n#Keyword Arguments \nWhen we call a function with some values, these values get assigned to the arguments \naccording to their position . \nPython allows functions to be called using keyword arguments. When we call functions in \nthis way, the order (position) of the arguments can",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 32
},
{
"content": "ng keyword arguments. When we call functions in \nthis way, the order (position) of the arguments can be changed. \n(Or) \nIf you have some functions with many parameters and you want to specify only some \nof them, then you can give values for such parameters by naming them - this is \ncalled keyword arguments - we use the name (keyword) instead of the position \n(which we have been using all along) to specify the arguments to the function. \nThere are two advantages - one, using the function i",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 32
},
{
"content": ") to specify the arguments to the function. \nThere are two advantages - one, using the function is easier since we do not need to \nworry about the order of the arguments. Two, we can give values to only those \nparameters which we want, provided that the other parameters have default argument \nvalues. \ndef func(a, b=5, c=10): \n print 'a is', a, 'and b is', b, 'and c is', c ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 32
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n28 \n \nfunc(3, 7) \nfunc(25, c=24) \nfunc(c=50, a=100) \n \nOutput: \na is 3 and b is 7 and c is 10 \na is 25 and b is 5 and c is 24 \na is 100 and b is 5 and c is 50 \n \nNote: \nThe function named func has one parameter without default argument values, \nfollowed by two parameters with default a rgument values. \nIn the first usage, func(3, 7), the parameter a gets the value 3, the parameter b ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 33
},
{
"content": "ent values. \nIn the first usage, func(3, 7), the parameter a gets the value 3, the parameter b gets the \nvalue 5 and c gets the default value of 10. \nIn the second usage func(25, c=24), the variable a gets the value of 25 due to the \nposition of the argume nt. Then, the parameter c gets the value of 24 due to naming i.e. \nkeyword arguments. The variable b gets the default value of 5. \nIn the third usage func(c=50, a=100), we use keyword arguments completely to \nspecify the values.",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 33
},
{
"content": " \nIn the third usage func(c=50, a=100), we use keyword arguments completely to \nspecify the values. Notice, that we are specifying value for parameter c before that \nfor a even though a is defined before c in the function definition. \nFor example: if you define the function like below \ndef func(b=5, c=10,a): # shows error : non-default argument follows default argument \n------------------------------------------------------- \ndef print_name(name1, name2): \n \"\"\" This function prints ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 33
},
{
"content": "-------------------------------------- \ndef print_name(name1, name2): \n \"\"\" This function prints the name \"\"\" \n print (name1 + \" and \" + name2 + \" are friends\") \n#calling the function \nprint_name(name2 = 'A',name1 = 'B') ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 33
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n29 \n Output: \nB and A are friends \n#Default Arguments \nFunction arguments can have default values in Python. \nWe can provide a default value to an argument by using the assignment operator (=) \ndef hello(wish,name='you'): \n return '{},{}'.format(wish,name) \nprint(hello(\" good morning \")) \nOutput: \ngood morning,you \n--------------------------------------------- \ndef hello(wish,name='you",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 34
},
{
"content": "utput: \ngood morning,you \n--------------------------------------------- \ndef hello(wish,name='you'): \n return '{},{}'.format(wish,name) //print(wish + ‘ ‘ + name) \nprint(hello(\"good morning\",\"nirosha\")) // hello(\"good morning\",\"nirosha\") \nOutput: \ngood morning,nirosha // good morning nirosha \nNote: Any number of arguments in a function can have a default value. But once we have a \ndefault argument, all the arguments to its right must also have default values. ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 34
},
{
"content": "ut once we have a \ndefault argument, all the arguments to its right must also have default values. \nThis means to say, non -default arguments cannot follow default arguments. For example, if \nwe had defined the function header above as: \ndef hello(name=' you', wish ): \nSyntax Error: non -default argument follows default argument \n------------------------------------------ \ndef sum(a=4, b=2): #2 is supplied as default argument ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 34
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n30 \n \"\"\" This function will print sum of two numbers \n if the arguments are not supplied \n it will add the default value \"\"\" \n print (a+b) \nsum(1,2) #calling with arguments \nsum( ) #calling without arguments \nOutput: \n3 \n6 \nVariable -length arguments \nSometimes you may need more arguments to process function then you mentioned in the \ndefinition. If we don’t know in advance",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 35
},
{
"content": "ore arguments to process function then you mentioned in the \ndefinition. If we don’t know in advance about the arguments needed in function, we can use \nvariable -length arguments also called arbit rary arguments. \nFor this an asterisk (*) is placed before a parameter in function definition which can hold \nnon-keyworded variable -length arguments and a double asterisk (**) is placed before a \nparameter in function which can hold keyworded variable -length arguments. \nIf we use one asterisk (*",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 35
},
{
"content": "ameter in function which can hold keyworded variable -length arguments. \nIf we use one asterisk (*) like *var, then all the positional arguments from that point till the \nend are collected as a tuple called ‘var’ and if we use two asterisks (**) before a variable like \n**var, then all the positional arguments from that point till the end are collected as \na dictionary called ‘var’. \n \ndef wish(*names): \n \"\"\"This function greets all \n the person in the names tuple.\"\"\" \n \n # nam",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 35
},
{
"content": "f wish(*names): \n \"\"\"This function greets all \n the person in the names tuple.\"\"\" \n \n # names is a tuple with arguments \n for name in names: \n print(\"Hello\",name) \n \nwish(\"MRCET\",\"CSE\",\"SIR\",\"MADAM\") ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 35
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n31 \n \nOutput: \nHello MRCET \nHello CSE \nHello SIR \nHello MADAM \n \n#Program to find area of a circle using function use single return value function with \nargument. \npi=3.14 \ndef areaOfCircle(r): \n \n return pi*r*r \nr=int(input(\"Enter radius of circle\")) \n \nprint(areaOfCircle(r)) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter radius ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 36
},
{
"content": " \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter radius of circle 3 \n28.259999999999998 \n \n#Program to write sum different product and using arguments with return value \nfunction. \ndef calculete(a,b): \n total=a+b \n diff=a -b \n prod=a*b \n div=a/b \n mod=a%b ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 36
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n32 \n return total,diff,prod,div,mod \na=int(input(\"Enter a value\")) \nb=int(input(\"Enter b value\")) \n #function call \ns,d,p,q,m = calculete(a,b) \nprint(\"Sum= \",s,\"diff= \",d,\"mul= \",p,\"div= \",q,\"mod= \",m) \n#print(\"diff= \",d) \n#print(\"mul= \",p) \n#print(\"div= \",q) \n#print(\"mod= \",m) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter a value 5 \nE",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 37
},
{
"content": "utput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter a value 5 \nEnter b value 6 \nSum= 11 diff= -1 mul= 30 div= 0.8333333333333334 mod= 5 \n \n#program to find biggest of two numbers using functions. \ndef biggest(a,b): \n if a>b : \n return a \n else : \n return b \n \n \na=int(input(\"Enter a value\")) \nb=int(input(\"Enter b value\")) \n #function call \nbig= biggest(a,b) \nprint(\"big number= \",big) \n \nOutput: ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 37
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n33 \n C:/Users/MRC ET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter a value 5 \nEnter b value -2 \nbig number= 5 \n#program to find biggest of two numbers using functions. (nested if) \n \ndef biggest(a,b,c): \n if a>b : \n if a>c : \n return a \n else : \n return c \n else : \n if b>c : \n return b \n else : \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 38
},
{
"content": " return c \n else : \n if b>c : \n return b \n else : \n return c \n \n \na=int(input(\"Enter a value\")) \nb=int(input(\"Enter b value\")) \nc=int(input(\"Enter c value\")) \n #function call \nbig= biggest(a,b ,c) \nprint(\"big number= \",big) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter a value 5 \nEnter b value -6 \nEnter c value 7 \nbig number= 7 \n \n#Writer a program to read one subject mark and ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 38
},
{
"content": "ter b value -6 \nEnter c value 7 \nbig number= 7 \n \n#Writer a program to read one subject mark and print pass or fail use single return \nvalues function with argument. \ndef result(a): \n if a>40: \n return \"pass\" ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 38
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n34 \n else: \n return \"fail\" \na=int(input(\"Enter one subject marks\")) \n \nprint(result(a)) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nEnter o ne subject marks 35 \nfail \n \n#Write a program to display mrecet cse dept 10 times on the screen. (while loop) \ndef usingFunctions(): \n count =0 \n while count<10: \n print(\"mrcet cse dept\",",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 39
},
{
"content": "loop) \ndef usingFunctions(): \n count =0 \n while count<10: \n print(\"mrcet cse dept\",count) \n count=count+1 \n \nusingFunctions() \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nmrcet cse dept 0 \nmrcet cse dept 1 \nmrcet cse dept 2 \nmrcet cse dept 3 \nmrcet cse dept 4 \nmrcet cse dept 5 \nmrcet cse dept 6 \nmrcet cse dept 7 \nmrcet cse dept 8 \nmrcet cse dept 9 \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 39
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n35 \n UNIT – II \nCONTR OL FLOW, LOOPS \nConditionals: Boolean values and operators, conditional (if), alternative (if -else), chained \nconditional (if -elif-else); Iteration: while, for, break, continue. \nControl Flow, Loops : \nBoolean Values and Operators: \nA boolean expression is an expression that is either true or false. The following examples \nuse the operator ==, which compares two operands",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 40
},
{
"content": "at is either true or false. The following examples \nuse the operator ==, which compares two operands and produces True if they are equal and \nFalse otherwise: \n>>> 5 == 5 \nTrue \n>>> 5 == 6 \nFalse \nTrue and False are special values that belong to the type bool; they are not strings: \n>>> type(True) \n<class 'bool'> \n>>> type(False) \n<class 'bool'> \nThe == operator is one of the relational operators; the others are: x != y # x is not equal to y \nx > y # x is greater than y x < y # x is l",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 40
},
{
"content": "operators; the others are: x != y # x is not equal to y \nx > y # x is greater than y x < y # x is less than y \nx >= y # x i s greater than or equal to y x <= y # x is less than or equal to y \nNote: \nAll expressions involving relational and logical operators will evaluate to either true or false \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 40
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n36 \n Conditional (if): \nThe if statement contains a logical expression using which data is compared and a decision \nis made based on the result of the comparison. \nSyntax: \nif expression: \n statement(s) \nIf the boolean expression evaluates to TRUE, then the block of statement(s) inside the if \nstatement is executed. If boolean expression evaluates to FALSE, then the first set of \ncode aft",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 41
},
{
"content": "f \nstatement is executed. If boolean expression evaluates to FALSE, then the first set of \ncode after the end of the if statement(s) is executed. \nif Statement Flowchart : \n \n Fig: Operation of if statement \nExample: Python if Statement \na = 3 \nif a > 2: \n print(a, \"is greater\") \nprint(\"done\") \n \na = -1 \nif a < 0: \n print(a, \"a is smaller\") \nprint(\"F inish\") \n \nOutput: \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 41
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n37 \n \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/if1.py \n3 is greater \ndone \n-1 a is smaller \nFinish \n-------------------------------- \na=10 \nif a>9: \n print(\"A is Greater than 9\" ) \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/if2.py \nA is Greater than 9 \nAlternative i f (If-Else): \nAn else statement can be combined with an if statemen",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 42
},
{
"content": " Greater than 9 \nAlternative i f (If-Else): \nAn else statement can be combined with an if statement. An else statement contains the \nblock of code (false block) that executes if the conditional expression in the if statement \nresolves to 0 or a FALSE value. \nThe else statement is an optional statement and there could be at most only one else \nStatement following if. \nSyntax of if - else : \nif test expression: \n Body of if stmts \nelse: \n Body of else stmts \nIf - else Flowchart : ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 42
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n38 \n \n Fig: Operation of if – else statement \nExample of if - else: \na=int(input('enter the number')) \nif a>5: \n print(\"a is greater\") \nelse: \n print(\"a is smaller than the input given\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ifelse.py \nenter the number 2 \na is smaller than the input given \n--------------------------------------",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 43
},
{
"content": "py \nenter the number 2 \na is smaller than the input given \n---------------------------------------- \na=10 \nb=20 \nif a>b: \n print(\"A is Greater than B\") \nelse: \n print(\"B is Greater than A\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/if2.py \nB is Greater than A \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 43
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n39 \n \nChained Conditional: (If -elif-else): \n \nThe elif statement allows us to check multiple expressions for TRUE and execute a \nblock of code as soon as one of the c onditions evaluates to TRUE. Similar to the else, \nthe elif statement is optional. However, unlike else, for which there can be at most one \nstatement, there can be an arbitrary number of elif statements following an if. \nSyntax of",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 44
},
{
"content": "ost one \nstatement, there can be an arbitrary number of elif statements following an if. \nSyntax of if – elif - else : \nIf test expression: \n Body of if stmts \nelif test expression: \n Body of elif stmts \nelse: \n Body of else stmts \n \nFlowchart of if – elif - else: \n \n Fig: Operation of if – elif - else statement \n Example of if - elif – else: \na=int(input('enter the number')) \nb=int(input('enter the number')) \nc=int(input('enter the number')) \nif a>b: \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 44
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n40 \n print(\"a is greater\") \nelif b>c: \n print(\"b is greater\") \nelse: \n print(\"c is greater\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ife lse.py \nenter the number5 \nenter the number2 \nenter the number9 \na is greater \n>>> \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ifelse.py \nenter the number2 \nenter the number5 \nenter the",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 45
},
{
"content": "ocal/Programs/Python/Python38 -32/pyyy/ifelse.py \nenter the number2 \nenter the number5 \nenter the number9 \nc is greater \n----------------------------- \nvar = 100 \nif var == 200: \n print(\"1 - Got a true expression value\") \n print(var) \nelif var == 150: \n print(\"2 - Got a true expression value\") \n print(var) \nelif var == 100: \n print(\"3 - Got a true expression value\") \n print(var) \nelse: \n print(\"4 - Got a false expression value\") \n print(var) \nprint(\"Good ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 45
},
{
"content": " print(var) \nelse: \n print(\"4 - Got a false expression value\") \n print(var) \nprint(\"Good bye!\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/ifelif.py \n3 - Got a true expression value \n100 \nGood bye! ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 45
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n41 \n Iteration: \nA loop statement allows us to execute a statement or group of statements multiple times as \nlong as the condition is true . Repeated execution of a set of statements with the help of loops \nis called iteration. \nLoops statements are used when we need to run same code again and again, each time with a \ndifferent v alue. \n \nStatements: \nIn Python Iteration (Loops) statements are",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 46
},
{
"content": "in, each time with a \ndifferent v alue. \n \nStatements: \nIn Python Iteration (Loops) statements are of three types: \n1. While Loop \n2. For Loop \n3. Nested For Loops \nWhile loop : \n Loops are either infinite or conditional. Python while loop keeps reiterating a block of \ncode defined inside it until the desired condition is met. \n The while loop contains a boolean expression and the code inside the loop is \nrepeatedly executed as long as the boolean expression is true. \n The statemen",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 46
},
{
"content": " inside the loop is \nrepeatedly executed as long as the boolean expression is true. \n The statements that are executed inside while can be a single line of code or a block of \nmultiple statements. \nSyntax: \n \n while(expression): \n Statement(s) \n \nFlowchart: \n \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 46
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n42 \n Example Programs: \n1. -------------------------------------- \ni=1 \n while i<=6: \n print(\"Mrcet college\") \n i=i+1 \noutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh1.py \nMrcet college \nMrcet college \nMrcet college \nMrcet college \nMrcet college \nMrcet college \n2. ----------------------------------------------------- \ni=1 \n \nwhile i<=3: \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 47
},
{
"content": "rcet college \n2. ----------------------------------------------------- \ni=1 \n \nwhile i<=3: \n print(\"MRCET\",end=\" \") \n j=1 \n while j<=1: \n print(\"CSE DEPT\",end=\"\") \n j=j+1 \n i=i+1 \n print() \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh2.py \nMRCET CSE DEPT \nMRCET CSE DEPT \nMRCET CSE DEPT \n3. -------------------------------------------------- \ni=1 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 47
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n43 \n j=1 \nwhile i<=3: \n print(\"MRCET\",end=\" \") \n \n while j<=1: \n print (\"CSE DEPT\",end=\"\") \n j=j+1 \n i=i+1 \n print() \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh3.py \nMRCET CSE DEPT \nMRCET \nMRCET \n \n4. ---------------------------------------- \n \n i = 1 \n while (i < 10): \n print (i) \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 48
},
{
"content": "------------------------ \n \n i = 1 \n while (i < 10): \n print (i) \n i = i+1 \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh4.py \n1 \n2 \n3 \n4 \n5 \n6 \n7 \n8 \n9 \n \n2. --------------------------------------- \n a = 1 \n b = 1 \n while (a<10): \n print ('Iteration',a) \n a = a + 1 \n b = b + 1 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 48
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n44 \n if (b == 4): \n break \n print ('While loop terminated') \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh5.py \nIteration 1 \nIteration 2 \nIteration 3 \nWhile loop terminated \n-------------------------- \ncount = 0 \nwhile (count < 9): \n print(\"The count is:\", count) \n count = count + 1 \nprint(\"Good bye!\") \n \nOutput: \nC:/Users/MRCET",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 49
},
{
"content": "nt(\"The count is:\", count) \n count = count + 1 \nprint(\"Good bye!\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/wh.py = \nThe count is: 0 \nThe count is: 1 \nThe count is: 2 \nThe count is: 3 \nThe count is: 4 \nThe count is: 5 \nThe count is: 6 \nThe count is: 7 \nThe count is: 8 \nGood bye! \n \nFor loop : \nPython for loop is used for repeated execution of a group of statements for the desired \nnumber of times. It iterates over the items of lists, tuples,",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 49
},
{
"content": " group of statements for the desired \nnumber of times. It iterates over the items of lists, tuples, strings, the dictionaries and other \niterable objects \nSyntax: for var in sequence: \n Statement(s) A sequence of values assigned to var in each iteration \nHolds the value of item \nin sequence in each iteration \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 49
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n45 \n Sample Program : \nnumbers = [1, 2, 4, 6, 11, 20] \nseq=0 \nfor val in numbers: \n seq=val*val \n print(seq) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/fr.py \n1 \n4 \n16 \n36 \n121 \n400 \n \nFlowchart: \n \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 50
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n46 \n Iterating over a list: \n#list of items \nlist = ['M','R','C','E','T'] \ni = 1 \n \n#Iterating over the list \nfor item in list: \n print ('college ',i,' is ',item) \n i = i+1 \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/lis.py \ncollege 1 is M \ncollege 2 is R \ncollege 3 is C \ncollege 4 is E \ncollege 5 is T \n \nIterating over a Tuple: \ntuple =",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 51
},
{
"content": " R \ncollege 3 is C \ncollege 4 is E \ncollege 5 is T \n \nIterating over a Tuple: \ntuple = (2,3,5,7) \nprint ('These are the first four prime numbers ') \n#Iterating over the tuple \nfor a in tuple: \n print (a) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fr3.py \nThese are the first four prime numbers \n2 \n3 \n5 \n7 \n \nIterating over a dictionary: \n#creating a dictionary \ncollege = {\"ces\":\"block1\",\"it\":\"block2\",\"ece\":\"block3\"} \n \n#Iterating over the",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 51
},
{
"content": "ating a dictionary \ncollege = {\"ces\":\"block1\",\"it\":\"block2\",\"ece\":\"block3\"} \n \n#Iterating over the dictionary to print keys \nprint ('Keys are:') ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 51
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n47 \n for keys in college: \n print (key s) \n \n#Iterating over the dictionary to print values \nprint ('Values are:') \nfor blocks in college.values(): \n print(blocks) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/dic.py \nKeys are: \nces \nit \nece \nValues are: \nblock1 \nblock2 \nblock3 \n \nIterating over a String : \n#declare a string to iterate over \ncollege = '",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 52
},
{
"content": "k1 \nblock2 \nblock3 \n \nIterating over a String : \n#declare a string to iterate over \ncollege = 'MRCET' \n \n#Iterating over the string \nfor name in college: \n print (name) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/strr.py \nM \nR \nC \nE \nT \n \nNested For loop: \nWhen one Loop defined within another Loop is called Nested Loops. \nSyntax: \nfor val in sequence: \nfor val in sequence: ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 52
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n48 \n statements \nstatements \n \n# Example 1 of Nested For Loops (Pattern Programs) \nfor i in range(1,6): \n for j in range(0,i): \n print(i, end=\" \") \n print('') \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/nesforr.py \n1 \n2 2 \n3 3 3 \n4 4 4 4 \n5 5 5 5 5 \n-------------------------- \n# Example 2 of Nested For Loops (Pattern Programs) \nfor i in range(1,",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 53
},
{
"content": "\n-------------------------- \n# Example 2 of Nested For Loops (Pattern Programs) \nfor i in range(1,6): \n for j in range(5,i -1,-1): \n print(i, end=\" \") \n print('') \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/nesforr.py \nOutput: \n1 1 1 1 1 \n2 2 2 2 \n3 3 3 \n4 4 ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 53
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n49 \n Break and continue: \nIn Python, break and continue statements can alter the flow of a normal loop. Sometimes \nwe wish to terminate the current iteration or even the whole loop without checking test \nexpression. The break and continue statements are used in these cases. \n \nBreak: \nThe break statement term inates the loop containing it and c ontrol of the progra m flows to \nthe statement i",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 54
},
{
"content": " statement term inates the loop containing it and c ontrol of the progra m flows to \nthe statement immediately after the body of the loop. If break statement is inside a nested \nloop (loop inside another loop), break will terminate the innermost loop. \nFlowchart: \n \nThe following shows the working of break statement in for and while loop : \nfor var in sequence: \n # code inside for loop \nIf condition: \n break (if break condition satisfies it jumps to outside loop) \n# code inside fo",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 54
},
{
"content": " \nIf condition: \n break (if break condition satisfies it jumps to outside loop) \n# code inside for loop \n# code outside for loop \n \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 54
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n50 \n while test expression \n# code inside while loop \nIf condition: \n break (if break condition satisfies it jumps to outside loop) \n# code inside while loop \n# code outside while loop \n \nExample: \nfor val in \"MRCET COLLEGE\": \n if val == \" \": \n break \n print(val) \n \nprint(\"The end\") \n \nOutput: \nM \nR \nC \nE \nT \nThe end \n \n# Program to disp lay all the elements before number",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 55
},
{
"content": " end\") \n \nOutput: \nM \nR \nC \nE \nT \nThe end \n \n# Program to disp lay all the elements before number 88 \n \nfor num in [11, 9, 88, 10, 90, 3, 19]: \n print(num) \n if(num==88): \n print(\"The number 88 is found\") \n print(\"Terminating the loop\") \n break \n \nOutput: \n11 \n9 \n88 \nThe number 88 is found ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 55
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n51 \n Terminating the loop \n \n#------------------------------------- \nfor letter in \"Python\": # First Example \n if letter == \"h\": \n break \n print(\"Current Letter :\", letter ) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/br.py = \nCurrent Letter : P \nCurrent Letter : y \nCurrent Letter : t \nContinue: \nThe continue statement is used to skip the rest",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 56
},
{
"content": "urrent Letter : y \nCurrent Letter : t \nContinue: \nThe continue statement is used to skip the rest of the code inside a loop for the current \niteration only. Loop does not terminate but continues on with the next iteration. \nFlowchart: \n \nThe following shows the working of break statement in for and while loop : \n",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 56
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n52 \n for var in sequence: \n # code inside for loop \nIf condition: \n continue (if break condition satisfies it jumps to outside loop) \n# code inside for loop \n# code outside for loop \n \nwhile test ex pression \n# code inside while loop \nIf condition: \n continue(if break condition satisfies it jumps to outside loop) \n# code inside while loop \n# code outside while loop \n \nExample: \n# Progr",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 57
},
{
"content": "jumps to outside loop) \n# code inside while loop \n# code outside while loop \n \nExample: \n# Program to show the use of continue statement inside loops \n \nfor val in \"string\": \n if val == \"i\": \n continue \n print(val) \n \nprint(\"The end\") \n \nOutput: \n \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/cont.py \ns \nt \nr \nn \ng \nThe end \n \n# program to display only odd numbers \n \nfor num in [20, 11, 9, 66, 4, 89, 44]: ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 57
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n53 \n # Skipping the iteration when number is even \n if num%2 == 0: \n continue \n # This statement will be skipped for all even numbers \n print(num) \n \nOutput: \n \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/cont2.py \n11 \n9 \n89 \n \n#------------------ \nfor letter in \"Python\": # First Example \n if letter == \"h\": \n continue \n print(\"Current L",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 58
},
{
"content": "etter in \"Python\": # First Example \n if letter == \"h\": \n continue \n print(\"Current Letter :\", letter) \nOutput: \n \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/con1.py \nCurrent Letter : P \nCurrent Letter : y \nCurrent Letter : t \nCurrent Letter : o \nCurrent Letter : n \n \nPass: \n \nIn Python programming, pass is a null statement. The difference between \na comment and pass statement in Python is that, while the interpreter ignores a comment \nentirely, pas",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 58
},
{
"content": "mment and pass statement in Python is that, while the interpreter ignores a comment \nentirely, pass is not ignored. \npass is just a placeholder for functionality to be added later. \n \nExample: \nsequence = {'p', 'a', 's', 's'} \nfor val in sequence: \n pass \n \nOutput: \n \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/f1.y.py ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 58
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n54 \n >>> \n \nSimilarily we can also write, \n \ndef f(arg): pass # a function that does nothing (yet) \n \nclass C: pass # a class with no methods (yet) \n ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 59
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n55 \n UNIT – III \nFUNCTIONS, ARRAYS \nFruitful functions: return values, parameters, local and global scope, function composition, \nrecursion; Strings: string slices, immutability, string functions and methods, string module; \nPython arrays, Access the Elements of an Array, ar ray methods. \nFunctions, Arrays : \nFruitful functions: \nWe write functions that return values, which we will call fruitf",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 60
},
{
"content": "s, Arrays : \nFruitful functions: \nWe write functions that return values, which we will call fruitful functions . We have seen \nthe return statement before, but in a fruitful function the return statement includes a return \nvalue . This statement means: \"Return immediately from this function and use the following \nexpression as a return value.\" \n(or) \nAny function that returns a value is called Fruitful function. A Function that does not return \na value is called a void function \nReturn ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 60
},
{
"content": "lled Fruitful function. A Function that does not return \na value is called a void function \nReturn values: \nThe Keyword return is used to return back the value to the called function. \n# returns the area of a circle with the given radius: \ndef area(radius): \n temp = 3.14 * radius**2 \n return temp \nprint(area(4)) \n \n (or) \n \ndef area(radius): \n return 3.14 * radius**2 \nprint(area(2)) \n \nSometimes it is useful to have multiple return statements, one in each branch of a \ncond",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 60
},
{
"content": "rea(2)) \n \nSometimes it is useful to have multiple return statements, one in each branch of a \nconditional: \ndef absolute_value(x): \n if x < 0: ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 60
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n56 \n return -x \n else: \n return x \nSince these return statements are in an alternative conditional, only one will be executed. \nAs soon as a return statement executes, the function terminates without executing any \nsubsequent statements. Code that appears after a return statement, or any other place the \nflow of execution can never reach, is called dead code. \nIn a fruitfu",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 61
},
{
"content": "ent, or any other place the \nflow of execution can never reach, is called dead code. \nIn a fruitful function, it is a good idea to ensure that every possible path through the program \nhits a return statement. For example: \ndef absolute_value(x): \n if x < 0: \n return -x \n if x > 0: \n return x \nThis fu nction is incorrect because if x happens to be 0, both conditions is true, and the \nfunction ends without hitting a return statement. If the flow of execution gets to",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 61
},
{
"content": " true, and the \nfunction ends without hitting a return statement. If the flow of execution gets to the end of a \nfunction, the return value is None, which is not the absolute value of 0. \n>>> print absolute_value(0) \nNone \nBy the way, Python provides a built -in function called abs that computes absolute values. \n# Write a Python function that takes two lists and returns True if they have at least one \ncommon member . \ndef common_data(list1, list2): \n for x in list1: \n for y ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 61
},
{
"content": "at least one \ncommon member . \ndef common_data(list1, list2): \n for x in list1: \n for y in list2: \n if x == y: \n result = True \n return result \nprint(common_data([1,2,3,4,5], [1,2,3,4,5])) \nprint(common_data([1,2,3,4,5], [1,7,8,9,510])) \nprint(common_data([1,2,3,4,5], [6,7,8 ,9,10])) \n \nOutput: \nC:\\Users \\MRCET \\AppData \\Local \\Programs \\Python \\Python38 -32\\pyyy \\fu1.py ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 61
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n57 \n True \nTrue \nNone \n \n#----------------- \ndef area(radius): \n b = 3.14159 * radius**2 \n return b \nParameters: \nParameters are passed during the definition of function while Arguments are passed during \nthe function call . \n \nExample: \n#here a and b are parameters \n \ndef add(a,b): #//function definition \n return a+b \n \n#12 and 13 are arguments \n#function call \nresult=add(12,1",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 62
},
{
"content": "//function definition \n return a+b \n \n#12 and 13 are arguments \n#function call \nresult=add(12,13) \nprint(result) \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/paraarg.py \n25 \n \nSome examples on functions: \n# To display vandemataram by using function use no args no return type \n#function defination \ndef display(): \n print(\"vandemataram\") \nprint(\"i am in mai n\") ",
"document_name": "Demo2.pdf",
"course_id": "pyt123",
"page_number": 62
},
{
"content": "PYTHON PROGRAMMING III YEAR/II SEM MRCET \n58 \n \n#function call \ndisplay() \nprint(\"i am in main\") \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \ni am in main \nvandemataram \ni am in main \n \n#Type1 : No parameters and no return type \ndef Fun1() : \n print(\"function 1\") \nFun1() \n \nOutput: \nC:/Users/MRCET/AppData/Local/Programs/Python/Python38 -32/pyyy/fu1.py \nfunction 1 \n#Type 2: with param",
"document_name": "Demo2.pdf",