What is if __name__ == "__main__" ??
Posted on Oct. 31, 2019, 5:16 p.m.
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 …
Read MorePython recursive functions
Posted on Oct. 31, 2019, 5:09 p.m.
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 …
Read MorePython virtualenv Guide
Posted on Oct. 31, 2019, 4:55 p.m.
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 …
Read MoreInstalling packages in python using PIP
Posted on Oct. 31, 2019, 2:52 p.m.
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 …
Read MorePython Regular Expression
Posted on Oct. 31, 2019, 2:29 p.m.
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.
import re
Now you are ready to use regular expression.
re.search() Method #
The re …
Python Generators
Posted on Oct. 31, 2019, 2:20 p.m.
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 …
Read MorePython *args and **kwargs
Posted on Oct. 31, 2019, 2:06 p.m.
What is *args?
The *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.
1 2 | def sum(a … |
Python Modules
Posted on Oct. 31, 2019, 1:59 p.m.
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 …
Read MorePython Exception Handling
Posted on Oct. 31, 2019, 1:52 p.m.
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:
1 2 3 4 5 | try:
# write some … |
Python inheritance and polymorphism
Posted on Oct. 31, 2019, 1:40 p.m.
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 …
Read More