-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrial.py
747 lines (614 loc) · 16.1 KB
/
trial.py
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
# Comments
# Hello World
print("Hello World!!")
# Simple math operations
a = 1
b = 2
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
print(a // b)
# Function
def func1():
number = 0
number += 1
print('Name' + str(number))
func1()
func1()
func1()
print("Function called 3 times.")
# Length of string
message = "hello world"
print(len(message))
print(message[2:5])
# Lowercase - Uppercase
print(message.lower()) # Convert string to lowercase
print(message.upper()) # Convert string to uppercase
print(message.count('l')) # Count occurrences of 'l'
print(message.find('world')) # Find index of 'world'
print(message.find('hero')) # Find returns -1 if not found
print(message.replace('world', 'Meet'))
name = 'Meet'
greetings = 'Hello'
wel_message = f'{greetings}, {name}. Welcome!'
print(wel_message)
# Type of variables
num = 3
print(type(num))
num1 = 2.34
print(type(num1))
# Arithmetic operations
m1 = 5
m2 = 10
print(m1 + m2) # Addition
print(m1 - m2) # Subtraction
print(m1 * m2) # Multiplication
print(m1 / m2) # Division
print(m1 // m2) # Floor division
print(m1 ** m2) # Exponent
print(m1 % m2) # Modulo
# Absolute value
print(abs(-3))
print(abs(10.4))
# Round of the value
print(round(15.5))
print(round(15484.5486616645, 2)) # Round to 2 decimal places
# Comparisons
val1 = 10
val2 = 20
print(val1 > val2) # Greater than
print(val1 < val2) # Less than
print(val1 == val2) # Equal
print(val1 != val2) # Not equal
print(val1 >= val2) # Greater than or equal to
print(val1 <= val2) # Less than or equal to
# String addition
num_1 = '1000'
num_2 = '2000'
print(num_1 + num_2)
# Type casting
# String to integer
num_1 = int(num_1)
num_2 = int(num_2)
print(num_1 + num_2)
# List
course = ['CSE', 'CE', 'IT', 'ECE', 'ICT']
print(course) # Print the whole list
print(course[2]) # Access the 2nd element
print(len(course)) # Length of the list
print(course[-1]) # Last element
print(course[0:2]) # Slice elements
print(course[2:]) # Elements from index 2
print(course[:4]) # Elements till index 4
print(course[::-1]) # Reverse the list
course.append('ME') # Append 'ME' to the list
print(course)
course.remove('IT') # Remove 'IT' from the list
print(course)
course.insert(2, 'EE') # Insert 'EE' at index 2
print(course)
course.pop() # Delete the last element
print(course)
course.reverse() # Reverse the list
print(course)
course_no = [10, 2, 3, 4, 5]
course.insert(0, course_no) # Insert course_no list into course at index 0
print(course)
# Merge two lists without using inbuilt functions
print(course + course_no)
# Merge two lists using inbuilt function
course.extend(course_no)
print(course)
# Sort the list elements
course_no.sort()
print(course_no)
print(sum(course_no))
print(max(course_no))
print(course.index('CSE'))
print('Arts' in course) # Check if 'Arts' is in the list
print('CSE' in course)
# Loop in Python
# For loop to print all elements in the list
for item in course_no:
print(item)
# Enumerate to get index and value
for item, course_no in enumerate(course_no):
print(item, course_no)
# Assigning index starting value to iterate over a list
for item, course in enumerate([546, 7864, 546, 543], start=20):
print(item, course)
# Join function to join list elements into a string
cour = ['a', 'b', 'c', 'd']
course_str = ','.join(cour)
print(course_str)
# Tuple
# Mutable
list_1 = ['history', 'Math', 'Physics', 'Chemistry']
list_2 = list_1
print(list_1)
print(list_2)
list_1[0] = 'Art'
print(list_1)
print(list_2)
# Immutable
tuple_1 = ('history', 'Math', 'Physics', 'Chemistry')
tuple_2 = tuple_1
print(tuple_1)
print(tuple_2)
# Sets in Python
cs_course = {'DSA', 'OOPS', 'COA', 'DBMS'}
bca_course = {'WEBD', 'OOPS', 'DBMS', 'SDE'}
print(cs_course)
print('COA' in cs_course) # Check if 'COA' is in cs_course
print(cs_course.intersection(bca_course)) # Intersection of cs_course and bca_course
print(cs_course.difference(bca_course)) # Difference of cs_course and bca_course
print(cs_course.union(bca_course)) # Union of cs_course and bca_course
# Empty Lists
empty_list = []
empty_list1 = list()
print(empty_list)
print(empty_list1)
# Empty Tuple
empty_tuple = ()
empty_tuple1 = tuple()
print(empty_tuple)
print(empty_tuple1)
# Empty Sets
empty_sets = set()
print(empty_sets)
# Dictionary
Student = {'name': 'Meet', 'age': '20', 'Courses': ['Math', 'CSE', 'Robotics']}
print(Student) # Print the whole dictionary
print(Student['name']) # Access 'name'
print(Student.get('name')) # Get 'name'
print(Student.get('phone', 'Not Found')) # Get 'phone' or 'Not Found'
Student['phone'] = '123456789' # Update 'phone'
print(Student.get('phone', 'Not Found'))
Student.update({'age': '21'}) # Update 'age'
print(Student)
del Student['age'] # Delete 'age'
print(Student)
print(Student.keys()) # Print keys
print(Student.values()) # Print values
print(Student.items()) # Print items
for key in Student: # Iterate over keys
print(key)
for key, value in Student.items(): # Iterate over items
print(key, value)
# Conditionals and Booleans
if True:
print("True Statement")
if False:
print("False Statement")
a = 1000
b = 2000
c = a % b
if c == 0:
print("Number is rational")
else:
print("Number is not rational")
# Comparisons:
# Equal : ==
# Not Equal : !=
# Greater Than : >
# Less Than : <
# Greater or Equal : >=
# Less or Equals : <=
# Object Identity : is
Language = ['Python', 'Java']
if 'Python' in Language:
print('Language is Python')
elif 'Java' in Language:
print('Language is Java')
else:
print('No Match Found')
user = 'Admin'
logged_in = True
# not method
if not logged_in:
print("Please Log in")
else:
print("Welcome")
# or method
if user == 'Admin' or logged_in:
print("Admin Page")
else:
print("Bad Codes")
# and method
if user == 'Admin' and logged_in:
print("Admin Page")
else:
print("Bad Codes")
a = [1, 2, 3]
b = a
print(id(a)) # Memory address of 'a'
print(id(b)) # Memory address of 'b'
print(id(a) == id(b))
a = [1, 2, 3, 4, 5]
# break to terminate loop
for num in a:
if num == 3:
print("Number found!")
break
print(num)
# continue to move the loop
for num in a:
if num == 3:
print("Number found!")
continue
print(num)
# This code snippet assigns a value to a number and prints it until the final number is printed, like 5 d in this code
for num in a:
for letter in 'abcd':
print(num, letter)
for i in range(10):
print(i)
for i in range(5, 10):
i += 1
print(i)
# While loop to print x values
x = 0
while x < 10:
print(x)
x += 2
# Recursion function
def hello_func(n):
if n > 0:
print("hello world")
hello_func(n - 1)
hello_func(5)
# Custom function
def hello_function(greeting):
return '{} Function.'.format(greeting)
print((hello_function('i\'m a test')))
def sample():
return 'hello Function.'
print(sample().upper())
def student_info(*args, **kwargs):
print(args)
print(kwargs)
student_info('Math', 'Arts', name='Meet', age='20')
# Exception Handling
try:
result = 10 / 0 # ZeroDivisionError
except ZeroDivisionError as e:
print(f"Error: {e}")
else:
print("Division successful!")
finally:
print("Execution complete.")
# Handling specific exceptions
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
print(f"Your number is: {num}")
finally:
print("Input handling complete.")
# File Operations
# Writing to a file
with open('sample.txt', 'w') as file:
file.write("Hello, this is a sample file.\n")
file.write("Writing some more lines.")
# Reading from a file
with open('sample.txt', 'r') as file:
contents = file.read()
print(contents)
# Appending to a file
with open('sample.txt', 'a') as file:
file.write("\nAppending a new line to the file.")
# Reading line by line
with open('sample.txt', 'r') as file:
lines = file.readlines()
for line in lines:
print(line.strip()) # Remove any extra newline characters
# Handling file not found error
try:
with open('nonexistent_file.txt', 'r') as file:
content = file.read()
except FileNotFoundError:
print("File not found!")
# Additional Concepts
# Classes and Object-Oriented Programming (OOP)
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print(f"{self.name} makes a sound.")
class Dog(Animal):
def speak(self):
print(f"{self.name} barks.")
dog = Dog("Buddy")
dog.speak()
# Lambda Functions
addition = lambda x, y: x + y
print(addition(2, 3))
# Generators and Iterators
def generator_example():
for i in range(5):
yield i
gen = generator_example()
for value in gen:
print(value)
# Decorators
def decorator_func(original_func):
def wrapper_func():
print("Wrapper executed this before {}".format(original_func.__name__))
return original_func()
return wrapper_func
@decorator_func
def display():
print("Display function executed.")
display()
# Modules and Packages
import math
print(math.sqrt(16))
# Concurrency and Threading
import threading
def print_numbers():
for i in range(1, 6):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
# Assertions
x = 10
y = 0
assert y != 0, "Divide by zero error"
print(x / y)
# Context Managers
class FileHandler:
def __enter__(self):
print("Entering context...")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting context...")
with FileHandler() as fh:
print("Inside the context.")
# Regular Expressions
import re
pattern = r'\b\d{3}\b'
text = "123 4567 890 12345"
matches = re.findall(pattern, text)
print(matches)
# Functional Programming (map, filter, reduce)
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
from functools import reduce
product = reduce(lambda x, y: x * y, numbers)
print(squared)
print(evens)
print(product)
# Data Structures (collections module)
from collections import deque, defaultdict, namedtuple
# Example usage of deque, defaultdict, namedtuple
# Concepts not covered in the previous code snippet
# 1. List comprehensions
numbers = [1, 2, 3, 4, 5]
squared = [x ** 2 for x in numbers]
evens = [x for x in numbers if x % 2 == 0]
print(squared)
print(evens)
# 2. Dictionary comprehensions
numbers_dict = {x: x ** 2 for x in numbers}
print(numbers_dict)
# 3. Set comprehensions
numbers_set = {x for x in numbers}
print(numbers_set)
# 4. File handling (Using 'with' statement)
with open('sample.txt', 'r') as file:
lines = file.readlines()
for line in lines:
print(line.strip())
# 5. Error handling with try-except-else-finally block
try:
result = 10 / 0 # ZeroDivisionError
except ZeroDivisionError as e:
print(f"Error: {e}")
else:
print("Division successful!")
finally:
print("Execution complete.")
# 6. Handling specific exceptions
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
print(f"Your number is: {num}")
finally:
print("Input handling complete.")
# 7. Lambda functions
addition = lambda x, y: x + y
print(addition(2, 3))
# 8. Generators and Iterators
def generator_example():
for i in range(5):
yield i
gen = generator_example()
for value in gen:
print(value)
# 9. Decorators
def decorator_func(original_func):
def wrapper_func():
print("Wrapper executed this before {}".format(original_func.__name__))
return original_func()
return wrapper_func
@decorator_func
def display():
print("Display function executed.")
display()
# 10. Modules and Packages
import math
print(math.sqrt(16))
# 11. Concurrency and Threading
import threading
def print_numbers():
for i in range(1, 6):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
# 12. Assertions
x = 10
y = 0
assert y != 0, "Divide by zero error"
print(x / y)
# 13. Context Managers
class FileHandler:
def __enter__(self):
print("Entering context...")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting context...")
with FileHandler() as fh:
print("Inside the context.")
# 14. Regular Expressions
import re
pattern = r'\b\d{3}\b'
text = "123 4567 890 12345"
matches = re.findall(pattern, text)
print(matches)
# 15. Functional Programming (map, filter, reduce)
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Map
squared = list(map(lambda x: x ** 2, numbers))
print(squared)
# Filter
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
# Reduce
product = reduce(lambda x, y: x * y, numbers)
print(product)
# 16. Data Structures (collections module)
from collections import deque, defaultdict, namedtuple
# Example usage of deque, defaultdict, namedtuple
queue = deque([1, 2, 3])
queue.append(4)
queue.popleft()
print(queue)
default_dict = defaultdict(int)
default_dict['key1'] = 1
print(default_dict['key1'])
print(default_dict['key2'])
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p.x, p.y)
# Concepts not covered in the previous code snippet (continued)
# 17. List comprehensions
numbers = [1, 2, 3, 4, 5]
squared = [x ** 2 for x in numbers]
evens = [x for x in numbers if x % 2 == 0]
print(squared)
print(evens)
# 18. Dictionary comprehensions
numbers_dict = {x: x ** 2 for x in numbers}
print(numbers_dict)
# 19. Set comprehensions
numbers_set = {x for x in numbers}
print(numbers_set)
# 20. File handling (Using 'with' statement)
with open('sample.txt', 'r') as file:
lines = file.readlines()
for line in lines:
print(line.strip())
# 21. Error handling with try-except-else-finally block
try:
result = 10 / 0 # ZeroDivisionError
except ZeroDivisionError as e:
print(f"Error: {e}")
else:
print("Division successful!")
finally:
print("Execution complete.")
# 22. Handling specific exceptions
try:
num = int(input("Enter a number: "))
except ValueError:
print("Invalid input. Please enter a valid number.")
else:
print(f"Your number is: {num}")
finally:
print("Input handling complete.")
# 23. Lambda functions
addition = lambda x, y: x + y
print(addition(2, 3))
# 24. Generators and Iterators
def generator_example():
for i in range(5):
yield i
gen = generator_example()
for value in gen:
print(value)
# 25. Decorators
def decorator_func(original_func):
def wrapper_func():
print("Wrapper executed this before {}".format(original_func.__name__))
return original_func()
return wrapper_func
@decorator_func
def display():
print("Display function executed.")
display()
# 26. Modules and Packages
import math
print(math.sqrt(16))
# 27. Concurrency and Threading
import threading
def print_numbers():
for i in range(1, 6):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
# 28. Assertions
x = 10
y = 0
assert y != 0, "Divide by zero error"
print(x / y)
# 29. Context Managers
class FileHandler:
def __enter__(self):
print("Entering context...")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting context...")
with FileHandler() as fh:
print("Inside the context.")
# 30. Regular Expressions
import re
pattern = r'\b\d{3}\b'
text = "123 4567 890 12345"
matches = re.findall(pattern, text)
print(matches)
# 31. Functional Programming (map, filter, reduce)
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Map
squared = list(map(lambda x: x ** 2, numbers))
print(squared)
# Filter
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
# Reduce
product = reduce(lambda x, y: x * y, numbers)
print(product)
# 32. Data Structures (collections module)
from collections import deque, defaultdict, namedtuple
# Example usage of deque, defaultdict, namedtuple
queue = deque([1, 2, 3])
queue.append(4)
queue.popleft()
print(queue)
default_dict = defaultdict(int)
default_dict['key1'] = 1
print(default_dict['key1'])
print(default_dict['key2'])
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p.x, p.y)