Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e7a2872

Browse files
method overloading
1 parent 5c5b632 commit e7a2872

File tree

5 files changed

+255
-13
lines changed

5 files changed

+255
-13
lines changed

‎.idea/workspace.xml‎

Lines changed: 11 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Notes/Tuple/Tuple.py‎

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,155 @@
11
print("------------------------------------------------------------------------------------------")
22
print("-------------------------------------Tuple------------------------------------------------")
33
print("------------------------------------------------------------------------------------------")
4+
5+
# Tuple
6+
# A tuple is a collection of ordered elements that can be of different data types.
7+
# Tuples are immutable, meaning that once they are created, their elements cannot be changed.
8+
# Tuples are defined using parentheses () and can contain any number of elements.
9+
# Tuples can also be nested, meaning that a tuple can contain other tuples as elements.
10+
# Tuples are similar to lists, but they are immutable,
11+
# meaning that their elements cannot be changed after they are created.
12+
# Tuples are often used to group related data together,
13+
# such as coordinates (x, y) or RGB color values (red, green, blue).
14+
# Tuples can also be used as keys in dictionaries,
15+
# while lists cannot be used as keys in dictionaries because they are mutable.
16+
# Tuples are also more memory efficient than lists,
17+
# making them a better choice for storing large amounts of data.
18+
# time complexity of tuple is O(1) for accessing elements,
19+
# O(n) for searching elements, and O(n) for iterating through the elements.
20+
# Tuples are also faster than lists for certain operations,
21+
# such as concatenation and repetition.
22+
# Tuples are also hashable, meaning that they can be used as keys in dictionaries,
23+
# while lists are not hashable and cannot be used as keys in dictionaries.
24+
# Tuples are also more memory efficient than lists,
25+
# making them a better choice for storing large amounts of data.
26+
27+
# Methods of Tuple
28+
# 1. count() - Returns the number of occurrences of a specified value in a tuple.
29+
# 2. index() - Returns the index of the first occurrence of a specified value in a tuple.
30+
# 3. len() - Returns the number of elements in a tuple.
31+
# 4. max() - Returns the largest element in a tuple.
32+
# 5. min() - Returns the smallest element in a tuple.
33+
# 6. sum() - Returns the sum of all elements in a tuple.
34+
# 7. sorted() - Returns a sorted list of the elements in a tuple.
35+
# 8. all() - Returns True if all elements in a tuple are true (or if the tuple is empty).
36+
# 9. any() - Returns True if any element in a tuple is true. If the tuple is empty, returns False.
37+
# 10. tuple() - Converts an iterable (like a list) into a tuple.
38+
# 11. zip() - Combines two or more tuples into a single tuple of tuples.
39+
# 12. enumerate() - Returns an enumerate object, which contains pairs of index and value from the tuple.
40+
# 13. reversed() - Returns a reversed iterator of the tuple.
41+
# 14. map() - Applies a function to all items in the tuple and returns a new tuple.
42+
# 15. filter() - Filters elements from the tuple based on a function and returns a new tuple.
43+
# 16. repeat() - Repeats the elements of a tuple a specified number of times.
44+
# 17. unpacking - Unpacks the elements of a tuple into separate variables.
45+
# 18. slicing - Returns a new tuple that contains a portion of the original tuple.
46+
# 19. concatenation - Combines two or more tuples into a single tuple.
47+
# 20. repetition - Repeats the elements of a tuple a specified number of times.
48+
# 21. membership - Checks if an element is present in a tuple.
49+
# 22. iteration - Iterates through the elements of a tuple.
50+
# 23. copying - Creates a shallow copy of a tuple.
51+
# 24. deep copying - Creates a deep copy of a tuple.
52+
# 25. sorting - Sorts the elements of a tuple and returns a new tuple.
53+
# 26. reversing - Reverses the elements of a tuple and returns a new tuple.
54+
# 27. converting - Converts a tuple to a list and vice versa.
55+
# 28. packing - Packs multiple values into a tuple.
56+
57+
# Example of Tuple
58+
# Creating a tuple
59+
my_tuple = (1, 2, 3, 4, 5)
60+
print("Tuple:", my_tuple)
61+
62+
# Accessing elements in a tuple
63+
print("First element:", my_tuple[0])
64+
print("Last element:", my_tuple[-1])
65+
print("Slice of tuple:", my_tuple[1:4])
66+
print("Tuple length:", len(my_tuple))
67+
68+
# Concatenating tuples
69+
tuple1 = (1, 2, 3)
70+
tuple2 = (4, 5, 6)
71+
tuple3 = tuple1 + tuple2
72+
print("Concatenated tuple:", tuple3)
73+
74+
# Repeating tuples
75+
tuple4 = tuple1 * 3
76+
print("Repeated tuple:", tuple4)
77+
78+
# Unpacking tuples
79+
a, b, c = tuple1
80+
print("Unpacked values:", a, b, c)
81+
82+
# Nested tuples
83+
nested_tuple = (1, (2, 3), (4, 5))
84+
print("Nested tuple:", nested_tuple)
85+
86+
# Accessing nested tuple elements
87+
print("Nested tuple element:", nested_tuple[1][0])
88+
print("Nested tuple length:", len(nested_tuple[1]))
89+
90+
# Tuple methods
91+
# Count method
92+
count_tuple = (1, 2, 3, 1, 4, 5)
93+
print("Count of 1 in tuple:", count_tuple.count(1))
94+
95+
# Index method
96+
print("Index of 3 in tuple:", count_tuple.index(3))
97+
98+
# Max method
99+
print("Max element in tuple:", max(count_tuple))
100+
101+
# Min method
102+
print("Min element in tuple:", min(count_tuple))
103+
104+
# Sum method
105+
print("Sum of elements in tuple:", sum(count_tuple))
106+
107+
# Sorted method
108+
print("Sorted tuple:", sorted(count_tuple))
109+
110+
# All method
111+
print("All elements are true:", all(count_tuple))
112+
113+
# Any method
114+
print("Any element is true:", any(count_tuple))
115+
116+
# Tuple conversion
117+
list_to_tuple = [1, 2, 3, 4, 5]
118+
tuple_from_list = tuple(list_to_tuple)
119+
print("Tuple from list:", tuple_from_list)
120+
121+
# Zip method
122+
tuple1 = (1, 2, 3)
123+
tuple2 = (4, 5, 6)
124+
zipped_tuple = zip(tuple1, tuple2)
125+
print("Zipped tuple:", list(zipped_tuple))
126+
127+
# Enumerate method
128+
tuple1 = (1, 2, 3)
129+
enumerated_tuple = enumerate(tuple1)
130+
print("Enumerated tuple:", list(enumerated_tuple))
131+
132+
# Reversed method
133+
tuple1 = (1, 2, 3)
134+
reversed_tuple = reversed(tuple1)
135+
print("Reversed tuple:", list(reversed_tuple))
136+
137+
# Map method
138+
def square(x):
139+
return x * x
140+
tuple1 = (1, 2, 3)
141+
mapped_tuple = map(square, tuple1)
142+
print("Mapped tuple:", tuple(mapped_tuple))
143+
144+
# Filter method
145+
def is_even(x):
146+
return x % 2 == 0
147+
tuple1 = (1, 2, 3, 4, 5)
148+
filtered_tuple = filter(is_even, tuple1)
149+
print("Filtered tuple:", tuple(filtered_tuple))
150+
151+
# Repeat method
152+
from itertools import repeat
153+
tuple1 = (1, 2, 3)
154+
repeated_tuple = repeat(tuple1, 3)
155+
print("Repeated tuple:", list(repeated_tuple))

‎Notes/oops/polymorphism/method_overloading.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# method Overloading
2+
# Method overloading is a feature that allows a class to have more than one method with the same name,
3+
# but different parameters.
4+
# In Python, method overloading is not supported directly,
5+
# but we can achieve it using the multipledispatch library.
6+
# To use the multipledispatch library, you need to install it first. You can do this using pip:
7+
# pip install multipledispatch
8+
# After installing the library, you can use it to create overloaded methods in your class.
9+
# Here is an example of method overloading using the multipledispatch library:
10+
# pip install multipledispatch
11+
12+
113
from multipledispatch import dispatch
214

315
# passing one parameter
@@ -33,3 +45,23 @@ def product(first, second, third):
3345

3446
# calling product method with 3 arguments but all float
3547
obj.product(2.2, 3.4, 2.3) # this will give output of 17.985999999999997
48+
49+
50+
class table:
51+
@dispatch(int)
52+
def table(self, n):
53+
for i in range(1, 11):
54+
print(f"{n} * {i} = {n * i}")
55+
56+
@dispatch(int, int)
57+
def table(self, n, m):
58+
for i in range(1, m + 1):
59+
print(f"{n} * {i} = {n * i}")
60+
61+
62+
obj = table()
63+
# calling table method with 1 argument
64+
obj.table(2) # this will give output of 2 * 1 = 2, 2 * 2 = 4, ..., 2 * 10 = 20
65+
# calling table method with 2 arguments
66+
obj.table(2, 5) # this will give output of 2 * 1 = 2, 2 * 2 = 4, ..., 2 * 5 = 10
67+
# calling table method with 3 arguments

‎Notes/oops/polymorphism/method_overridding.py‎

Whitespace-only changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Operator Overloading
2+
# Operator overloading is a feature that allows us to define the behavior of operators
3+
# for user-defined classes.
4+
# In Python, we can overload operators by defining special methods in our class.
5+
# These special methods are also known as magic methods or dunder methods (double underscore methods).
6+
# The special methods for operator overloading are defined with double underscores
7+
# before and after the method name.
8+
# For example, to overload the addition operator (+), we define the __add__ method in our class.
9+
# Other in arguments are passed to the method as parameters.
10+
# The first argument is always self, which refers to the instance of the class.
11+
# The second argument is the object that we are adding to the first object.
12+
# We can also overload other operators like -, *, /, %, //, **, ==, !=, <, >, <=, >=, etc.
13+
14+
# Here is an example of operator overloading in Python:
15+
class Point:
16+
def __init__(self, x, y):
17+
self.x = x
18+
self.y = y
19+
20+
# Overloading the + operator
21+
def __add__(self, other):
22+
return Point(self.x + other.x, self.y + other.y)
23+
24+
# Overloading the - operator
25+
def __sub__(self, other):
26+
return Point(self.x - other.x, self.y - other.y)
27+
28+
# Overloading the * operator
29+
def __mul__(self, scalar):
30+
return Point(self.x * scalar, self.y * scalar)
31+
32+
# Overloading the / operator
33+
def __truediv__(self, scalar):
34+
return Point(self.x / scalar, self.y / scalar)
35+
36+
# Overloading the str() function
37+
def __str__(self):
38+
return f"({self.x}, {self.y})"
39+
40+
# Example usage
41+
p1 = Point(2, 3)
42+
p2 = Point(4, 5)
43+
p3 = p1 + p2 # Calls __add__
44+
p4 = p1 - p2 # Calls __sub__
45+
p5 = p1 * 2 # Calls __mul__
46+
p6 = p1 / 2 # Calls __truediv__
47+
print(p3) # Output: (6, 8)
48+
print(p4) # Output: (-2, -2)
49+
print(p5) # Output: (4, 6)
50+
print(p6) # Output: (1.0, 1.5)
51+
# In this example, we have defined a Point class that represents a point in 2D space.
52+
# We have overloaded the +, -, *, and / operators to perform addition, subtraction, multiplication, and division on Point objects.
53+
# The __str__ method is also overloaded to provide a string representation of the Point object.
54+
# This allows us to use the operators with Point objects just like we would with built-in types.
55+
# Operator overloading is a powerful feature that allows us to create more intuitive and readable code.
56+
# It allows us to define the behavior of operators for our custom classes, making them behave like built-in types.
57+
# This can make our code more readable and easier to understand.
58+
# However, it is important to use operator overloading judiciously and not to overload operators in a way that is confusing or unexpected.
59+
# In general, operator overloading should be used to make the code more intuitive and readable.
60+
# It should not be used to create unexpected behavior or to make the code more complex.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /