Python Lambda Function

Python allows you to create anonymous function i.e function having no names using a facility called lambda function.

lambda functions are small functions usually not more than a line. It can have any number of arguments just like a normal function. The body of lambda functions is very small and consists of only one expression. The result of the expression is the value when the lambda is applied to an argument. Also there is no need for any return statement in lambda function.

Let’s take an example:

Consider a function multiply()

This function is too small, so let’s convert it into a lambda function.

To create a lambda function first write keyword lambda followed by one of more arguments separated by comma, followed by colon sign ( : ), followed by a single line expression.

Expected Output:

Here we are using two arguments x  and y , expression after colon is the body of the lambda function. As you can see lambda function has no name and is called through the variable it is assigned to.

You don’t need to assign lambda function to a variable.

Expected Output:
12

Note that lambda function can’t contain more than one expression.

What is if __name__ == “__main__” ??

Every module in python has a special attribute called __name__ . The value of __name__  attribute is set to '__main__'  when module run as main program. Otherwise the value of __name__  is set to contain the name of the module.

Consider the following code for better understanding.

Here we have defined a new module my_module . We can execute this module as main program by entering the following code Continue reading

Python recursive functions

When a function call itself is knows as recursion. Recursion works like loop but sometimes it makes more sense to use recursion than loop. You can convert any loop to recursion.

Here is how recursion works. A recursive function calls itself. As you you’d imagine such a process would repeat indefinitely if not stopped by some condition. This condition is known as base condition. A base condition is must in every recursive programs otherwise it will continue to execute forever like an infinite loop.

Overview of how recursive function works Continue reading

Python virtualenv Guide

Note: This tutorial need pip, if you have not already done so, first go through installing pip .

virtualenv is a tool used to separate different dependencies required by the projects. While working on multiple projects it’s a common issue that one project need a version of package that is completely different from the other one, virtualenv helps us to resolve such kind of issues. It also helps to prevent polluting global site package.

Installing virtualenv

virtualenv is just a package available at pypi, you can use pip to install virtualenv.

After installation you may need to add C:\Python34\Scripts  to your PATH environment variable. This way commands like pip, virtualenv will become available in any directory level. Continue reading

Installing packages in python using PIP

PIP is a package management system used to install packages from repository. You can use pip to install various software packages available on http://pypi.python.org/pypi.  pip is much similar to composer in php. Pip is a recursive acronym which stands for  pip installs packages.

Installing pip

Python 2.7.9 and later (python2 series), and Python 3.4 and later (python 3 series) already comes with pip.

To check your python version you need to enter the following command : Continue reading

Python Regular Expression

Regular expression is widely used for pattern matching. Python has built-in support for regular function. To use regular expression you need to import re module.

Now you are ready to use regular expression.

re.search() Method

re.search()  is used to find the first match for the pattern in the string.

Syntax: re.search(pattern, string, flags[optional])

re.search() method accepts pattern and string and returns a match object on success or None  if no match is found. match object  has group()  method which contains the matching text in the string. Continue reading

Python Generators

Generators are function used to create iterators, so that it can be used in the for loop.

Creating Generators

Generators are defined similar to function but there is only one difference, we use yield  keyword to return value used for each iteration of the for loop. Let’s see an example where we are trying to clone python’s built-in range()  function. Continue reading

Python *args and **kwargs

What is *args ??

*args  allows us to pass variable number of arguments to the function. Let’s take an example to make this clear.

Suppose you created a function to add two number like this.

As you can see this program only accepts two numbers, what if you want to pass more than two arguments, this is where *args  comes into play. Continue reading

Python Modules

Python module is a normal python file which can store function, variable, classes, constants etc. Module helps us to organize related codes . For e.g math module in python has mathematical related functions.

Creating module

Create a new file called mymodule.py and write the following code.

Continue reading

Python Exception Handling

Exception handling enables you handle errors gracefully and do something meaningful about it. Like display a message to user if intended file not found. Python handles exception using try .. except ..  block.

Syntax:

As you can see in try block you need to write code that might throw an exception. When exception occurs code in the try block is skipped. If there exist a matching exception type in except  clause then it’s handler is executed. Continue reading