Skip to content

Commit a62dd8f

Browse files
committed
chore(classes) add classes examples ⭐
1 parent 7995393 commit a62dd8f

14 files changed

Lines changed: 150 additions & 0 deletions

notes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
A local scope refers to the local objects available in the current function.
3+
A global scope refers to the objects available throughout the code execution since their inception.
4+
5+
6+
A local scope refers to the local objects available in the current function.
7+
A global scope refers to the objects available throughout the code execution since their inception.
8+
9+
The key difference between the two is that while lists are mutable, tuples on the other hand are immutable objects. This means that lists can be modified, appended or sliced on the go but tuples remain constant and cannot be modified in any manner.

training/animals_lambdas.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
animals = ['dog', 'cat', 'parrot', 'rabbit']
2+
uppered_animals = list(map(lambda animal: animal.upper(), animals))
3+
print(uppered_animals)
4+
5+
sequences = [10,2,8,7,5,4,3,11,0, 1]
6+
filtered_result = filter (lambda x: x > 4, sequences)
7+
print(list(filtered_result))

training/another_lambda.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
is_even_list = [lambda arg=x: arg * 10 for x in range(1, 500)]
2+
3+
# iterate on each lambda function
4+
# and invoke the function to get the calculated value
5+
for item in is_even_list:
6+
print(item())
7+
8+
9+
10+
# Python 3 code to people above 18 yrs
11+
ages = [13, 90, 17, 59, 21, 60, 5]
12+
13+
adults = list(filter(lambda age: age > 18, ages))
14+
15+
print(adults)
16+

training/command.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
3+
print ('Number of arguments:', len(sys.argv), 'arguments.')
4+
print ('Argument List:', str(sys.argv))

training/formal_interface.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import abc
2+
class Myinterface( abc.ABC ) :
3+
@abc.abstractclassmethod
4+
def disp():
5+
pass
6+
class Myclass( Myinterface ) :
7+
pass
8+
o1 = Myclass( )

training/interfaces_notes.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#In python classes contains no abstract methods
2+
# Informal Interfaces
3+
# python informal interface is also a class that defines methods that can be overridden but without force enforcement. An informal interface also called Protocols
4+
5+
class Fruits :
6+
def __init__( self, ele) :
7+
self.__ele = ele
8+
def __contains__( self, ele) :
9+
return ele in self.__ele
10+
def __len__( self ):
11+
return len( self.__ele)
12+
Fruits_list = Fruits([ "Apple", "Banana", "Orange" ])
13+
print(len(Fruits_list))
14+
print("Apple" in Fruits_list)
15+
print("Mango" in Fruits_list)
16+
print("Orange" not in Fruits_list)
17+
18+
# As in the above example code, class Fruits implement the _len_, and _contains_ methods, so on the instance of the Fruits class,
19+
# we can directly use the len function to get the size and can check the membership by using the in operator. As in the above code,
20+
# the _iter_ method (iterable protocol) is not implemented, so we would not iterate over the instance of the Fruits.
21+
# Therefore an informal interface cannot be enforced formally.
22+

training/lambda.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
x = lambda a, b, c : a + b + c
2+
#print(x(5, 6, 2))
3+
4+
x = lambda a, b, c : a + b + c
5+
#print(x(5, 6, 2))
6+
7+
8+
9+
def cube(y):
10+
return y*y*y
11+
lambda_cube = lambda y: y*y*y
12+
# using function defined
13+
# using def keyword
14+
print("Using function defined with `def` keyword, cube:", cube(5))
15+
# using the lambda function
16+
print("Using lambda function, cube:", lambda_cube(5))

training/lambda_dif.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# #Lambdas, also known as anonymous functions, are small, restricted functions which do not need a name (i.e., an identifier).
3+
# Every lambda function in Python has 3 essential parts:
4+
# The lambda keyword.
5+
# The parameters (or bound variables), and
6+
# The function body.
7+
# The syntax for writing a lambda is: lambda parameter: expression
8+
#Syntax:
9+
10+
lambda x : x + x
11+
#Syntax:
12+
13+
def sum(x) : return x + x
14+
15+

training/lambda_if.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Example of lambda function using if-else
2+
Max = lambda a, b : a if(a > b) else b
3+
4+
print(Max(1, 2))

training/lambda_list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
2+
3+
final_list = list(filter(lambda x: (x % 2 != 0), li))
4+
print(final_list)

0 commit comments

Comments
 (0)