Skip to content

Commit 1e13f34

Browse files
authored
Add files via upload
1 parent 460b808 commit 1e13f34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2466
-48
lines changed

Diff for: Advance_function.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ def addList(val = "NA", lst = []):
4949
print(addList(1))
5050
print(addList("Py"))
5151
print(addList(2))
52+
5253
print(addList(4, []))
5354
print(addList(5, ["Hello"]))

Diff for: Class_1.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
author : Jaydatt Patel
3+
class in python
4+
'''
5+
6+
class Point:
7+
# creating variables
8+
var1 = 1234
9+
var2 = 'Python'
10+
11+
# constructor with default paramere arguments
12+
def __init__(self,x = 999,y = 999): # self is currunt pointing object, like 'this' in java or c++
13+
self.x = x # if var not defined in self class then it will define new variable
14+
self.y = y
15+
16+
# function in class
17+
def show(self):
18+
return {'X' : self.x,'Y' : self.y}
19+
20+
# creating an object using different constructor
21+
obj1 = Point(25,41)
22+
obj2 = Point(9,13)
23+
obj3 = Point(50)
24+
obj4 = Point(y=15,x=16)
25+
obj5 = Point()
26+
27+
print("obj1 is obj2 : ", obj1 is obj2)
28+
print('------------1-------------')
29+
print('obj1 :', obj1.show())
30+
print('obj2 :', obj2.show())
31+
print('obj3 :', obj3.show())
32+
print('obj4 :', obj4.show())
33+
print('obj5 :', obj5.show())
34+
print('------------2-------------')
35+
Point.get = 55 # create variable in class at anywere
36+
obj1.z = 10 # create variable in only for object at anywere
37+
38+
print('Point.get : ',Point.get)
39+
40+
print('obj1.get : ',obj1.get)
41+
print('obj1.z : ',obj1.z)
42+
43+
print('------------3-------------')
44+
obj1.var1 = 356 # this willl
45+
print("Point.var1 : ",Point.var1)
46+
print("obj1.var1 : ",obj1.var1)
47+
print("obj2.var1 : ",obj2.var1)
48+
49+
print('------------4-------------')
50+
Point.var1 = 985
51+
print("Point.var1 : ",Point.var1)
52+
print("obj1.var1 : ",obj1.var1)
53+
print("obj2.var1 : ",obj2.var1)

Diff for: Class_2.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
author : Jaydatt Patel
3+
class in python
4+
'''
5+
6+
class Fruit():
7+
def __init__(self, name, price):
8+
self.name = name
9+
self.price = price
10+
11+
def sort_priority(self):
12+
return self.price
13+
14+
# creating list of Fruit objects
15+
L = [Fruit("Cherry", 10), Fruit("Apple", 5), Fruit("Blueberry", 20)]
16+
17+
18+
print("---------sorted method-(1)-------")
19+
for f in sorted(L, key=lambda x: x.price):
20+
print(f.name)
21+
22+
print("---------sorted method-(2)-------")
23+
for f in sorted(L, key=lambda x: x.sort_priority()):
24+
print(f.name)
25+
26+

Diff for: Class_Inheritance_1.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'''
2+
authour : Jaydatt Patel
3+
Inheritance python
4+
'''
5+
# creating base or super class
6+
class Person:
7+
8+
# defining variable
9+
class_type = 'Person'
10+
11+
# creating constructor for person
12+
def __init__(self,name='None',age=0):
13+
self.name = name
14+
self.age = age
15+
16+
# method overriding
17+
def __str__(self):
18+
return "Name: {}, Age: {}".format(self.name,self.age)
19+
20+
# method overriding
21+
def fun(self):
22+
return print('Base')
23+
24+
# creating derived class from base or super class
25+
class Student(Person):
26+
27+
# overriding variable
28+
class_type = 'Student'
29+
30+
# creating constructor for student
31+
def __init__(self,name,age,course):
32+
# calling super class constructor
33+
Person.__init__(self,name,age) # here self is current object that pass to super class
34+
self.course = course
35+
36+
# method overriding
37+
def __str__(self):
38+
return "Name: {}, Age: {}, Course: {}".format(self.name,self.age, self.course)
39+
40+
# method overriding
41+
def fun(self):
42+
return print('Derived')
43+
44+
print('------------------')
45+
p = Person('Amit',18)
46+
p.fun()
47+
print(p.class_type,p)
48+
49+
print('------------------')
50+
st = Student('Rahul',20,'Computer Engineer')
51+
st.fun()
52+
print(st.class_type,st)
53+
54+
55+

Diff for: Class_Override_Method_1.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
author : Jaydatt Patel
3+
overide methods in class in python
4+
'''
5+
6+
class Point:
7+
# constructor
8+
def __init__(self,x = 0,y = 0):
9+
self.x = x
10+
self.y = y
11+
12+
# override function : __str__() for class to string cast.
13+
def __str__(self):
14+
return 'Point(__str__) : x = {} , y = {}'.format(self.x,self.y)
15+
16+
# override function : __add__()
17+
def __add__(self,other):
18+
return Point((self.x + other.x) , (self.y + other.y)) # return new Point by adding two objects
19+
20+
# override function : __str__()
21+
def __sub__(self,other):
22+
return Point((self.x - other.x),(self.y - other.y)) # return new Point by subtracting two objects
23+
24+
obj = Point()
25+
print('Override(__str__) : ',obj)
26+
27+
obj1 = Point(25,41) # Instantiate an object
28+
obj2 = Point(9,13) # make a second
29+
obj3 = obj1 + obj2
30+
print('add: ',obj3)
31+
print('sub: ',obj1 - obj2)
32+
33+

Diff for: Exception_handing_1.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'''
2+
author : Jaydatt Patel
3+
4+
Exception handing
5+
syntax :
6+
try:
7+
<try clause code block>
8+
except <ErrorType> as err_variable:
9+
<exception handler code block>
10+
except <ErrorType> as err_variable:
11+
<exception handler code block>
12+
..
13+
..
14+
..
15+
16+
17+
# Note : Whenever you occur exception, you must pass to Exception class or perticular define class like IndexError, ZeroDivisionError.
18+
# You can not pass divide by zero exception to IndexError class, It must be passed to ZeroDivisionError class.
19+
# If You pass divide by zero exception to IndexError class instead of ZeroDivisionError class, Then program terminated and rest of the program will not execute.
20+
21+
22+
# Standard Exceptions :
23+
24+
StandardError : Base class for all built-in exceptions except StopIteration and SystemExit.
25+
ImportError : Raised when an import statement fails.
26+
SyntaxError : Raised when there is an error in Python syntax.
27+
IndentationError : Raised when indentation is not specified properly.
28+
NameError : Raised when an identifier is not found in the local or global namespace.
29+
UnboundLocalError : Raised when trying to access a local variable in a function or method but no value has been assigned to it.
30+
TypeError : Raised when an operation or function is attempted that is invalid for the specified data type.
31+
LookupError : Base class for all lookup errors.
32+
IndexError : Raised when an index is not found in a sequence.
33+
KeyError : Raised when the specified key is not found in the dictionary.
34+
ValueError : Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.
35+
RuntimeError : Raised when a generated error does not fall into any category.
36+
MemoryError : Raised when a operation runs out of memory.
37+
RecursionError : Raised when the maximum recursion depth has been exceeded.
38+
SystemError : Raised when the interpreter finds an internal problem, but when this error is encountered the Python interpreter does not exit.
39+
40+
41+
# Math Exceptions :
42+
43+
ArithmeticError : Base class for all errors that occur for numeric calculation. You know a math error occurred, but you don’t know the specific error.
44+
OverflowError : Raised when a calculation exceeds maximum limit for a numeric type.
45+
FloatingPointError : Raised when a floating point calculation fails.
46+
ZeroDivisonError : Raised when division or modulo by zero takes place for all numeric types.
47+
48+
49+
# I/O Exceptions :
50+
51+
FileNotFoundError : Raised when a file or directory is requested but doesn’t exist.
52+
IOError : Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. Also raised for operating system-related errors.
53+
PermissionError : Raised when trying to run an operation without the adequate access rights.
54+
EOFError : Raised when there is no input from either the raw_input() or input() function and the end of file is reached.
55+
KeyboardInterrupt : Raised when the user interrupts program execution, usually by pressing Ctrl+c.
56+
57+
# Other Exceptions :
58+
59+
Exception : Base class for all exceptions. This catches most exception messages.
60+
StopIteration : Raised when the next() method of an iterator does not point to any object.
61+
AssertionError : Raised in case of failure of the Assert statement.
62+
SystemExit : Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, it causes the interpreter to exit.
63+
OSError : Raises for operating system related errors.
64+
EnvironmentError : Base class for all exceptions that occur outside the Python environment.
65+
AttributeError : Raised in case of failure of an attribute reference or assignment.
66+
NotImplementedError : Raised when an abstract method that needs to be implemented in an inherited class is not actually implemented.
67+
68+
'''
69+
70+
print("----------(1)----------")
71+
try:
72+
items = ['a', 'b']
73+
third = items[2]
74+
print("This won't print")
75+
except Exception as err:
76+
print("Got Error : " , err)
77+
78+
print("continuing")
79+
80+
print("----------(2)----------")
81+
try:
82+
x = 5
83+
y = x/0
84+
print("This won't print, either")
85+
except Exception as err:
86+
print("Got error : ",err)
87+
88+
print("continuing again")
89+
90+
91+
print("----------(4)----------")
92+
dic = {'name':'Rahul','Age' : 20}
93+
try :
94+
for key in dic:
95+
dic[key]
96+
dic['Mobile'] # key not exist
97+
except KeyError as err:
98+
print("Got error : ", err)
99+
100+
print("continuing again")

Diff for: Exception_handing_2.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
'''
2+
author : Jaydatt Patel
3+
4+
Exception handing
5+
syntax :
6+
try:
7+
<try clause code block>
8+
except <ErrorType> as err_variable:
9+
<exception handler code block>
10+
except <ErrorType> as err_variable:
11+
<exception handler code block>
12+
..
13+
..
14+
..
15+
16+
17+
# Note : Whenever you occur exception, you must pass to Exception class or perticular define class like IndexError, ZeroDivisionError.
18+
# You can not pass divide by zero exception to IndexError class, It must be passed to ZeroDivisionError class.
19+
# If You pass divide by zero exception to IndexError class instead of ZeroDivisionError class, Then program terminated and rest of the program will not execute.
20+
21+
22+
# Standard Exceptions :
23+
24+
StandardError : Base class for all built-in exceptions except StopIteration and SystemExit.
25+
ImportError : Raised when an import statement fails.
26+
SyntaxError : Raised when there is an error in Python syntax.
27+
IndentationError : Raised when indentation is not specified properly.
28+
NameError : Raised when an identifier is not found in the local or global namespace.
29+
UnboundLocalError : Raised when trying to access a local variable in a function or method but no value has been assigned to it.
30+
TypeError : Raised when an operation or function is attempted that is invalid for the specified data type.
31+
LookupError : Base class for all lookup errors.
32+
IndexError : Raised when an index is not found in a sequence.
33+
KeyError : Raised when the specified key is not found in the dictionary.
34+
ValueError : Raised when the built-in function for a data type has the valid type of arguments, but the arguments have invalid values specified.
35+
RuntimeError : Raised when a generated error does not fall into any category.
36+
MemoryError : Raised when a operation runs out of memory.
37+
RecursionError : Raised when the maximum recursion depth has been exceeded.
38+
SystemError : Raised when the interpreter finds an internal problem, but when this error is encountered the Python interpreter does not exit.
39+
40+
41+
# Math Exceptions :
42+
43+
ArithmeticError : Base class for all errors that occur for numeric calculation. You know a math error occurred, but you don’t know the specific error.
44+
OverflowError : Raised when a calculation exceeds maximum limit for a numeric type.
45+
FloatingPointError : Raised when a floating point calculation fails.
46+
ZeroDivisonError : Raised when division or modulo by zero takes place for all numeric types.
47+
48+
49+
# I/O Exceptions :
50+
51+
FileNotFoundError : Raised when a file or directory is requested but doesn’t exist.
52+
IOError : Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. Also raised for operating system-related errors.
53+
PermissionError : Raised when trying to run an operation without the adequate access rights.
54+
EOFError : Raised when there is no input from either the raw_input() or input() function and the end of file is reached.
55+
KeyboardInterrupt : Raised when the user interrupts program execution, usually by pressing Ctrl+c.
56+
57+
# Other Exceptions :
58+
59+
Exception : Base class for all exceptions. This catches most exception messages.
60+
StopIteration : Raised when the next() method of an iterator does not point to any object.
61+
AssertionError : Raised in case of failure of the Assert statement.
62+
SystemExit : Raised when Python interpreter is quit by using the sys.exit() function. If not handled in the code, it causes the interpreter to exit.
63+
OSError : Raises for operating system related errors.
64+
EnvironmentError : Base class for all exceptions that occur outside the Python environment.
65+
AttributeError : Raised in case of failure of an attribute reference or assignment.
66+
NotImplementedError : Raised when an abstract method that needs to be implemented in an inherited class is not actually implemented.
67+
68+
'''
69+
70+
print("----------(1)----------")
71+
try:
72+
items = ['a', 'b']
73+
third = items[2]
74+
print("This won't print")
75+
except Exception as err:
76+
print("Got Error : " , err)
77+
78+
print("continuing")
79+
80+
print("----------(2)----------")
81+
try:
82+
x = 5
83+
y = x/0
84+
print("This won't print, either")
85+
except Exception as err:
86+
print("Got error : ",err)
87+
88+
print("continuing again")
89+
90+
91+
print("----------(4)----------")
92+
dic = {'name':'Rahul','Age' : 20}
93+
try :
94+
for key in dic:
95+
dic[key]
96+
dic['Mobile'] # key not exist
97+
except KeyError as err:
98+
print("Got error : ", err)
99+
100+
print("continuing again")

0 commit comments

Comments
 (0)