Monday, April 3, 2023

Functional Programming in Python

 Functional programming is a programming paradigm that breaks down a problem into individual functions. In this paradigm, we avoid mutable data types and state changes as much as possible. It also emphasizes RECURSION rather than loops. map(), filter() and reduce() are the three cornerstone functions of functional programming.



map()

This first argument to map() is a transformation function, where each original item is transformed into a new one. 

num = [2, 3, 6, 9, 10]

def cube(num):

  return num ** 3

cubed = map(cube, num)

print(list(cubed))


Another way:

cubed = map(lambda n: n ** 3, num)

 

Another method:

list(map(lambda x, y: x / y, [6, 3, 5], [2, 4, 6]))

[3.0, 0.75, 0.8333333333333334]


A lot of math-related transformations can be performed with map().

For tuples we use starmap().


import itertools 

num = [(2, 3), (6, 9), (10,12)] 

multiply = itertools.starmap(lambda x,y: x * y, num)

list(multiply) #  [6, 54, 120]



filter()

A filtering operation processes an iterable and extracts the items that satisfy a given condition. The function argument must be a single-argument function. It’s typically a boolean-valued function that returns either True or False. The filter() accepts only one iterable.


num = [12, 37, 34, 26, 9, 250, 451, 3, 10]

even = list(filter(lambda x: (x % 2 == 0), num))

print(even)


sort() 


The sort method is a helpful tool to manipulate lists in Python. For example, if you need to sort a list in ascending or reverse order, you can use the following:


num = [24, 4, 13, 35, 28]

num.sort()

num.sort(reverse=True)

It is important to note that the sort() method mutates the original list and it is therefore impossible to revert back the list’s items to their original position. 



groupby()

ITERTOOLS.GROUPBY() takes a list of iterables and groups them based on a specified key. The key is useful to specify what action has to be taken to each individual iterable. The return value will be similar to a dictionary, as it is in the {key:value} form. Because of this, it is very important to sort the items with the same key as the one used for grouping. 


import itertools

spendings = [("January", 25), ("February", 47), ("March", 38), ("March", 54), ("April", 67),

             ("January", 56), ("February", 32), ("May", 78), ("January", 54), ("April", 45)]

spendings_dic = {}

func = lambda x: x[0]

for key, group in groupby(sorted(spendings, key=func), func):

    spendings_dic[key] = list(group)

print(spendings_dic)


{'April': [('April', 67), ('April', 45)],

 'February': [('February', 47), ('February', 32)],

 'January': [('January', 25), ('January', 56), ('January', 54)],

 'March': [('March', 38), ('March', 54)],

 'May': [('May', 78)]}

In the above snippet, we used sorted() instead of sort(). This is because we wanted to sort an iterable that was not a list.


Contrary to sort(), sorted() will create a copy of the original list, making it possible to retrieve the original order. 

Finally, we can use map() from the previous section to sum the monthly expenses:


monthly_spendings = {key: sum(map(lambda x: x[1], value)) for key, value in spendings_dic.items()}

print(monthly_spendings)

{'April': 112, 'February': 79, 'January': 135, 'March': 92, 'May': 78}



reduce()

The REDUCE() function implements a technique called FOLDING or reduction. It takes an existing function, applies it cumulatively to all the items in iterable, and returns a single final value.


reduce() was originally a built-in function and was moved to functools.reduce() in Python 3.0.


Unless you cannot find any solution other than reduce(), you should avoid using it. The reduce() function can create some abysmal performance issues because it calls functions multiple times, making your code slow and inefficient. Functions such as sum(), any(), all(), min(), max(), len(), math.prod() are faster, more readable, and Pythonic. 


from functools import reduce

yearly_spendings = reduce(lambda x, y:x + y, monthly_spendings.values())

print(yearly_spendings)

496


Serialization in Python

 There are different ways in Python to read data from files and to write data to files.


1. Using input from console

x = input('Enter your name:')

print('Hello, ' + x)



2. Using File Handling methods

The file handling methods in Python include open, close, readlines and writelines.

Common operation modes for files are r(read), w(write), a(append at end) and r+(read and write).


# read

text_file = open('/Users/pankaj/abc.txt','r')

line_list = text_file.readlines()

for line in line_list:

    print(line)

text_file.close()


# write

text_file = open('/Users/pankaj/file.txt','w')

word_list= []

for i in range (1, 5):

    print("Please enter data: ")

    line = input() 

    word_list.append(line) 

text_file.writelines(word_list) # overwrites file data

text_file.close() 


# append

text_file = open('/Users/pankaj/file.txt','a')

word_list= []

for i in range (1, 5):

    print("Please enter data: ")

    line = input() 

    word_list.append(line)

text_file.writelines(word_list)

text_file.close() 


The file.seek(7) moves the cursor to the specified location.



3. Serialization

Serialization is the process of converting the object into a format that can be stored or transmitted.  Python has a number of built-in modules for this process: marshall, json, and pickle. The marshall is mainly used by the interpreter. The json produces human-readable output and works well with other languages, but it works only with certain data types.So pickle provides the best Serialization solution. 

pickle.dump(): to convert data into serialized form

pickle.load(): to convert serialized format into original data type.


# pickle

import pickle

number_of_data = int(input('Enter the number of data : '))

data = []

for i in range(number_of_data):

    raw = input('Enter data '+str(i)+' : ')

    data.append(raw)

file = open('important', 'wb')

pickle.dump(data, file)

file.close()


# unpickle

file = open('important', 'rb')

data = pickle.load(file)

file.close()

for item in data:

    print(item)

Static in Python

Instance variables

They are declared inside a method or the constructor of a class. Their values vary from object to object.

Class variables

They are declared inside the class, but outside all method definitions. They are shared among all instances of a class. They are allocated memory when an object for the class is created for the first time. They can be accessed by either using objects or class name.

Instance methods

They are specific to each object. They can access both class variables and instance variables. They use self as the first parameter. They can be called only using the object of the class.

Class methods

They are shared among all objects of the class. They can access only class variables. They use cls as the first parameter. They can be called using ClassName or by using a class object. They are mostly used as factory methods.

Static methods 

They are also shared among all objects of the class. They cannot access any variable of a class. They do not take any extra parameter. They can be called using ClassName or by using a class object. They are mostly used as utility methods.

class Student:

    # class variables

    school_name = 'ABC School'


    # constructor

    def __init__(self, name, age):

        # instance variables

        self.name = name

        self.age = age


    # instance method

    def show(self):

        print(self.name, self.age, Student.school_name)


    @classmethod

    def change_School(cls, name):

        cls.school_name = name


    @staticmethod

    def find_notes(subject_name):

        return ['chapter 1', 'chapter 2', 'chapter 3']

Static methods are useful in creating Utility functions.


Decorator

A decorator is a function that takes another function as an argument and extends its behavior without explicitly modifying it. eg: for logging, debugging, authentication, measuring execution time, and many more. 

def decorator(func):

  def wrapper():

    print("This is printed before the function is called")

    func()

    print("This is printed after the function is called")  

  return wrapper

This decorator function is called by adding @decorator above the actual function. eg: @staticmethod, @classmethod, @property (for getter/setter). Its main function is to support code reusability.


Iterator

It is a way of iterating over iterable objects like lists, tuples, dicts, and sets.

tup = ("apple", "banana", "cherry")

itr = iter(tup)

print(next(itr))

print(next(itr))

print(next(itr))


Generator

Generators are functions used to create iterators. These functions use one or more yield statements instead of return.

def seq(x):

    for i in range(x):

        yield i      

range_ = seq(4)

print(next(range_))

print(next(range_))

print(next(range_))

print(next(range_))

Generator functions are more memory efficient than normal functions.

Multithreading in Python

Multithreading makes threads appear to be running parallelly. We can do multithreading in Python using the threading module.


import threading

def even():

    for i in range(0,20,2):

        print(i)

def odd():

    for i in range(1,20,2):

        print(i)

if __name__ == "__main__":      # main method

    trd1 = threading.Thread(target=even)

    trd2 = threading.Thread(target=odd)

    trd1.start() 

    trd2.start() 

    trd1.join()   # waiting until thread 1 is done

    trd2.join()   # waiting until thread 2 is done

    print('End')


functions:

threading.active_count(), threading.main_thread(),  threading.current_thread().name 

threading.enumerate()    # list of active threads


Synchronization

A race condition occurs when two or more threads access shared data and try to change it at the same time. We use the locks to solve this problem. The locks provide two methods: acquire, release.

lock = threading.Lock() 

trd1 = threading.Thread(target=increment,args=(lock,))

def increment(lock):

    for _ in range(200000):

        lock.acquire()

        a += 1

        lock.release()

Containers in Python

Container is any object that can hold other objects. There are 4 built in containers:

1. List

A list is defined as an ordered, mutable, and heterogeneous collection of objects.

list1=[1,'a',5.6,'Python']   # create

list1[1],  list1[-4:7]    # read

list1[1]=1.2    # reassigning

for i in lst:     #  loop using in

print(i)

for i in range(len(lst)):     # loop on index

  print(lst[i])

new_list=[expression for element in list]   # list comprehension

eg: string="Python"

list3=[i for i in string]         # create

print(list3)    #  gives ['P','y','t','h','o','n']


list1=[1,2,3,4,5,6]

list2=[ i if i%3==0 else -1 for i in list1 ]          # list comprehension with if else (if no else, if part is at the end)

print(list2)


list1.append(17)   # add at end

list1.extend([2,8.0,'l'])   # add >1

list1.insert(2,'a')    #  add at 2nd index



del list1[1:4] #  deleting 2nd to 4th elements

lst.pop()  # delete last element

lst.pop(1)  # delete 1st index element

lst.remove('x')  # delete element's first occurrence


len(lst), min(lst), sum(lst)

list('abc')   # type casting

any(lst),  all(lst)


lst.index('s')   # find     (in can also be used to find)

lst.count('x')     # find

lst.reverse()     # reverses, no sorting

lst.clear()


m_list=[[1,2,3,4],['a','b','c'],[4.5,6.7,1.3]]     #2d list

print(list1+list2)     #   Concatenation

print(list2*3)   # repeats thrice

print('b' not in list2)    #  boolean

lst.copy(),   list(lst)      # copy

lst = [ ]  # empty list

Sort

lst.sort()    #   sorts the list    , available only in list

sorted(lst)    # returns sorted list, original unaffected, method in other containers also

def check(item1, item2):    # comparator method

    return lst(item1) - lst(item2)

from functools import cmp_to_key

sorted(mylist, key=cmp_to_key(check))



2. Tuple

Tuples are ordered and heterogenous, but they are immutable.

tup = ("python", 1.1, 3)      # create

tup = tuple(lst) ,   tup = tuple(str),   tup = (3,)   # create

tup[0], tup[-4:-1]  # access

a, b, c = tup  #  unpacking

Addition to a tuple can be done in 2 ways - by concatenation, or by converting to list and append.

Tuple3 = Tuple1 + Tuple2    #   concatenation. 

No update or reassign can be done on tuple since immutable.

del tup  # delete

tup.index(), tup.count()    # methods to find

len(tup), any(tup), all(tup), max(tup), sorted(tup)   #   functions on tuple  # sorted returns list

Methods that cannot be used for tuples:   append(), insert(), remove(), pop(), clear(), sort(), reverse()

for i in tup:    # loop

tup1=(1,2,3), tup2=(1,2,3), tup1 is tup2    # returns false

tup = ( )   # empty tuple



3. Set

Sets are unique, unordered, heterogenous and mutable. But a set cannot have mutable elements like lists, sets, or dictionaries (Strings and tuples are immutable along with all primitives)

Set = {'A', 'B', 'C', 'D'}     #   create

Set = set(lst), Set = set(tup)    # create

for x in Set:      # loop

We cannot read from set using index.

Set.add('E')     # insert

Set.update(set1)    #   concatenate

Set.update(lst)

set2 = Set.union(set1)  # concatenate with assignment

Set.remove('D')     # delete

Set.discard('D')

Set.pop()    # cannot give index

Set.clear()

del Set


len(Set), all(Set), any(Set), sorted(Set)     # built-in functions

max(Set), sum(Set) for homogeneous 

set2 = Set.intersection(set1)    # common elements

Set.isdisjoint(set1)     # True if no common elements

set2 = Set.difference(set1)     # minus

set2 = Set.symmetric_difference(set1)   # exclude common

set1.issubset(Set) 

set1 = Set.copy()

Cannot use Set={ } for empty set since that becomes a dictionary.

set1={'abab'} gives {'abab'}

set2=set('abab') gives {‘b’, ‘a’}



4. Dictionary

They store values in key-value pairs.  They are ordered (from Python 3.7) with unique keys. They are mutable. But their keys should be immutable.

dic = {'a':1,'b':2,'c':3}   #   create

dic = dict(a = "John", b = 36, c = "Norway")    #      create with equal to and no quotes for keys

dict[lst]    

dic['a'], dic.get('a')      #    access

dic = { }   #  empty dictionary

dic.keys()  ,   dic.values()

dic['a'] = 5      #    add ,  reassign ,   update

dic.pop('a') ,  del dic,  dic.clear() , dic.popitem()    #   delete

for i in  dic:      # loop keys

for x, y in dic.items():

dic.copy(),  dict(dic)    #  copy

Nested dictionary has dictionary as value, accessed as  dict['a']['age']

len(dic), any(dic),  all(dic), sorted(dic)

dic.fromkeys({0,1,2,3,4})    # create None valued dictionary with keys

dic.has_key('a')

dic={x:x*2 for x in ['a','b','c','d','e']}   #    dictionary comprehension

dic = {1: 'Geeks', 2: 'For',

        3: {'A': 'Welcome', 'B': 'To', 'C': 'Geeks'}}     # nested dictionary


Strings in Python

String is an immutable data structure in Java. That means, once value has been assigned to a String, it can’t be changed and if changed, a new object is created. It is also a sequence of characters. Python does not have a character data type, a single character is simply a string with a length of 1. 

Create

String str = "Test"

String str = 'Test'

String str = '''Tes

t'''

Retrieve

String characters can be retrieved with the index.

s = str[3] # fourth character

s = str[-2] # second last character

s = str[1:3] # slicing, returns characters from 1 to 2

i = str.index('e') # returns index

str.index('e', 2, 7) # start and stop locations

str.rindex('e')  # last index


Add/Update/Delete

str = "Hello".join(str)  # using join

str = "Hello" + str[0] + 'eeeee' + str[1:]  #  using plus  # indexing and slicing

str += "hi" 

str = str.replace('l','m')

str = str[0:4]   # deletion

del str

str[0] = 'x' will give error


In all the three cases, the result has to be reassigned to the value, else value will return unchanged.


Formatting

str = r"The \n stands for next line"    #  raw string

str = "{} {} {}".format('I', 'like', 'mango')

str = "{0} {2} {1}".format('I', 'mango', 'like')

var="mango"

str = f"I like {var}"

num = 12.3456789

print('Two decimals: %3.2f' %num)

print('Four decimals: %3.4f' %num)

print(a,b)  # concat

str1 = ' '

str2 = 'geeks'

print(repr(str1 and str2))  # returns ' '   # repr is printable representation to print quotes

print('a' and 'b')  # returns b or second element for and

print('a' or 'b')  # returns a or first element for or

'a' * 3 # returns aaa


Operations

Other operations we can do with a String are,

len(str),  str.upper(), str.strip() 

str.split(',')   # returns list

't' in str, str.find("t"),   str.index("t")

str[2:10:2]   # start,stop,step

str[::-1]   # to reverse

print(type(str)) is String

for x in "banana":

  print(x)


str(obj)  # type casting

ord(str) to get ascii

chr(str) to get back string from ascii

str[len(str)-1]

str.isalnum()   # alphanumeric


Comment inside a method given at the top with triple quotes is called DocStrings.


Memory

str="check" and str1="check" are saved in the same memory location.

str==str1 checks for value and returns true.

str.__eq__(str1) also checks for value and returns true.

__eq__() method can be overridden in a class.

str is str1 checks for memory location and returns true


(In Java, == checks for location and .equals() of String is overridden to check for value. Location is same within Stringpool.)


Now, str="check@" and str1="check@" are saved in different memory locations due to special character.

So str==str1 is true, str.__eq__(str1) is true, but str is str1 is false.

String objects are saved in the heap just like any other object. But String interning is a process that reuses basic alphanumeric Strings. This saves a lot of memory.

OOP concepts in Python

Python is an Object Oriented Programming Language. In Python, everything is object, but everything is not in classes. The Basic OOP concepts are,

Abstraction

Python has to import Abstract Base Class to add the feature of abstraction.

from abc import ABC,abstractmethod

class Polygon(ABC):

   @abstractmethod    

   def sides(self):   

      pass  

  

class Triangle(Polygon):  # Polygon is parent

   def sides(self):   # all methods should pass self

      print("Triangle has 3 sides")   

class Square(Polygon):   

   def sides(self):   

      print("Square has 4 sides")       


Triangle().sides()   # Class() creates object

Square().sides()   # function call is outside any class


Polymorphism

Two ways of achieving this are,

1. Overloading

def add(p, q, r = 0):  # default value given to make parameter optional
    return p + q + r  
print (add(6, 23))  
print (add(22, 31, 544))  

2. Overriding

class Bird:  
    def intro(self):  
        print("There are multiple types of birds in the world.")  
    def flight(self):  
        print("Many of these birds can fly but some cannot.")  
  
class Sparrow(Birds):  
    def flight(self):  
        print("Sparrows are the birds which can fly.")  
      
class Ostrich(Birds):  
    def flight(self):  
        print("Ostriches are the birds which cannot fly.")  
      
Bird().intro()
Sparrow().intro()  # same as bird
Ostrich().flight()   # overridden method


Inheritance

A child class extends the parent and overrides its necessary methods. 

class Vehicle:
    def drive(self):
print("Parent")

class Car(Vehicle):   # single inheritance
    def __init__(self):  # constructor
       Vehicle.__init__(self)  # calling parent constructor
    def drive(self):
print("Child")

class Nano(Car,Bus):  # multiple inheritance
    def drive(self):
print("Grand Child")

print(issubclass(Car,Vehicle))  # returns True
n = Nano()  
print(isinstance(n,Nano))   # returns True

Every class inherits from the base class called Object.



Encapsulation

In Python, instance variables are declared inside the constructor. They can be of 3 types 

class Employee:
    def __init__(self, name, project, salary):
        self.name = name   # public- accessible everywhere
        self._project = project   # protected with single underscore- accessible within class and subclasses
        self.__salary = salary   # private with double underscore- accessible only within class

Private members can be accessed using public getter methods or by name mangling. Getters and Setters are declared only for private variables, others can be accessed with dot.

def get_salary(self):
    print('Salary:', self.__salary)
print('Salary:', new Employee()._Employee__salary) # one underscore before and two after
print('Project:', new Employee()._project)