# 1.
Example of a class definition statement in Python
class MyClass:
def __init__(self, name):
self.name = name
def my_method(self):
print(f"Hello, {self.name}")
c = MyClass('ahmed')
# Example of a method call statement in Python
c.my_method() # Hello, ahmed
#------------------------------------------------------------------
# 2. global and local variables
x = 'x'
# This is a global variable that can be accessed and modified from
anywhere in the program
def sub():
global x # another way to assign global variable in all program
y = 'y'
print(f'local variable : {y}')
print(f'global variable : {x}')
sub()
#------------------------------------------------------------------
#3.Aliases : Happens when multiple variables have the same memory
address
# so when one changes the others change as well.
a = [2,4,6,8,10]
b = a
b[0] = 0
print(a) # [0, 4, 6, 8, 10]
print(b) # [0, 4, 6, 8, 10]
#------------------------------------------------------------------
#4.Dynamic type binding :The variable is bound to a type when
# assigned a value in an assignment statement.
z = 5
z = [1,2,3]
print(z) # [1,2,3]
#------------------------------------------------------------------
# 5.Type conversion (also known as type casting)
# Type casting is the process of converting a value of one type to
another.
# Example of [1] narrowing type conversion in Python
i = 3.14
ix = int(i) # Convert x to an integer
print(ix) # Output: 3
# Example of [2] widening type conversion in Python
f = 10
fy = float(f) # Convert x to a float
print(fy) # Output: 10.0
#------------------------------------------------------------------
# 6.Example of dynamic scoping in Python
x = 0
def sub1():
x = 1
def sub2():
print(x)
sub2()
sub1() # Output: 1
#------------------------------------------------------------------
# 7.Method overriding: 2 methods that have same name and parameters
# but occur in different classes that has inheritance relationship.
# It may have a different implementation.
class Shape:
def area(self):
pass
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
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
def divide(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
print(type(x)) # Output: <class 'int'>
print(type(pi)) # Output: <class 'float'>
print(type(is_true)) # Output: <class 'bool'>
print(type(name)) # Output: <class 'str'>
print(type(no_value)) # Output: <class 'NoneType'>
#------------------------------------------------------------------
# 11.non-primitive data types
my_list = [1, 2, 3, 4, 5]
my_tuple = (1, 2, 3, 4, 5)
my_set = {1, 2, 3, 4, 5}
my_dict = {"name": "John", "age": 30, "city": "New York"}
print(type(my_list)) # Output: <class 'list'>
print(type(my_tuple)) # Output: <class 'tuple'>
print(type(my_set)) # Output: <class 'set'>
print(type(my_dict)) # Output: <class 'dict'>
#------------------------------------------------------------------
#12.List comprehension
# is a concise and efficient way to create a new list
# by iterating over an existing list or other iterable object.
# Create a new list of squares of numbers from 1 to 10
squares = [x**2 for x in range(1, 11)]
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 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]]
# Count the number of zero elements in the matrix
num_zeros = 0
for row in matrix:
for element in row:
if element == 0:
num_zeros += 1
# Determine if the matrix is sparse
total_elements = len(matrix) * len(matrix[0])
density = num_zeros / total_elements
if density > 0.5:
print("The matrix is a sparse matrix")
else:
print("The matrix is not a sparse matrix")
#------------------------------------------------------------------
#15.Abstract Data Types (ADTs) are data structures that define
# a set of operations to be performed on their elements,
# without specifying how those operations are implemented.
#stack
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
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
if b != 0 and a/b > 2:
print("This will not be printed")
else:
print("Short-circuit evaluation occurred")
#Haskell
"""
module Main where
main :: IO()
main = do
let a = 5
let b = 0
if b /= 0 && a/b > 2
then putStrLn "This will not be printed"
else putStrLn "Short-circuit evaluation occurred"
"""
#------------------------------------------------------------------