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.
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
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.
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
Regular expression is widely used for pattern matching. Python has built-in support for regular function. To use regular expression you need to import
Now you are ready to use regular expression.
re.search() is used to find the first match for the pattern in the string.
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
Generators are function used to create iterators, so that it can be used in the for loop.
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
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.
def sum(a, b):
print("sum is", a+b)
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 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.
Create a new file called
mymodule.py and write the following code.
foo = 100
print("i am from mymodule.py")
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.
# write some code
# that might throw exception
# Exception handler, alert the user
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
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
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
Let’s take an example to understand better Continue reading
Creating object and classes
Python is an object-oriented language. In python everything is object i.e
bool even modules, functions are also objects.
Object oriented programming use objects to create programs, and these objects stores data and behaviors. Continue reading