Saturday, 23 November 2024

Why do we require Map, Filter and Lambda Function?


The question comes to every developer mind is that what is function? and why do we need function? in any programming language.
Let's talk about what is function.
Ans: As we know in industry projects there are so many tasks inside one project. All those tasks are depending on each other or sometimes they work individually. There might be situation comes that same task we require more than one time in each module. so, in that case we have to write that specific task code in all those modules again and again. which ultimately increases the code size of project also readability of code also not there and in case any problem occurs then it is very tedious & time-consuming task to debug this code and rectify the bug.
How to overcome all those limitations simply the answer is using function.
Definition: To perform specific task repeatedly we write a block of code in programming that reusable block of code is called as function.

How to create function in python?
simple every function has its specific function name for eg: if i want to create addition function.

def addition ():
    a = 10
    b = 20
    c = a + b
    print ("addition of two number is:", c)

Here, def is the keyword you have to use compulsory while creating the function after that write function name that must be specific as per the logic of function. after that there is ( ): brackets that indicates either function is taking arguments/parameters as an input or not.
Every function process something and may or may not return the output to the caller function.

here is the example of function taking argument as an input. here we are passing two arguments or parameters that is a and b to addition function.

def addition (a, b):
    c = a + b
    print ("addition of two number is:", c)

When there is word function there are 3 things related with function that is:
1. Function Prototype Declaration: In this we are just deciding the prototype of function. Prototype means set of rules so, here we decide what is our function name, how many arguments it takes, after processing the data does this function return output to caller function or not. Example: addition (int, int): Here function name is addition, and it takes two arguments of type integer means we are setting the rule that this function must take data of type integer only.

2. Function Definition: Here in function definition, we write the logic of that specific task so that we can reuse this function again and again in various places inside the project. Example: if we want to add two integer number then function definition as follows:

def addition (a, b):
    c = a + b
    print ("addition of two number is:", c)

3. Function Calling: Function calling means we have to execute this function definition by calling it. so, when we call the function as per function prototype the body of function get execute and we will get the required output. Example: Now, we want to execute addition function then we must call this function like this:
a = 10
b = 20
addition (a, b) 

here in above code, we are accepting the two values from user and pass those values at the time of function calling for processing.

From the above discussion i hope there is no doubt that what is function and why do we require function. Now let's discuss why do we require map, filter and lambda function.

Why do we need Function?
Ans: We need function to overcome the limitation mentioned above as follows:
  1. To avoid repetition of code.
  2. To improve the code readability.
  3. To simplifies the code debugging.
  4. To enables reusability of code.
  5. To modularize the code.

1. What is the use of map function in python?
Ans: A map is built in function in python. We used map function to map specific functions into looping items.
For Example: I want to find out even numbers from the given numbers list and find out the square of all those even number so without using map function i have to write code as follows:

Without map function:

numbers = [11, 22, 33, 44, 55, 66]
evens = []
for n in numbers:
    if n % 2 == 0:
        evens. append(n)
print(evens) # Output: [22, 44, 66]

evens = [22, 44, 66]
square = []
for a in evens:
    a = a*a:
    square. append(a)
print(square)# Output: [484, 1936, 4356]


Total we have to write 12 lines of code. what we have done here, we first create one list of numbers [] where we take six numbers like 11 to 66 in it. Now among this number we want to find even numbers, so we create one more list of evens [] to store even numbers in it.
To find out the even numbers from numbers [] list we iterate or loop through each element of list by using for loop and check using decision making statement if that either this number is even or odd. if the number is even, we append it inside evens list and so on.
Finally processing all the elements from list will get all even numbers inside evens list and lastly, we display all those numbers to user.
Next, we want to find out the square of all those even numbers from the evens list. so once again we want to process each element from the evens [] list one by one so, we need for loop for the same so, accordingly we use for loop take one element from evens list then find out its square and append this square inside square list. finally, we display all the squared element to the user.

With map function:

Now when we use map function then there is no need to use boiler plate of for loop means without using for loop, we can process all elements inside the list with the help of map and filter function.
How, let's see how we use map () in various way and then at last we do above task.

Syntax:
map (function, Iterable)

 1. using map () we can apply specific function to each element of list.

Without Map means using loop how we do this as follows:

numbers = [11, 22, 33, 44]
twice = []
for n in numbers:
    twice. append (n * 2)
print(twice) # Output: [22, 44, 66, 88]

With Map means without using loop how we do this as follows:

numbers = [11, 22, 33, 44]
twice = list (map (lambda x: x * 2, numbers))
print(twice) # Output: [22, 44, 66, 88]

Explanation:
Now question comes to your mind is how map () works and what is lambda and how it works?

First let's discuss what lambda is.
Using lambda, we can avoid the overhead (means defining the function body and writing that one line of code in it) of defining separate function for easy or simple task.

For example, if we want to do twice each element from numbers list without using lambda function then we have to write definition of twice () function then only it is possible like follows:

def Twice (a):
    return a*2

numbers = (1, 2, 3, 4, 5, 6)
result = map (Twice, numbers)
print (list (result) #Output: 2, 4, 6, 8, 10, 12


How lambda does the same task in one line of code instead of 2 lines of code, because inside function definition there is only one line of code which is very simple.

Twice = lambda x: x*2
print (Twice (5) #Output: 10

why use lambda?
Ans: Without using lambda, you would have to define or write a separate named function even for small or simple one line of tasks, making the code unnecessarily lengthy.

So, when to use lambda?
Ans: When we want to create small unnamed (anonymous) functions in a single line which is often used for short-term, simple operations. 

twice = list (map (lambda x: x * 2, numbers))

Now, we understand how lambda works in it and why to use it. so, in above line of code lambda takes x values from numbers [] with the help of map function find out its double and return that it in map object form. finally, we convert this map object into list type and stored it inside twice [] list.

2. Converting data types into strings.

strings = ["11", "22", "33", "44"]
integers = list (map (int, strings))
print (integers) # Output: [11, 22, 33, 44] 

3. Applying a function to multiple Iterables.

mylist1 = [10, 20, 30]
mylist2 = [44, 55, 66]
sum = list (map (lambda a, b: a + b, mylist1, mylist2))
print(sum) # Output: [54, 75, 96]

4. Applying a predefined function: you can pass predefined function to map.

def square_my_num(x):
    return x ** 2

numbers = [ 2, 3, 4]
squared_list = list (map (square_my_num, numbers))
print(squared_list) # Output: [4, 9, 16]

5. Processing of Strings: Capitalize each string from string list.


names = ["ramnath", "somnath", "tejas", "pallavi"]
capitalized = list (map (str.capitalize, names))
print(capitalized) # Output: ['Ramnath', 'Somnath', 'Tejas', 'Pallavi']

Now, Concepts related with map and lambda i think gets clear idea of it.

Now What is Filter?
Ans: Filters elements from a list (or other iterable) based on a condition, returning only those that meet the condition.

Why to use Filter and when to use it?
Ans: If we are not using inbuilt function Filter then we need to write loop statement and decision-making statement to filter the element from the list. which ultimately increases the code size.

Example: Suppose we want to filter even numbers from the given list.

Without using Filter:

numbers = [11, 22, 33, 44, 55, 66]
evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)
print(evens) # Output: [22, 44, 66]

Using Filter:

numbers = [1, 2, 3, 4, 5, 6]
evens = list (filter (lambda x: x % 2 == 0, numbers))
print(evens) # Output: [2, 4, 6]

Explanation: Based on the lambda functions condition the elements from the numbers [] list get filter and stored inside the evens [] list. here we observe that no need to write looping and condition statement the inbuilt function filter does it automatically for us.


Let's combine this 3 functions map, filter and lambda in one example.

numbers = [11, 22, 33, 44]

# Filter even numbers

evens = filter (lambda x: x % 2 == 0, numbers) # [22, 44]

# Square the even numbers

squared = map (lambda x: x ** 2, evens) # [ 484, 1936]

# Convert to list and print

result = list(squared)

print(result) # Output: [484, 1936]


Tuesday, 19 November 2024

Modules and Regular Expressions in Python.


 

Everyone knows what function is. A block of reusable code that performs specific task. Through function we can organize the code properly, making it more readable and reusable.

Now, question comes to mind that what is a module? why do we require module? How to create a module?

Let's first start with the question why do we require module?

To enhance the modularity of code or project, you can wrap or define more than one related function together and load these functionalities inside another program as and when required.

what is a module?

A module is a block or file consist of classes, variables, constants, definition of functions or any other python object.

or

A file containing more than one related function you want to include in your program just like a code library.

can any program access this module for reusability?

Yes, all the content available inside this module made available to any other program.

Now, next question comes to our mind is how we can access this in our program but before this How to create a module?

Let's create a simple module:

Step 1: First decide what name you want to give to your module. for example, if i want to create "JSPM" module.

Step 2: Create a file JSPM.py and write all the related code inside this file to make it a module.

def welcome (name):

    print ("Hello, "+name+ "Welcome to JSPM University")


# Can we create object of subject's information in module?

Yes,

FPP = {
  "Teachername""Nitin Shivale",
  "age"36,
  "Subjectname""Fundamentals of python programming"
}


Step 3: JSPM Module is created with one welcome () function and one variable FPP of type dictionary in it. As we know we can create variable of all types like array, dictionary, objects etc. Now next task is we want to access all the functionalities available inside JSPM Module in another program. so, how we are going to access it?

Step 4: Suppose i have created the python file of name Admission.py and inside this file i want to write a code to greet all the students who are taking admission in JSPM University. Now we know that functionality is already available inside JSPM Module. no need to write same code once again so simply reuse that code by importing the functionality of that module inside our program.


import JSPM

name = input ("Enter the name of newly admitted student:")

JSPM.welcome (name)

After execution of this code message will display as follows:

Enter the name of newly admitted student: Tejas Bhosale

suppose if you enter the name = "Tejas Bhosale" then it will give us the output: Hello Tejas Bhosale Welcome to JSPM University

Can i create alias of my module and use it to access the functionality of module?

Yes, we can do this it's very simple just use the as keyword to do this.

import JSPM as ju

a = ju.FPP["Teachername"]
print(a)

Here after execution, you will get the output as follows:

Nitin Shivale


Q. Can i import only the required functionalities from the module instead of importing whole module?

Yes, you can suppose from the above example instead of calling all the functionalities i just wanted to import only dictionary variable that is FPP then see how i can do this.

from JSPM import FPP

print (FPP["Teachername"]) #output: Nitin Shivale

print (FPP["age"]) #Output: 36

print (FPP["Subjectname"]) #Output: Fundamentals of python programming


Module Search Path:

When we are importing the module in our program from where exactly it is located and gets loaded in our program is the next question comes to our mind.

Q. How the module search path works in the Python?

When you import module in your program for example, in our above example when we write import JSPM. 

What will happen where the python will search that module let's learn this first:

Python will search for the JSPM.py file from the following sources:

  • The current folder from which the program executes.
  • A list of folders specified in the PYTHONPATH environment variable, if you set it before.
  • An installation-dependent list of folders that you configured when you installed Python.

Python stores the resulting search path in the sys.path variable that comes from the sys module.

If we want to check how many paths are there inside sys.path variable, then we can check this by writing following code.

import sys

for path in sys.path:
    print(path)

To make sure Python can always find the module.py, you need to:

  • Keep module.py in the folder where the program will run.
  • Include the folder that contains the module.py in the PYTHONPATH environment variable. Or you can keep the module.py in one of the folders included in the PYTHONPATH variable.
  • Place the module.py in one of the installation-dependent folders.

Note:

>> import sys
>> sys.path.append('H:\\modules\\')

Python allows you to modify the module search path at runtime by modifying the sys.path variable. This allows you to store module files in any folder of your choice.

Since the sys.path is a list, you can append a search-path to it.


Q. What is Loading and Reloading of Module?

To load a module, we use the import statement. for e.g. to load JSPM module we write import JSPM

Why we have to reload module once it is loaded?
As we know there is always an improvement in every project's functionality likewise in our module also certain things are going to be added or changed after some times. 
To make available those newly added functionality to the code or program which one imported this module in it. we have to reload that module in once again in that program. 
Remember by using reload () function we can reload the module just you have to pass that module object name as an argument to reload () function.

For e.g. 

import sys
import JSPM
JSPM.reload(sys)

Q. What is dir () function?

In Python, the dir() function is a built-in function used to list the attributes (methods, properties, and other members like module, string, list, dictionaries, etc.) of an object.

Syntax: dir({object})

Parameter: dir([optional]): Here it takes object name

Returns:
dir() tries to return a valid list of attributes of the object it is called upon. Also, dir() function behaves rather differently with different type of objects, as it aims to produce the most relevant one, rather than the complete information. 


  • For Class Objects, it returns a list of names of all the valid attributes and base attributes as well. 
  • For Modules/Library objects, it tries to return a list of names of all the attributes, contained in that module. 
  • If no parameters are passed it returns a list of names in the current local scope.
Example:

# Python3 code to demonstrate dir()
# when no parameters are passed
# Note that we have not imported any modules
print(dir())
 
# Now let's import two modules
import sys
import math
 
print(dir())


Output
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '...

When User Defined Objects are Passed as Parameters

# Creation of a simple class with __dir()__
# method to demonstrate it's working
class jspmbsiotr:
 
    # Function __dir()___ which list all
    # the base attributes to be used.
    def __dir__(self):
        return ['college_name', 'principal',
               'intake', 'department', 'address']
 
 
# user-defined object of class jspmbsiotr
my_college = jspmbsiotr()
 
# listing out the dir() method
print(dir(my_college))


Output
['address', 'college_name', 'department', 'intake', 'principal']

Math Module:

The Math Module in Python provides all variety of mathematical functions including constants and Operations. Following program shows us the basic functionalities provided by the math module.

# Import the math module

import math


# Constants provided by the math module

print ("Math Constants:")

print ("Value of pi:", math.pi)  # 3.141592653589793

print ("Value of e:", math.e)    # 2.718281828459045


# Basic mathematical functions

print ("\nBasic Functions:")

number = -25

print (f"Absolute value of {number}: {math.fabs(number)}") # Absolute value

number = 16

print (f"Square root of {number}: {math.sqrt(number)}") # Square root


Trigonometric functions

angle = math.pi / 4 # 45 degrees in radians

print (f"Sine of 45 degrees: {math.sin(angle)}") # Trigonometric sine

print (f"Cosine of 45 degrees: {math.cos(angle)}") # Trigonometric cosine

print (f"Tangent of 45 degrees: {math.tan(angle)}") # Trigonometric tangent


# Logarithmic functions

print ("\nLogarithmic Functions:")

number = 10

print (f"Natural logarithm (ln) of {number}: {math.log(number)}")

print (f"Base-10 logarithm (log10) of {number}: {math.log10(number)}")


# Exponential functions

print ("\nExponential Functions:")

exponent = 2

print (f"e raised to the power of {exponent}: {math.exp(exponent)}")

base = 2

power = 3

print(f"{base} raised to the power of {power}: {math.pow(base, power)}")


# Rounding functions

print ("\nRounding Functions:")

number = 4.7

print (f"Floor of {number}: {math.floor(number)}") # Rounds down

print (f"Ceiling of {number}: {math.ceil(number)}") # Rounds up


# Factorial

print("\nFactorial:")

number = 5

print (f"Factorial of {number}: {math.factorial(number)}")


# GCD (Greatest Common Divisor)

print ("\nGCD (Greatest Common Divisor):")

a = 48

b = 18

print (f"GCD of {a} and {b}: {math.gcd(a, b)}")


# Degrees and Radians Conversion

print ("\nDegree and Radian Conversions:")

angle_in_degrees = 180

angle_in_radians = math.radians(angle_in_degrees)

print(f"{angle_in_degrees} degrees in radians: {angle_in_radians}")

print(f"{angle_in_radians} radians in degrees: {math.degrees(angle_in_radians)}")


Monday, 18 November 2024

Write a python program to accept a file name from the user and perform operations.


Q. What is Data Streaming and Buffering?

Data Streaming and Data Buffering is nothing but different methods of data transfer among client server architecture. As we know with HTTP Protocol, the client can either transfer data to the server that is uploading data to the server or sometimes client request some data from the server means downloading data from the server.


When the size of the data is more than some Megabytes to ensure the loss of data is minimal client and server can choose to transfer data in different chunks.


Now, when the server selects to send the data in chunks. so, at the time of download operations server chooses to transfer data in different chunks.

Now, when the client selects to send the data in chunks. so, at the time of uploading operations client choose to transfer data in different chunks.

Q. What is data Buffering?

Ans: When the requested data is completely accumulated inside the memory and then processed, we called it Buffering.


Q. What is Data Streaming?

Ans: when the data is processed at the time each chunk is received, we called it Streaming.

This is possible because request implements Readable Stream interface, and the content can be directly written to the file system using Writable Stream interface.

Q. What is File?

A File is collection of data that is stored on various devices like Computer, Laptop, Mobile and Servers.
We can say that file is the fundamental unit of digital storage which is used to Store, organize and manage data permanently on secondary storage.

Q. Why do we require File in python?

A File is collection of data that is stored on various devices like Computer, Laptop, Mobile and Servers. Now just consider the following scenario in python.

1. We write a python program to store employee information.
2. Accordingly, we write one class of employee.
3. We have created Three objects of employee inside the RAM (Random Access Memory) and store all the data inside it for three employees. but as you know RAM is a volatile memory that data is available till power is on.
4. Once power is off all data vanished from RAM and we loss all data completely.
5. Now every time when we want that data, we have to execute the code and create data inside the RAM.
6. What if we want that data permanently get stored inside secondary storage so that we can access it whenever we want. To accomplished these, we have to create a file and write all data inside that file for permanently storage.
7. Below is the scenario of Program RAM and File.


Q. How to create File in python?

By using python inbuilt function open (filename, mode) we can create file in python.



How many modes file gets open in python?

  1. r: open for read operation but file must be present.
  2. w: open already existed file if present and override the content in it. but if file is not already present in that case it can create new file and write in it.
  3. a: open already present file in append mode. in this mode we append content at the end.
  4. r+: To perform both operation (read and write data into the file). This mode does not override the already present data, but you can change the data starting from the beginning of the file.
  5. w+: To perform both operation (write and read data). It overwrites the previous file if it is already present, it will truncate the file to zero length or create a file if it does not exist.
  6. a+: To append and read data from the file. It will not override already present data.

Problem Statement:

Write a python program to accept a file name from the user and perform the following operations as       

1. Display the first N line of the file 
2.Find the frequency of occurrence of the word accepted from the user in the file





The number of functions used in above code and their usage are as follows:

1. Strip() Function: If no character is specified in Strip() function then by default it removes all the starting and ending whitespaces.

For Example:

Case 1:

msg = "  JSPM UNI  "

afterstriped = msg . strip ()

print(afterstriped)  # output is: "JSPM UNI"

Case 2:

msg = " ....,,,,nmsssss good sssrrt,,,...st"

afterstrip = msg . strip(".,nmsrt")

print(afterstrip) #output is: good