Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Programming

Paradigms
Using Python
Programming Paradigms

• Imperative: Program state change

• Procedural: Procedural calls to modularize code

• Object Oriented: Entity state and behavior

• Functional: Everything is a math equation


Example: Computing square
of each item in the list

• [ 1, 2, 3, 4, 5 ]

Ans:

• [1, 4, 9, 16, 25 ]
Imperative

temp_list = [ 1, 2, 3, 4, 5]

result_list = [ ]

for item in temp_list:

result_list.append ( item ** 2 )

——————————————————-

result_list = [ 1, 4, 9, 16, 25 ]
Procedural
temp_list = [ 1, 2, 3, 4, 5]

def compute_square( arg_list ):

result_list = [ ]

for item in arg_list:

result_list.append ( item ** 2 )

return result_list

result_list = compute_square ( temp_list)

——————————————————-

result_list = [ 1, 4, 9, 16, 25 ]
Object Oriented
class ChangeList(object):

def __init__(self, any_list):

self.any_list = any_list

def do_square(self):

self.square = [ ]

for x in any_list:

square.append( x**2)

temp_list_object = ChangeList(my_list)

temp_list_object.do_square( )

print(temp_list_object.square)

——————————————————-

[ 1, 4, 9, 16, 25 ]
Functional

import functools

temp_list = [1, 2, 3, 4, 5]

square = lambda x: x**2

result_list = functools.reduce(square, temp_list)

print (result_list)

——————————————————-

[ 1, 4, 9, 16, 25 ]
Functions and Functional
Paradigms

for x in l:

function_name(x)

map ( function_name, l )
Statically Typed Language

• a = 5

• a = “5”
Statically Typed Language

• a = 5

• a = “5”
Dynamically Typed
Language

• a = 5

• a = “5”
Weakly Typed Language

• a = 5

• b = “5”

• c = concatenate(a, b)

• d = add(a, b)
Strongly Typed Language

• a = 5

• b = “5”

• c = concatenate(str(a), b)

• d = add(a, int(b))
Which one is slowest &
Why?

• C, C++, Java : Static, Strongly Typed

• Python: Dynamic, Strongly Typed

• Perl: Dynamic, Weakly Typed

• It doesn’t affect the speed


SURPRISE

Quiz
1 True or False
• We can pass statements as arguments in imperative
programming language.

• We can pass procedures as arguments in procedural


programming language.

• We can pass objects as arguments in object oriented


programming language.

• We can pass functions as arguments in functional


programming language.
2. MCQ

• Automatic conversion of data type is done in

A. Dynamic Type Language

B. Weekly Type Language

C. Static Type Language

D. Strongly Type Language


3. True or False
class Point( ):

pass

one = Point( )
one.x = 3, one.y = 4

two = Point( )
two.x = 3, two.y = 4 one == two

one == two

4. True or False

import copy

c = Circle( )
d = copy.copy ( c )
c == d
                 
5. True or False

import copy

c = Circle( )
d = copy.copy ( c )
c.center == d.center
6. Short Answer (one short
sentence)

• Difference between _ _ mul _ _ and _ _ rmul _ _


7. Short Answer
• Identify the programming paradigm for each of the
following statements

A. swap ( add (5,6), 10 )

B. [ 2 * item for item in [ -2, -1, 0, 1, 2] ]

C. list ( map ( lambda item: 2 * item, [ -2, -1, 0, 1 ,


2 ] ) )

D. [ abs (item) for item in [ -2, -1, 0, 1, 2] ]


8. True or False

A static method can modify the class variables ?


9. Short Answer (one word/
phrase)

• a = 5, 6.4, “hello”

• type ( a )
10. Short Answer

• Compute 2’s complement in binary for decimal value 14


Duck Type
• If it looks like a duck

• Swims like a duck

• Quacks like a duck

• Then, probably its a

• Duck

You might also like