Repository navigation
Expand file tree
/
Copy pathcode-book.html
More file actions
2006 lines (1769 loc) · 68 KB
/
Copy pathcode-book.html
File metadata and controls
2006 lines (1769 loc) · 68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2418305_PYTHON_CODE_BOOK_A4</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,400;0,500;0,600;0,700;1,400&display=swap" rel="stylesheet">
<style>
:root {
--bg-white: #FFFFFF;
--text-main: #1E1E1E;
--kw-blue: #0070F3;
--fn-purple: #8B5CF6;
--str-green: #008F11;
--cmt-gray: #6B7280;
--term-green: #00A651;
--matrix-green: #008F11;
--cyber-blue: #0070F3;
--neon-purple: #8B5CF6;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
}
html, body {
background-color: #FFFFFF;
color: var(--text-main);
font-family: 'JetBrains Mono', monospace;
font-size: 11px;
line-height: 1.42;
width: 100%;
}
/* Floating Print Action Bar (Hidden in Print) */
#action-bar {
position: fixed;
top: 12px;
right: 16px;
z-index: 9999;
display: flex;
gap: 8px;
background: #FFFFFF;
border: 1.5px solid var(--cyber-blue);
padding: 6px 12px;
box-shadow: 0 4px 14px rgba(0, 112, 243, 0.12);
}
.action-btn {
background: #FFFFFF;
color: var(--cyber-blue);
border: 1px solid var(--cyber-blue);
font-family: 'JetBrains Mono', monospace;
font-size: 11px;
font-weight: 600;
padding: 5px 10px;
cursor: pointer;
}
.action-btn:hover {
background: var(--cyber-blue);
color: #FFFFFF;
}
.badge-stat {
color: var(--term-green);
font-weight: 700;
font-size: 11px;
display: flex;
align-items: center;
padding-right: 6px;
}
/* A4 Page Architecture */
#book-root {
width: 210mm;
margin: 0 auto;
background: #FFFFFF;
}
.a4-page {
width: 210mm;
height: 297mm;
background-color: #FFFFFF;
padding: 11mm 13mm 11mm 13mm;
page-break-after: always;
break-after: page;
page-break-inside: avoid;
break-inside: avoid;
position: relative;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-between;
border-bottom: 1px dashed #E5E7EB;
}
.page-header {
border-bottom: 1.5px solid var(--cyber-blue);
padding-bottom: 4px;
margin-bottom: 6px;
display: flex;
justify-content: space-between;
font-size: 10px;
font-weight: 600;
}
.code-sheet {
flex: 1;
width: 100%;
background: #FFFFFF;
overflow: hidden;
}
.code-line {
display: flex;
white-space: pre;
font-size: 10.6px;
line-height: 1.38;
}
.ln {
width: 34px;
min-width: 34px;
color: var(--cmt-gray);
user-select: none;
text-align: right;
padding-right: 10px;
opacity: 0.65;
font-size: 9.5px;
}
.lc {
flex: 1;
overflow: hidden;
}
.page-footer {
border-top: 1px solid #E5E7EB;
padding-top: 4px;
margin-top: 6px;
display: flex;
justify-content: space-between;
font-size: 9.5px;
color: var(--cmt-gray);
}
/* Syntax Highlighting Classes */
.kw { color: var(--kw-blue); font-weight: 600; }
.fn { color: var(--fn-purple); font-weight: 500; }
.st { color: var(--str-green); }
.cm { color: var(--cmt-gray); font-style: italic; }
.tm { color: var(--term-green); font-weight: 600; }
.tx { color: var(--text-main); }
@page {
size: A4 portrait;
margin: 0mm;
}
@media print {
#action-bar {
display: none !important;
}
html, body {
width: 210mm;
background: #FFFFFF !important;
}
#book-root {
width: 210mm;
margin: 0;
}
.a4-page {
border-bottom: none;
width: 210mm;
height: 297mm;
page-break-after: always;
break-after: page;
}
}
</style>
</head>
<body>
<div id="action-bar">
<span class="badge-stat" id="page-counter">PAGES: 36+ A4</span>
<button class="action-btn" onclick="window.print()">PRINT / SAVE PDF (A4)</button>
</div>
<div id="book-root"></div>
<!-- RAW PYTHON CODE CORPUS: SBTE BIHAR 2418305 (220+ #WAP PROGRAMS ACROSS UNITS 1 TO 6) -->
<script id="python-corpus" type="text/plain">
# ==============================================================================
# UNIT 1.0: FUNDAMENTALS OF PYTHON PROGRAMMING SYNTAX
# ==============================================================================
# WAP to display "Hello, World!" using Python.
print("Hello, World!")
# WAP to display student details using Python.
print("Name: Lalan Kumar")
print("Course: Diploma in Electrical Engineering")
print("Semester: 3rd")
print("Subject: Python Programming")
# WAP to demonstrate single-line and multi-line comments in Python.
# This is a single-line comment.
# Python ignores this line during execution.
"""
This is a multi-line comment example.
It contains multiple lines of text.
Python does not execute this text as a statement.
"""
print("Single-line and multi-line comments demonstrated.")
# WAP to declare and display variables.
name = "Gautam" # Name
age = 17 # Integer
price = 7.5 # Float
print(name)
print(age)
print(price)
# WAP to display the data types of variables.
a = 10 # Integer
b = 3.14 # Float
c = "Hello" # String
d = True # Boolean
print(type(a))
print(type(b))
print(type(c))
print(type(d))
# WAP to perform arithmetic operations.
a = 10 # First number
b = 5 # Second number
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
#WAP Display All Python Keywords Token Using Keyword Module?
import keyword
print("List of Python Keywords:")
print(keyword.kwlist)
print("Total Number of Keywords:", len(keyword.kwlist))
#WAP Show Valid Identifiers and Case Sensitivity in Python?
student_name = "Lalan Kumar"
Student_Name = "Gautam Kumar"
STUDENT_NAME = "Aman Kumar"
_subject_code = 2418305
marks_1 = 75
print(student_name)
print(Student_Name)
print(STUDENT_NAME)
print("Subject Code:", _subject_code, "Marks:", marks_1)
#WAP Demonstrate String Numeric Boolean and Special None Literals?
str_literal = "SBTE Bihar Patna"
int_literal = 2418305
float_literal = 98.75
bool_literal = True
none_literal = None
print("String Literal:", str_literal)
print("Integer Literal:", int_literal)
print("Float Literal:", float_literal)
print("Boolean Literal:", bool_literal)
print("Special Literal:", none_literal)
#WAP Create Variables and Display Values with Data Types?
roll_no = 22
student_name = "Lalan Kumar"
percentage = 77.6
is_pass = True
print(roll_no, type(roll_no))
print(student_name, type(student_name))
print(percentage, type(percentage))
print(is_pass, type(is_pass))
#WAP Demonstrate Dynamic Typing in Python Variables?
x = 500
print("Value of x:", x, "Type:", type(x))
x = 75.25
print("Value of x:", x, "Type:", type(x))
x = "Python Programming"
print("Value of x:", x, "Type:", type(x))
#WAP Assign Single Value to Multiple Variables and Multiple Values to Multiple Variables?
p = q = r = 100
print("Chained Assignment:", p, q, r)
a, b, c = 10, 20.5, "SBTE"
print("Multiple Assignment:", a, b, c)
#WAP Demonstrate Concept of Lvalue and Rvalue with Memory Address id()?
x = 25
y = 50
z = x + y
# Here x, y, z on Left Side are Lvalue (Memory Location)
# Here 25, 50, (x + y) on Right Side are Rvalue (Data Value)
print("Rvalue of x:", x, "Lvalue Memory Address id(x):", id(x))
print("Rvalue of y:", y, "Lvalue Memory Address id(y):", id(y))
print("Rvalue of z:", z, "Lvalue Memory Address id(z):", id(z))
#WAP Show Integer Number Data Type in Decimal Binary Octal and Hexadecimal Form?
dec_num = 100
bin_num = 0b1100100
oct_num = 0o144
hex_num = 0x64
print("Decimal Integer:", dec_num, type(dec_num))
print("Binary Integer (0b1100100):", bin_num)
print("Octal Integer (0o144):", oct_num)
print("Hexadecimal Integer (0x64):", hex_num)
#WAP Show Floating Point Number Data Type and Exponential Scientific Notation?
price = 145.75
pi_val = 3.14159
speed_of_light = 3.0e8
charge_of_electron = 1.6e-19
print("Float Price:", price, type(price))
print("Pi Value:", pi_val)
print("Exponential 3.0e8:", speed_of_light)
print("Exponential 1.6e-19:", charge_of_electron)
#WAP Show Complex Number Data Type Real Part Imaginary Part and Conjugate?
c1 = 4 + 5j
c2 = 2 - 3j
print("Complex Number c1:", c1, type(c1))
print("Real Part of c1:", c1.real)
print("Imaginary Part of c1:", c1.imag)
print("Conjugate of c1:", c1.conjugate())
print("Addition of c1 and c2:", c1 + c2)
print("Multiplication of c1 and c2:", c1 * c2)
#WAP Demonstrate Boolean Data Type True and False?
a = 20
b = 10
res1 = a > b
res2 = a < b
print("Result 1 (20 > 10):", res1, type(res1))
print("Result 2 (20 < 10):", res2, type(res2))
print("Integer value of True:", int(True))
print("Integer value of False:", int(False))
#WAP Demonstrate Sequence Data Types String List and Tuple Overview?
my_string = "Welcome to Python"
my_list = [10, 20, 30, 40, 50]
my_tuple = (100, 200, 300, 400)
print("Sequence String:", my_string, type(my_string))
print("Sequence List:", my_list, type(my_list))
print("Sequence Tuple:", my_tuple, type(my_tuple))
#WAP Demonstrate None Type Data Type in Python?
val = None
print("Value:", val)
print("Data Type of None:", type(val))
print("Check if val is None:", val is None)
#WAP Demonstrate Mapping Data Type Dictionary Overview?
student_dict = {"name": "Lalan Kumar", "roll": 101, "course": "Diploma EE", "code": 2418305}
print("Dictionary Data:", student_dict)
print("Type of Dictionary:", type(student_dict))
print("Student Name:", student_dict["name"])
print("Subject Code:", student_dict["code"])
#WAP Show Distinction Between Immutable Data Types (int, str, tuple) Using id()?
num = 10
print("Before change num:", num, "Memory ID:", id(num))
num = 20
print("After change num:", num, "Memory ID:", id(num))
text = "Hello"
print("Before change text:", text, "Memory ID:", id(text))
text = text + " World"
print("After change text:", text, "Memory ID:", id(text))
#WAP Show Distinction Between Mutable Data Types (list, dict, set) Using id()?
my_list = [10, 20, 30]
print("Before change list:", my_list, "Memory ID:", id(my_list))
my_list[0] = 99
my_list.append(40)
print("After change list:", my_list, "Memory ID:", id(my_list))
my_dict = {"a": 1, "b": 2}
print("Before change dict:", my_dict, "Memory ID:", id(my_dict))
my_dict["a"] = 100
print("After change dict:", my_dict, "Memory ID:", id(my_dict))
#WAP Perform All Arithmetic Operators (+, -, *, /, //, %, **) in Python?
a = 17
b = 5
print("a =", a, "b =", b)
print("Addition (a + b):", a + b)
print("Subtraction (a - b):", a - b)
print("Multiplication (a * b):", a * b)
print("Division (a / b):", a / b)
print("Floor Division (a // b):", a // b)
print("Modulus Remainder (a % b):", a % b)
print("Exponent Power (a ** b):", a ** b)
#WAP Perform All Relational Comparison Operators ( =, =, >, <, =, =)?
x = 25
y = 15
print("x =", x, "y =", y)
print("x == y (Equal to):", x == y)
print("x != y (Not Equal to):", x != y)
print("x > y (Greater than):", x > y)
print("x < y (Less than):", x < y)
print("x >= y (Greater than or Equal to):", x >= y)
print("x <= y (Less than or Equal to):", x <= y)
#WAP Perform All Logical Operators (and, or, not) in Python?
a = 10
b = 20
c = 30
print("Logical AND (a < b and b < c):", a < b and b < c)
print("Logical AND (a < b and b > c):", a < b and b > c)
print("Logical OR (a < b or b > c):", a < b or b > c)
print("Logical OR (a > b or b > c):", a > b or b > c)
print("Logical NOT (not (a > b)):", not (a > b))
#WAP Demonstrate Assignment (=) and Augmented Assignment Operators (+=, -=, *=, /=, //=, %=, **=)?
n = 10
print("Initial Assignment (=):", n)
n += 5
print("Add AND (+= 5):", n)
n -= 3
print("Subtract AND (-= 3):", n)
n *= 4
print("Multiply AND (*= 4):", n)
n /= 2
print("Divide AND (/= 2):", n)
n //= 3
print("Floor Divide AND (//= 3):", n)
n **= 2
print("Exponent AND (**= 2):", n)
n %= 7
print("Modulus AND (%= 7):", n)
#WAP Perform Bitwise Operators (&, |, ^, ~, <<, >>) in Python?
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print("Bitwise AND (a & b):", a & b)
print("Bitwise OR (a | b):", a | b)
print("Bitwise XOR (a ^ b):", a ^ b)
print("Bitwise NOT (~a):", ~a)
print("Bitwise Left Shift (a << 2):", a << 2)
print("Bitwise Right Shift (a >> 1):", a >> 1)
# WAP to Demonstrate Identity Operators (is, is not) in Python
x = 100
y = 100
list1 = [1, 2, 3]
list2 = [1, 2, 3]
print("x is y:", x is y)
print("list1 is list2:", list1 is list2)
print("list1 is not list2:", list1 is not list2)
print("list1 == list2 (Value equality):", list1 == list2)
#WAP Demonstrate Membership Operators (in, not in) in Python?
course = "Python Programming"
numbers = [10, 20, 30, 40, 50]
print("'Python' in course:", "Python" in course)
print("'Java' not in course:", "Java" not in course)
print("30 in numbers:", 30 in numbers)
print("99 in numbers:", 99 in numbers)
#WAP Show Difference Between Expressions and Statements in Python?
# Expression: Combination of values, variables and operators that evaluates to a value
# Statement: Complete instruction that Python interpreter can execute
x = 15 # Assignment Statement
y = 10 # Assignment Statement
result = (x * 2) + (y * 3) # (x * 2) + (y * 3) is an Expression; whole line is a Statement
print("Evaluated Expression Result:", result) # Print Statement
#WAP Demonstrate Implicit Type Conversion (Automatic Type Promotion)?
num_int = 125
num_flt = 12.50
sum_num = num_int + num_flt
print("Type of num_int:", type(num_int))
print("Type of num_flt:", type(num_flt))
print("Value of sum_num:", sum_num)
print("Implicitly Converted Type of sum_num:", type(sum_num))
#WAP Demonstrate Explicit Type Conversion (Type Casting int, float, str, list, tuple)?
s = "250"
n_int = int(s)
n_flt = float(s)
num = 99
s_num = str(num)
my_tuple = (1, 2, 3)
my_list = list(my_tuple)
print("String to Integer:", n_int, type(n_int))
print("String to Float:", n_flt, type(n_flt))
print("Integer to String:", s_num, type(s_num))
print("Tuple to List:", my_list, type(my_list))
#WAP Demonstrate Input and Output Mechanisms Using print() sep end and Formatting?
name = "Lalan Kumar"
roll = 22
marks = 92.456
print("Welcome", "to", "SBTE", "Bihar", sep=" - ")
print("Hello", end=" ")
print("World!")
print(f"Student: {name}, Roll: {roll}, Marks: {marks:.2f}")
print("Student: {}, Roll: {}, Marks: {:.2f}".format(name, roll, marks))
#WAP Show Operator Precedence and Associativity Expression Evaluation?
# PEMDAS Rule: Parentheses -> Exponent -> Multiply/Divide -> Add/Subtract
exp1 = 10 + 20 * 3
exp2 = (10 + 20) * 3
exp3 = 2 ** 3 ** 2 # Right-to-Left associativity: 2 ** (3 ** 2) = 2 ** 9 = 512
exp4 = (2 ** 3) ** 2 # Parentheses first: 8 ** 2 = 64
exp5 = 100 / 10 * 2 # Left-to-Right associativity: (100 / 10) * 2 = 20.0
print("10 + 20 * 3 =", exp1)
print("(10 + 20) * 3 =", exp2)
print("2 ** 3 ** 2 =", exp3)
print("(2 ** 3) ** 2 =", exp4)
print("100 / 10 * 2 =", exp5)
#WAP Calculate Simple Interest and Total Amount?
principal = 5000
rate = 8.5
time = 3
simple_interest = (principal * rate * time) / 100
total_amount = principal + simple_interest
print("Principal Amount:", principal)
print("Rate of Interest:", rate)
print("Time (in Years):", time)
print("Simple Interest:", simple_interest)
print("Total Amount:", total_amount)
#WAP Calculate Area and Circumference of a Circle?
radius = 7.0
pi = 3.14159
area = pi * radius * radius
circumference = 2 * pi * radius
print("Radius of Circle:", radius)
print("Area of Circle:", round(area, 2))
print("Circumference of Circle:", round(circumference, 2))
#WAP Convert Temperature from Celsius to Fahrenheit and Fahrenheit to Celsius?
celsius = 37.0
fahrenheit = (celsius * 9 / 5) + 32
print(celsius, "Degree Celsius =", fahrenheit, "Degree Fahrenheit")
f_temp = 98.6
c_temp = (f_temp - 32) * 5 / 9
print(f_temp, "Degree Fahrenheit =", c_temp, "Degree Celsius")
#WAP LAB 1(b): Calculate the Area of a Triangle where its Three Sides a, b, c are Given?
a = 5.0
b = 6.0
c = 7.0
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print("Side a:", a, "Side b:", b, "Side c:", c)
print("Semi-Perimeter (s):", s)
print("Area of Triangle:", round(area, 2))
#WAP LAB 1(c): Swap Two Variables Using Third Temporary Variable?
a = 10
b = 20
print("Before Swapping: a =", a, "b =", b)
temp = a
a = b
b = temp
print("After Swapping: a =", a, "b =", b)
#WAP LAB 1(c): Swap Two Variables Without Using Third Variable?
a = 50
b = 100
print("Before Swapping: a =", a, "b =", b)
a, b = b, a
print("After Swapping (Tuple Unpacking): a =", a, "b =", b)
x = 15
y = 25
x = x + y
y = x - y
x = x - y
print("After Swapping (Arithmetic + -): x =", x, "y =", y)
#WAP LAB 1(d): Solve Quadratic Equation ax^2 + bx + c = 0 for Real Numbers?
import math
a = 1.0
b = -5.0
c = 6.0
d = (b ** 2) - (4 * a * c)
print("Quadratic Equation: a =", a, "b =", b, "c =", c)
print("Discriminant (d):", d)
root1 = (-b + math.sqrt(d)) / (2 * a)
root2 = (-b - math.sqrt(d)) / (2 * a)
print("Root 1:", root1)
print("Root 2:", root2)
# ==============================================================================
# UNIT 2.0: CONDITIONAL AND ITERATIVE STATEMENTS
# ==============================================================================
#WAP Demonstrate Simple if Statement to Check Voting Eligibility?
age = 20
if age >= 18:
print("Age is:", age)
print("You are Eligible to Vote!")
print("Program Execution Completed.")
#WAP Demonstrate Simple if Statement to Check Passing Marks?
marks = 65
if marks >= 40:
print("Marks Obtained:", marks)
print("Congratulations! You Passed the Examination.")
#WAP Demonstrate if-else Statement to Check Whether a Number is Even or Odd?
num = 28
if num % 2 == 0:
print(num, "is an EVEN Number.")
else:
print(num, "is an ODD Number.")
#WAP Demonstrate if-else Statement to Find Greater Between Two Numbers?
a = 45
b = 72
if a > b:
print(a, "is greater than", b)
else:
print(b, "is greater than", a)
#WAP Demonstrate if-else Statement to Check Divisibility by 5 and 11?
num = 55
if num % 5 == 0 and num % 11 == 0:
print(num, "is divisible by both 5 and 11.")
else:
print(num, "is NOT divisible by both 5 and 11.")
#WAP LAB 2(a): Check if a Number is Positive, Negative or Zero Using if-elif-else?
num = -15
if num > 0:
print(num, "is a POSITIVE Number.")
elif num < 0:
print(num, "is a NEGATIVE Number.")
else:
print(num, "is ZERO.")
#WAP Demonstrate if-elif-else Statement to Display Student Grade Based on Marks?
marks = 84
if marks >= 90:
print("Marks:", marks, "-> Grade: A+ (Outstanding)")
elif marks >= 80:
print("Marks:", marks, "-> Grade: A (Excellent)")
elif marks >= 70:
print("Marks:", marks, "-> Grade: B (Very Good)")
elif marks >= 60:
print("Marks:", marks, "-> Grade: C (Good)")
elif marks >= 40:
print("Marks:", marks, "-> Grade: D (Pass)")
else:
print("Marks:", marks, "-> Grade: F (Fail)")
#WAP Display Day Name of the Week Using if-elif-else Ladder?
day_no = 3
if day_no == 1:
print("Monday")
elif day_no == 2:
print("Tuesday")
elif day_no == 3:
print("Wednesday")
elif day_no == 4:
print("Thursday")
elif day_no == 5:
print("Friday")
elif day_no == 6:
print("Saturday")
elif day_no == 7:
print("Sunday")
else:
print("Invalid Day Number!")
#WAP Calculate Electricity Bill Based on Units Consumed Using if-elif-else?
units = 250
if units <= 100:
bill = units * 4.0
elif units <= 200:
bill = (100 * 4.0) + ((units - 100) * 6.0)
else:
bill = (100 * 4.0) + (100 * 6.0) + ((units - 200) * 8.0)
print("Total Electricity Units:", units)
print("Total Bill Amount: Rs.", bill)
#WAP Find Largest Among Three Numbers Using Nested if-else Statement?
a = 40
b = 95
c = 70
if a >= b:
if a >= c:
print(a, "is the Largest Number.")
else:
print(c, "is the Largest Number.")
else:
if b >= c:
print(b, "is the Largest Number.")
else:
print(c, "is the Largest Number.")
#WAP LAB 2(b): Check Whether the Given Year is a Leap Year?
year = 2024
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print(year, "is a LEAP YEAR.")
else:
print(year, "is NOT a Leap Year.")
#WAP Print Numbers from 1 to 10 Using while Loop?
i = 1
while i <= 10:
print(i, end=" ")
i = i + 1
print()
#WAP Calculate Sum of First N Natural Numbers Using while Loop?
n = 10
i = 1
total_sum = 0
while i <= n:
total_sum = total_sum + i
i = i + 1
print("Sum of first", n, "natural numbers is:", total_sum)
#WAP Find Sum of Digits of a Given Number Using while Loop?
num = 4567
temp = num
digit_sum = 0
while temp > 0:
rem = temp % 10
digit_sum = digit_sum + rem
temp = temp // 10
print("Given Number:", num)
print("Sum of Digits:", digit_sum)
#WAP Reverse a Given Integer Number Using while Loop?
num = 12345
temp = num
rev = 0
while temp > 0:
rem = temp % 10
rev = (rev * 10) + rem
temp = temp // 10
print("Original Number:", num)
print("Reversed Number:", rev)
#WAP Check Whether a Given Integer Number is Palindrome Using while Loop?
num = 121
temp = num
rev = 0
while temp > 0:
rem = temp % 10
rev = (rev * 10) + rem
temp = temp // 10
if num == rev:
print(num, "is a PALINDROME Number.")
else:
print(num, "is NOT a Palindrome Number.")
#WAP Check Whether a Given Number is an Armstrong Number Using while Loop?
num = 153
temp = num
sum_cubes = 0
while temp > 0:
digit = temp % 10
sum_cubes = sum_cubes + (digit ** 3)
temp = temp // 10
if num == sum_cubes:
print(num, "is an ARMSTRONG Number.")
else:
print(num, "is NOT an Armstrong Number.")
#WAP Demonstrate range() Function with Stop, Start-Stop, and Start-Stop-Step?
print("range(6):", list(range(6)))
print("range(1, 10):", list(range(1, 10)))
print("range(2, 21, 2):", list(range(2, 21, 2)))
print("range(10, 0, -1):", list(range(10, 0, -1)))
#WAP Print Even Numbers from 2 to 20 Using for Loop and range()?
print("Even Numbers from 2 to 20:")
for i in range(2, 21, 2):
print(i, end=" ")
print()
#WAP Calculate Sum of Odd Numbers from 1 to 20 Using for Loop?
odd_sum = 0
for i in range(1, 21, 2):
odd_sum = odd_sum + i
print("Sum of Odd Numbers from 1 to 20:", odd_sum)
#WAP Check Whether a Single Number is Prime or Not Using for Loop?
num = 29
is_prime = True
if num <= 1:
is_prime = False
else:
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is a PRIME Number.")
else:
print(num, "is NOT a Prime Number.")
#WAP LAB 2(c): Print All Prime Numbers in an Interval (Between 10 and 50)?
lower = 10
upper = 50
print("Prime Numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")
print()
#WAP LAB 2(d): Display the Multiplication Table Based on the Given Input?
num = 7
print("Multiplication Table of", num)
for i in range(1, 11):
print(num, "x", i, "=", num * i)
#WAP LAB 2(e): Print the Fibonacci Sequence Up to N Terms?
n_terms = 10
a = 0
b = 1
print("Fibonacci Sequence up to", n_terms, "terms:")
for i in range(n_terms):
print(a, end=" ")
c = a + b
a = b
b = c
print()
#WAP LAB 2(f): Find the Factorial of a Given Number Using for Loop?
num = 5
fact = 1
if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0:
print("The Factorial of 0 is 1.")
else:
for i in range(1, num + 1):
fact = fact * i
print("The Factorial of", num, "is:", fact)
#WAP Demonstrate break Statement Inside a Loop?
for i in range(1, 11):
if i == 6:
print("Breaking the loop at i =", i)
break
print("Value of i:", i)
#WAP Demonstrate continue Statement Inside a Loop?
for i in range(1, 8):
if i == 4:
print("Skipping i = 4 using continue")
continue
print("Value of i:", i)
#WAP Demonstrate pass Statement Inside a Loop?
for i in range(1, 6):
if i == 3:
pass # Null operation placeholder
print("Number:", i)
#WAP Print Right-Angled Star (*) Triangle Pattern Using Nested Loops?
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end=" ")
print()
#WAP Print Number Triangle Pattern (1, 1 2, 1 2 3...) Using Nested Loops?
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
#WAP Print Inverted Star (*) Pattern Using Nested Loops?
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print("*", end=" ")
print()
#WAP Print Pyramid Star (*) Pattern Using Nested Loops?
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "* " * i)
# ==============================================================================
# UNIT 3.0: STRING, LIST, TUPLES, SET AND DICTIONARY
# ==============================================================================
#WAP Create a String and Demonstrate Positive and Negative Indexing?
s = "PYTHON"
print("Complete String:", s)
print("Positive Indexing -> s[0]:", s[0], "s[1]:", s[1], "s[5]:", s[5])
print("Negative Indexing -> s[-1]:", s[-1], "s[-2]:", s[-2], "s[-6]:", s[-6])
#WAP Perform String Operations: Concatenation (+), Repetition (*), and Membership (in, not in)?
s1 = "Hello"
s2 = "Python"
print("Concatenation (s1 + ' ' + s2):", s1 + " " + s2)
print("Repetition (s1 * 3):", s1 * 3)
print("Membership ('Py' in s2):", "Py" in s2)
print("Membership ('z' not in s1):", "z" not in s1)
#WAP Perform String Slicing Operations [start : stop : step]?
text = "PROGRAMMING"
print("Original String:", text)
print("text[0:7] :", text[0:7])
print("text[3:8] :", text[3:8])
print("text[:4] :", text[:4])
print("text[4:] :", text[4:])
print("text[::2] :", text[::2])
print("text[::-1] :", text[::-1])
#WAP Traverse a String Character by Character Using for Loop and while Loop?
word = "SBTE"
print("Traversing using for loop:")
for ch in word:
print(ch, end=" ")
print("\nTraversing using while loop:")
i = 0
while i < len(word):
print(word[i], end=" ")
i = i + 1
print()
#WAP Demonstrate Built-in String Functions and Case Methods (len, upper, lower, title, capitalize, swapcase)?
s = "welcome to python programming"
print("Length len(s):", len(s))
print("upper() :", s.upper())
print("lower() :", s.lower())
print("title() :", s.title())
print("capitalize() :", s.capitalize())
print("swapcase() :", "PyThOn".swapcase())
#WAP Demonstrate Built-in String Methods (count, find, index, replace, strip, split, join)?
s = " python is easy and python is fast "
clean_s = s.strip()
print("strip() :", clean_s)
print("count('python') :", clean_s.count("python"))
print("find('easy') :", clean_s.find("easy"))
print("replace() :", clean_s.replace("easy", "powerful"))
words = clean_s.split(" ")
print("split(' ') :", words)
print("'-'.join(words) :", "-".join(words))
#WAP Demonstrate String Checking Methods (isalpha, isdigit, isalnum, isupper, islower, isspace)?
print("'Python'.isalpha() :", "Python".isalpha())
print("'12345'.isdigit() :", "12345".isdigit())
print("'Python3'.isalnum() :", "Python3".isalnum())
print("'SBTE'.isupper() :", "SBTE".isupper())
print("'sbte'.islower() :", "sbte".islower())
print("' '.isspace() :", " ".isspace())
#WAP LAB 3(a): Check Whether the Given String is Palindrome?
s = "madam"
rev_s = s[::-1]
print("Original String:", s)
print("Reversed String:", rev_s)
if s == rev_s:
print(s, "is a PALINDROME String.")
else:
print(s, "is NOT a Palindrome String.")
#WAP LAB 3(b): Reverse Words in a Given String in Python?
s = "Diploma in Electrical Engineering"
words = s.split()
reversed_words = words[::-1]
result = " ".join(reversed_words)
print("Original String:", s)
print("Reversed Words :", result)
#WAP LAB 3(c): Identify in a String the Name, Position and Counting of Vowels?
s = "Python Programming"
vowels = "aeiouAEIOU"
count = 0
print("Given String:", s)
for i in range(len(s)):
if s[i] in vowels:
print("Vowel Name:", s[i], "-> Position (Index):", i)
count = count + 1
print("Total Count of Vowels:", count)