You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Sieve of Eratosthenes: generate prime numbers with the help of an algorithm given by the Greek Mathematician named Eratosthenes, whose algorithm is known as Sieve of Eratosthenes.
2
+
# File I/O : File input and output operations in Python Programming
3
+
# Exceptions and Assertions
4
+
# Modules : Introduction , Importing Modules ,
5
+
# Abstract Data Types : Abstract data types and ADT interface in Python Programming.
6
+
# 08
7
+
# V
8
+
fromcursesimportpair_number
9
+
10
+
11
+
defsieve_of_eratosthenes(n):
12
+
prime_list= []
13
+
foriinrange(2, n+1):
14
+
ifinotinprime_list:
15
+
prime_list.append(i)
16
+
forjinrange(i*2, n+1, i):
17
+
ifjnotinprime_list:
18
+
prime_list.append(j)
19
+
returnprime_list
20
+
21
+
print(sieve_of_eratosthenes(100))
22
+
23
+
classfile_io:
24
+
def__init__(self, file_name):
25
+
self.file_name=file_name
26
+
self.file_object=open(file_name, "r")
27
+
self.file_content=self.file_object.read()
28
+
self.file_object.close()
29
+
30
+
defread_file(self):
31
+
returnself.file_content
32
+
33
+
defwrite_file(self, content):
34
+
self.file_object=open(self.file_name, "w")
35
+
self.file_object.write(content)
36
+
self.file_object.close()
37
+
38
+
defappend_file(self, content):
39
+
self.file_object=open(self.file_name, "a")
40
+
self.file_object.write(content)
41
+
self.file_object.close()
42
+
43
+
def__del__(self):
44
+
self.file_object.close()
45
+
46
+
file_io_object=file_io("test.txt")
47
+
file_io_object.write_file("Hello World")
48
+
file_io_object.append_file("\nHello World")
49
+
print(file_io_object.read_file())
50
+
delfile_io_object
51
+
52
+
53
+
defexeptions_and_assertions():
54
+
try:
55
+
assert1==2
56
+
exceptAssertionError:
57
+
print("Assertion Error")
58
+
exceptExceptionase:
59
+
print(e)
60
+
61
+
exeptions_and_assertions()
62
+
classadt():
63
+
def__init__(self, name):
64
+
self.name=name
65
+
66
+
def__str__(self):
67
+
returnself.name
68
+
69
+
def__repr__(self):
70
+
returnself.name
71
+
72
+
def__eq__(self, other):
73
+
returnself.name==other.name
74
+
75
+
def__hash__(self):
76
+
returnhash(self.name)
77
+
78
+
79
+
classadt_list(list):
80
+
def__init__(self, name):
81
+
self.name=name
82
+
83
+
def__str__(self):
84
+
returnself.name
85
+
86
+
def__repr__(self):
87
+
returnself.name
88
+
89
+
def__eq__(self, other):
90
+
returnself.name==other.name
91
+
92
+
def__hash__(self):
93
+
returnhash(self.name)
94
+
95
+
adt_list_object=adt_list("adt_list")
96
+
print(adt_list_object)
97
+
print(adt_list_object==adt_list_object)
98
+
print(adt_list_object==adt_list("adt_list"))
99
+
print(adt_list_object==adt_list("adt_list2"))
100
+
101
+
# Classes : Class definition and other operations in the classes , Special Methods ( such as _init_,
102
+
# _str_, comparison methods and Arithmetic methods etc.) , Class Example , Inheritance , Inheritance
0 commit comments