Repository navigation
Expand file tree
/
Copy pathcodebook.html
More file actions
2041 lines (1797 loc) · 71.6 KB
/
Copy pathcodebook.html
File metadata and controls
2041 lines (1797 loc) · 71.6 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: 13px;
line-height: 1.48;
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: 9mm 11mm 9mm 11mm;
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: 5px;
margin-bottom: 6px;
display: flex;
justify-content: space-between;
font-size: 12px;
font-weight: 700;
}
.code-sheet {
flex: 1;
width: 100%;
background: #FFFFFF;
overflow: hidden;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.code-line {
display: flex;
white-space: pre;
font-size: 13px;
line-height: 1.46;
}
.ln {
width: 38px;
min-width: 38px;
color: var(--cmt-gray);
user-select: none;
text-align: right;
padding-right: 10px;
opacity: 0.75;
font-size: 11.8px;
font-weight: 500;
}
.lc {
flex: 1;
overflow: hidden;
}
.page-footer {
border-top: 1.5px solid #E5E7EB;
padding-top: 5px;
margin-top: 6px;
display: flex;
justify-content: space-between;
font-size: 11.5px;
color: var(--cmt-gray);
font-weight: 700;
}
/* 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 Display Print Hello World?
print("Hello World")
#WAP Display Student Name Roll Number and Branch?
print("Name: Lalan Kumar")
print("Roll No: 22")
print("Branch: Electrical Engineering")
print("Board: SBTE Bihar")
#WAP Show Display in Comment and type of comment?
print("Lalan Kumar")
#this is a single line comment
'''This is a Example of
Python Comment
This is the multiline comment'''
#WAP to differentiate between Procedure Oriented Programming (POP) and Object Oriented Programming (OOP) approach with example?
# 1. Procedure Oriented Approach (Step-by-step procedure using data variables)
length = 10
width = 5
area_pop = length * width
print("Area using POP Approach:", area_pop)
# 2. Object Oriented Approach (Using built-in String Object and its method)
branch_obj = "electrical engineering"
print("Upper case using OOP Object Method:", branch_obj.upper())
#WAP to take input from user and display Student Name and Branch?
name = input("Enter Student Name: ")
branch = input("Enter Branch Name: ")
print("Student Name:", name)
print("Branch Name:", branch)
#WAP to input two integer numbers from user and find their sum?
a = int(input("Enter first integer number: "))
b = int(input("Enter second integer number: "))
s = a + b
print("Sum of two numbers:", s)
#WAP to input two floating point numbers from user and find their product?
x = float(input("Enter first float number: "))
y = float(input("Enter second float number: "))
prod = x * y
print("Product of two numbers:", prod)
#WAP to Show Python Character Set Letters Digits and Special Symbols?
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
digits = "0123456789"
special_symbols = "+-*/%=<>!&|^~()[]{}:;,."
print("Alphabets:", letters)
print("Digits:", digits)
print("Special Symbols:", special_symbols)
#WAP to Find ASCII Value of a Character using ord() and Character from ASCII Value using chr()?
ch = input("Enter a single character: ")
print("ASCII value of", ch, "is:", ord(ch))
code = int(input("Enter an ASCII number (65-90): "))
print("Character for ASCII", code, "is:", chr(code))
#WAP to Demonstrate Python Escape Sequence Characters (\n, \t, \', \", \\)?
print("First Line\nSecond Line")
print("Roll\tName\tBranch")
print("101\tLalan\tEE")
print("Single Quote: \'Python\' and Double Quote: \"Python\"")
print("File Path: C:\\Users\\SBTE\\Python")
#WAP to 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 to 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 = 95
print(student_name)
print(Student_Name)
print(STUDENT_NAME)
print("Subject Code:", _subject_code, "Marks:", marks_1)
#WAP to Demonstrate String, Numeric, Boolean and Special None Literals?
str_literal = "SBTE Bihar"
int_literal = 1402025022
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 to Create Variables and Display Values with Data Types using type()?
roll_no = 25
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 to 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 to 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 to Use the Concept of Lvalue and Rvalue with Memory Address id()?
x = 25
y = 50
z = x + y
# Left side variable (x, y, z) is Lvalue (Locator / Memory Object)
# Right side expression (25, 50, x + y) is Rvalue (Read Value)
print("Rvalue of x:", x, "Lvalue Memory ID id(x):", id(x))
print("Rvalue of y:", y, "Lvalue Memory ID id(y):", id(y))
print("Rvalue of z:", z, "Lvalue Memory ID id(z):", id(z))
#WAP to 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:", dec_num, "Binary:", bin_num, "Octal:", oct_num, "Hexadecimal:", hex_num)
n = int(input("Enter any Decimal Integer to convert: "))
print("Binary:", bin(n), "Octal:", oct(n), "Hexadecimal:", hex(n))
#WAP to Show Floating Point Number Data Type and Exponential Notation?
price = 145.75
speed_of_light = 3.0e8
charge_of_electron = 1.6e-19
print("Float Price:", price, type(price))
print("Exponential 3.0e8:", speed_of_light)
print("Exponential 1.6e-19:", charge_of_electron)
#WAP to Show Complex Number Data Type, Real Part, Imaginary Part and Conjugate?
c1 = 4 + 5j
c2 = complex(2, -3)
print("Complex Number c1:", c1, type(c1))
print("Real Part:", c1.real, "Imaginary Part:", 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 to Demonstrate Boolean Data Type True and False?
a = 20
b = 10
print("20 > 10 is:", a > b, type(a > b))
print("20 < 10 is:", a < b, type(a < b))
print("Integer of True:", int(True), "Integer of False:", int(False))
#WAP to 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("String:", my_string, type(my_string))
print("List:", my_list, type(my_list))
print("Tuple:", my_tuple, type(my_tuple))
#WAP to Demonstrate None Type and Mapping Data Type (Dictionary) Overview?
val = None
print("Value:", val, "Type:", type(val))
student_dict = {"name": "Lalan Kumar", "roll": 101, "course": "Diploma EE"}
print("Dictionary:", student_dict, "Type:", type(student_dict))
print("Student Name:", student_dict["name"])
#WAP to Show Distinction Between Immutable and Mutable Data Types Using id()?
# Immutable Data Type (int, str, tuple): Memory address changes on modification
num = 10
print("Immutable int before change:", num, "ID:", id(num))
num = num + 5
print("Immutable int after change :", num, "ID:", id(num))
# Mutable Data Type (list, dict, set): Memory address remains same on modification
my_list = [10, 20, 30]
print("Mutable list before change :", my_list, "ID:", id(my_list))
my_list.append(40)
print("Mutable list after change :", my_list, "ID:", id(my_list))
#WAP to Input Two Numbers from User and Perform All Arithmetic Operators (+, -, *, /, //, %, **)?
a = int(input("Enter first number a: "))
b = int(input("Enter second number 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 to Perform All Relational Comparison Operators (==, !=, >, <, >=, <=)?
x = 25
y = 15
print("x =", x, "y =", y)
print("x == y :", x == y)
print("x != y :", x != y)
print("x > y :", x > y)
print("x < y :", x < y)
print("x >= y :", x >= y)
print("x <= y :", x <= y)
#WAP to 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 OR (a < b or b > c) :", a < b or b > c)
print("Logical NOT (not (a > b)) :", not (a > b))
#WAP to Demonstrate Assignment (=) and Augmented Assignment Operators (+=, -=, *=, /=, //=, %=, **=)?
n = int(input("Enter initial value of n: "))
n += 5
print("After n += 5 :", n)
n -= 3
print("After n -= 3 :", n)
n *= 4
print("After n *= 4 :", n)
n //= 2
print("After n //= 2 :", n)
n **= 2
print("After n **= 2 :", n)
n %= 7
print("After n %= 7 :", n)
#WAP to 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) and Membership Operators (in, not in)?
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)
course = "Python Programming"
print("'Python' in course :", "Python" in course)
print("'Java' not in course:", "Java" not in course)
#WAP to Show Difference Between Expressions and Statements and Evaluate Expression using eval()?
x = 15 # Assignment Statement
y = 10 # Assignment Statement
result = (x * 2) + (y * 3) # (x * 2) + (y * 3) is an Expression
print("Statement Result:", result)
expr = input("Enter any mathematical expression (e.g. 10 + 20 * 3): ")
print("Evaluated Result using eval():", eval(expr))
#WAP to Demonstrate Implicit Type Conversion and Explicit Type Casting?
# Implicit Type Conversion (Automatic by Python)
num_int = 125
num_flt = 12.50
sum_num = num_int + num_flt
print("Implicit Conversion Result:", sum_num, type(sum_num))
# Explicit Type Conversion (Type Casting by Programmer)
s = "250"
print("String to int :", int(s), type(int(s)))
print("String to float:", float(s), type(float(s)))
print("Tuple to list :", list((10, 20, 30)))
#WAP to Demonstrate Input and Output Mechanisms Using input(), print(), sep, end and f-string?
name = input("Enter Student Name: ")
roll = int(input("Enter Roll Number: "))
marks = float(input("Enter Marks Percentage: "))
print("SBTE", "Bihar", "Patna", sep=" - ")
print("Hello", end=" ")
print(name)
print(f"Name: {name}, Roll: {roll}, Marks: {marks:.2f}%")
#WAP to Show Operator Precedence and Associativity in Expression Evaluation?
exp1 = 10 + 20 * 3
exp2 = (10 + 20) * 3
exp3 = 2 ** 3 ** 2 # Right-to-Left associativity: 2 ** (3 ** 2) = 512
exp4 = (2 ** 3) ** 2 # Parentheses first: 8 ** 2 = 64
print("10 + 20 * 3 =", exp1)
print("(10 + 20) * 3 =", exp2)
print("2 ** 3 ** 2 =", exp3)
print("(2 ** 3) ** 2 =", exp4)
#WAP to Input Principal, Rate and Time from User and Calculate Simple Interest?
p = float(input("Enter Principal Amount: "))
r = float(input("Enter Rate of Interest: "))
t = float(input("Enter Time in Years: "))
si = (p * r * t) / 100
total = p + si
print("Simple Interest:", si)
print("Total Amount :", total)
#WAP to Input Radius of a Circle and Calculate Area and Circumference?
r = float(input("Enter Radius of Circle: "))
pi = 3.14159
area = pi * r * r
circumference = 2 * pi * r
print("Area of Circle :", round(area, 2))
print("Circumference of Circle:", round(circumference, 2))
#WAP to Input Temperature in Celsius and Convert it into Fahrenheit?
c = float(input("Enter Temperature in Celsius: "))
f = (c * 9 / 5) + 32
print("Temperature in Fahrenheit:", round(f, 2))
#WAP LAB 1(b): Calculate the Area of a Triangle where its Three Sides a, b, c are Given: s=(a+b+c)/2, Area=sqrt(s(s-a)(s-b)(s-c)) (Without Using Function)?
a = float(input("Enter first side a: "))
b = float(input("Enter second side b: "))
c = float(input("Enter third side c: "))
s = (a + b + c) / 2
area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
print("Semi-Perimeter (s):", s)
print("Area of Triangle :", round(area, 2))
#WAP LAB 1(c): Swap Two Variables Using a Third Temporary Variable?
a = int(input("Enter first number a: "))
b = int(input("Enter second number b: "))
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 : a =", a, "b =", b)
#WAP LAB 1(d): Solve Quadratic Equation ax^2 + bx + c = 0 for Real Numbers?
import math
a = float(input("Enter coefficient a (non-zero): "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
d = (b ** 2) - (4 * a * c)
print("Discriminant (d):", d)
if d >= 0:
root1 = (-b + math.sqrt(d)) / (2 * a)
root2 = (-b - math.sqrt(d)) / (2 * a)
print("Real Root 1:", round(root1, 2))
print("Real Root 2:", round(root2, 2))
else:
print("Discriminant is negative, no real roots exist.")
# ==============================================================================
# UNIT 2.0: CONDITIONAL AND ITERATIVE STATEMENTS
# ==============================================================================
#WAP to Check Voting Eligibility Using Simple if Statement?
age = int(input("Enter your age: "))
if age >= 18:
print("You are Eligible to Vote!")
print("Program Execution Completed.")
#WAP to Check Whether a Given Number is Even or Odd Using if-else Statement?
num = int(input("Enter an integer number: "))
if num % 2 == 0:
print(num, "is an EVEN Number.")
else:
print(num, "is an ODD Number.")
#WAP to Find Greater Between Two Numbers Using if-else Statement?
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print(a, "is greater than", b)
else:
print(b, "is greater than or equal to", a)
#WAP to Check Whether a Number is Divisible by Both 5 and 11 Using if-else?
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 Statement?
num = float(input("Enter any number: "))
if num > 0:
print(num, "is a POSITIVE Number.")
elif num < 0:
print(num, "is a NEGATIVE Number.")
else:
print(num, "is ZERO.")
#WAP to Input Marks of a Student and Display Grade Using if-elif-else Ladder?
marks = float(input("Enter Student Marks (0-100): "))
if marks >= 90:
print("Grade: A+ (Outstanding)")
elif marks >= 80:
print("Grade: A (Excellent)")
elif marks >= 70:
print("Grade: B (Very Good)")
elif marks >= 60:
print("Grade: C (Good)")
elif marks >= 40:
print("Grade: D (Pass)")
else:
print("Grade: F (Fail)")
#WAP to Input Day Number (1-7) and Display Day Name of the Week Using if-elif-else?
day_no = int(input("Enter Day Number (1 to 7): "))
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 to Find Largest Among Three Numbers Using Nested if-else Statement?
a = int(input("Enter first number a: "))
b = int(input("Enter second number b: "))
c = int(input("Enter third number c: "))
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 = int(input("Enter a year (e.g. 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 to Print Numbers from 1 to 10 Using while Loop?
i = 1
while i <= 10:
print(i, end=" ")
i = i + 1
print()
#WAP to Input N and Calculate Sum of First N Natural Numbers Using while Loop?
n = int(input("Enter value of N: "))
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 to Input a Number and Find the Sum of its Digits Using while Loop?
num = int(input("Enter any multi-digit number: "))
temp = num
digit_sum = 0
while temp > 0:
rem = temp % 10
digit_sum = digit_sum + rem
temp = temp // 10
print("Sum of Digits of", num, "is:", digit_sum)
#WAP to Input an Integer Number and Reverse it Using while Loop?
num = int(input("Enter a number to reverse: "))
temp = num
rev = 0
while temp > 0:
rem = temp % 10
rev = (rev * 10) + rem
temp = temp // 10
print("Reversed Number:", rev)
#WAP to Check Whether a Given Integer Number is Palindrome Using while Loop?
num = int(input("Enter a number to check Palindrome: "))
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 to Check Whether a Given Number is an Armstrong Number Using while Loop?
num = int(input("Enter a 3-digit number (e.g. 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 to 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 to Print All Even Numbers from 2 to 20 Using for Loop and range()?
for i in range(2, 21, 2):
print(i, end=" ")
print()
#WAP to Check Whether a Given Number is Prime or Not Using for Loop?
num = int(input("Enter a number to check Prime: "))
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?
lower = int(input("Enter lower interval limit: "))
upper = int(input("Enter upper interval limit: "))
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 = int(input("Enter a number for Multiplication Table: "))
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 = int(input("Enter number of Fibonacci terms: "))
a = 0
b = 1
print("Fibonacci Sequence:")
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 Loop?
num = int(input("Enter a number to find Factorial: "))
fact = 1
if num < 0:
print("Factorial does not exist for negative numbers.")
else:
for i in range(1, num + 1):
fact = fact * i
print("The Factorial of", num, "is:", fact)
#WAP to Demonstrate break, continue and pass Statements Inside a Loop?
for i in range(1, 10):
if i == 3:
pass # Null placeholder
if i == 5:
continue # Skips iteration when i is 5
if i == 8:
break # Terminates loop when i is 8
print(i, end=" ")
print()
#WAP to 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 to 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 to Print Inverted Star (*) Pattern and Pyramid Pattern Using Loops?
rows = 5
for i in range(rows, 0, -1):
for j in range(1, i + 1):
print("*", end=" ")
print()
for i in range(1, rows + 1):
print(" " * (rows - i) + "* " * i)
# ==============================================================================
# UNIT 3.0: STRING, LIST, TUPLES, SET AND DICTIONARY
# ==============================================================================
#WAP to Create a String and Demonstrate Positive and Negative Indexing?
s = "PYTHON"
print("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 to 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 to Perform String Slicing Operations [start : stop : step]?
text = "PROGRAMMING"
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 to Input a String and Traverse it Character by Character Using for and while Loop?
word = input("Enter a string to traverse: ")
print("Using for loop:")
for ch in word:
print(ch, end=" ")
print("\nUsing while loop:")
i = 0
while i < len(word):
print(word[i], end=" ")
i = i + 1
print()
#WAP to Demonstrate Built-in String Functions and Case Methods (len, upper, lower, title, capitalize, swapcase)?
s = "welcome to python programming"
print("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 to Demonstrate Built-in String Methods (strip, count, find, index, replace, 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 to 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 = input("Enter a string to check Palindrome: ")
rev_s = s[::-1]
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 = input("Enter a sentence to reverse its words: ")
words = s.split()
reversed_words = words[::-1]
result = " ".join(reversed_words)
print("Reversed Words String:", result)
#WAP LAB 3(c): Identify in a String the Name, Position and Counting of Vowels?
s = input("Enter a string to find vowels: ")
vowels = "aeiouAEIOU"
count = 0
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)
#WAP LAB 3(d): Count the Number of Matching Characters in a Pair of String (Set)?
str1 = input("Enter first string: ")
str2 = input("Enter second string: ")
matching_chars = set(str1).intersection(set(str2))
print("Matching Characters:", matching_chars)
print("Number of Matching Characters:", len(matching_chars))
#WAP LAB 3(e): Python Program for Removing i-th Character from a String?
s = input("Enter a string: ")
i = int(input("Enter index i to remove character: "))
new_s = s[:i] + s[i + 1:]
print("String after removing", i, "th character:", new_s)
#WAP to Create a List and Demonstrate Positive and Negative Indexing in List?
my_list = [10, 20, 30, 40, 50]
print("List:", my_list)
print("Positive Indexing -> my_list[0]:", my_list[0], "my_list[2]:", my_list[2])
print("Negative Indexing -> my_list[-1]:", my_list[-1], "my_list[-2]:", my_list[-2])
#WAP to Perform List Operations: Concatenation (+), Repetition (*), Membership (in), and Slicing?
list1 = [1, 2, 3]
list2 = [4, 5, 6]
print("Concatenation (list1 + list2):", list1 + list2)
print("Repetition (list1 * 3) :", list1 * 3)
print("Membership (2 in list1) :", 2 in list1)
nums = [10, 20, 30, 40, 50, 60]
print("Slicing nums[1:4] :", nums[1:4])
print("Reverse Slicing nums[::-1] :", nums[::-1])
#WAP to Input N Elements into a List from User and Traverse the List Using Loop?
n = int(input("How many elements to enter in list: "))
my_list = []
for i in range(n):
val = int(input("Enter element: "))
my_list.append(val)
print("Created List:", my_list)
print("Traversing List Elements:")
for item in my_list:
print(item, end=" ")
print()
#WAP to Demonstrate Built-in List Functions (len, max, min, sum, sorted) and Methods (append, insert, extend)?
nums = [40, 10, 50, 20, 30]
print("len:", len(nums), "max:", max(nums), "min:", min(nums), "sum:", sum(nums))
nums.append(60)
nums.insert(1, 15)
nums.extend([70, 80])
print("Updated List:", nums)
#WAP to Demonstrate List Methods (remove, pop, index, count, sort, reverse) and del Statement?
items = [10, 30, 20, 40, 20, 50]
print("count(20):", items.count(20), "index(40):", items.index(40))
items.remove(20)
popped = items.pop()
del items[0]
items.sort()
print("Sorted List:", items)
items.reverse()
print("Reversed List:", items)
#WAP to Perform Linear Search on a List of Numbers?
arr = [15, 42, 8, 23, 67, 91, 34]
key = int(input("Enter number to search in list [15, 42, 8, 23, 67, 91, 34]: "))
found = False
for i in range(len(arr)):
if arr[i] == key:
print("Element", key, "found at index:", i)
found = True
break
if not found:
print("Element", key, "not found in the list.")
#WAP to Count the Frequency of All Elements in a List?
my_list = [10, 20, 10, 30, 20, 10, 40, 30]
freq = {}
for item in my_list:
if item in freq:
freq[item] = freq[item] + 1
else:
freq[item] = 1
print("Given List:", my_list)
print("Frequency of Elements:", freq)
#WAP LAB 4(a): Find Largest Number in a Given List Without Using max()?