Category Archives: Python Basics

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  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. Method  is used to find the first match for the pattern in the string.

Syntax:, string, flags[optional]) 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 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.


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

Python inheritance and polymorphism

Inheritance allows programmer to create a general class first then later extend it to more specialized class. It also allows programmer to write better code.

Using inheritance you can inherit all access data fields and methods, plus you can add your own methods and fields, thus inheritance provide a way to organize code, rather than rewriting it from scratch.

In object-oriented terminology when class X  extend class Y , then Y  is called super class or base class and X  is called subclass or derived class. One more point to note that only data fields and method which are not private are accessible by child classes, private data fields and methods are accessible only inside the class. Continue reading

Python Operator Overloading

You have already seen you can use +  operator for adding numbers and at the same time to concatenate strings. It is possible because +  operator is overloaded by both int  class and str  class. The operators are actually methods defined in respective classes. Defining methods for operators is known as operator overloading. For e.g. To use +  operator with custom objects  you need to define a method called __add__  .

Let’s take an example to understand better Continue reading