PLC codes
PLC codes
def my_method(self):
print(f"Hello, {self.name}")
c = MyClass('ahmed')
class Rectangle(Shape):
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14 * self.radius ** 2
rectangle = Rectangle(5, 3)
circle = Circle(4)
print(rectangle.area()) # Output: 15
print(circle.area()) # Output: 50.24
#------------------------------------------------------------------
# 8.Method overloading: 2 methods with same name but different
arguments and implementation.
class Calculator:
def add(self, a, b=None, c=None):
if b is None and c is None:
return a
elif c is None:
return a + b
else:
return a + b + c
calculator = Calculator()
print(calculator.add(1)) # Output: 1
print(calculator.add(1, 2)) # Output: 3
print(calculator.add(1, 2, 3)) # Output: 6
#------------------------------------------------------------------
# 9.Orthogonality
"""""
The code you provided applies the principle of combining a small set of
primitive structures to make control and data structures of the
language
by using the basic arithmetic operators (+, -, *, /) as the primitive
structures to perform the arithmetic operations in the Calculator
class.
"""
class Calculator:
def add(self, a, b):
return a + b
calculator = Calculator()
result1 = calculator.add(2, 3) # Output: 5
result2 = calculator.subtract(10, 5) # Output: 5
result3 = calculator.multiply(4, 6) # Output: 24
result4 = calculator.divide(10, 2) # Output: 5.0
#------------------------------------------------------------------
#10.primitive data types
x = 10
pi = 3.14
is_true = True
name = "John"
no_value = None
# Haskell
"""
module Main where
main :: IO()
main = do
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let evens = [ x | x <- numbers, x `mod` 2 == 0 ]
print(evens)
-- Output: [2, 4, 6, 8, 10]
"""
#------------------------------------------------------------------
#13.a simple Python code to check if a matrix is a sparse matrix or
not:
# Define a matrix
matrix = [[0, 0, 0, 1],
[0, 2, 0, 0],
[0, 0, 0, 0],
[0, 0, 3, 0]]
#stack
class Stack:
def __init__(self):
self.items = []
def pop(self):
if not self.is_empty():
return self.items.pop()
def peek(self):
if not self.is_empty():
return self.items[-1]
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
#list
list = [1, 2, 3, 4, 5]# create a list of integers
list.append(6) # append an element to the end of the list
list.insert(0, 0)# insert an element at a specific index
list.pop()# remove the last element from the list
list.pop(0)# remove the element at a specific index
list.sort()# sort the list in ascending order
list.reverse()# reverse the order of the elements in the list
length = len(list)# get the length of the list
is_in_list = 3 in my_list# check if an element is in the list
#------------------------------------------------------------------
#16. Short-circuit evaluation
a = 5
b = 0
#Haskell
"""
module Main where
main :: IO()
main = do
let a = 5
let b = 0