Python Tutorials and Examples

Python map examples

Python map is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that map applies read more

Python value in array (list) check – code snippet

In Python we frequently need to check if a value is in an array (list) or not. Python x in list can be used for read more

Python string split examples

Python has in-built string split function which can be used to split a string on any delimiter and return a python list (array). If no read more

Python string quick start tutorial

Python built-in data type string is very frequently used in any python program. Here are some examples of using strings to get started with. Print read more

Python filter vs ifilter

Python filter is very handy python built-in function to filter iterables (e.g. lists) in python. But it returns a list which may consume huge memory read more

Python filter list/iterable examples

Python filter is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that filter applies read more

Selenium Phantomjs – check browser errors using pytest

Checking browser errors (including Javascript errors) is an important use case in testing a website. Javascript errors are hard to QA and can cause bad read more

Python selenium – print browser log (including javascript errors) with phantomjs

It is useful to find out javascript errors in a page automatically. This can help in site quality test automation. Here is selenium code snippet read more

Python – dictionary get key with default

Get dictionary key with default if not found using method get() This is better approach in most cases. Get dictionary key using square brackets ([]) read more

python print examples

Here are some examples you can print strings or variables in python on stdout. Print a string with newline in the end By default python read more

Python for loop examples

Frequently used python for loop examples. For loop to iterate over a list Iterate on each element of a list Iterate on each index, element read more

Python selenium – execute javascript code

Selenium code in python can execute custom Javascript and also return values from webdriver (PhantomJS, Firefox, Chrome. etc.) context. Here is sample code which counts read more

Python pytest – using selenium with PhantomJS

Here is quick tutorial to use python selenium with PhantomJS as driver. We will fetch a page and assert that page title has certain keyword. read more

How to find python package’s file location

Here is simple python code snippet to see the file location of a python package on command line: $ python -c “import urllib2; print urllib2.__file__;” read more

How to test a site using pytest

Python is fairly good programming language for writing and maintaining unit and functional tests. One advantage of python is that it does not require compilation read more

Command line – run python webserver

Sometime we need to run a quick static webserver which can serve various files in a directory. This can be pretty handy to serve html read more

Python selenium webdriver – quick start guide on Mac

Selenium is an excellent tool which automates browsers. One of the commonly approach is to use Selenium webdriver which has API in many languages like Python, Java, etc. This read more

Using python to analyze bots from apache logs

Apache logs contains pretty useful information about various visitors and bots coming to your site. Here is how a typical apache log entry looks like: read more

Using robotexclusionrulesparser python module to parse robots.txt and crawl a site

Here is a small code snippet to demonstrate how to parse robots.txt and crawl a web page in python using robotexclusionrulesparser module url = “http://www.foo.com/bar” read more

Python – How to sort dictionary by values

Sometimes we want to iterate over a dictionary in sorted order of values. We can do it by using key argument to python sorted() method read more

Python/Perl/Unix one liners

Here are some cool python one liners. Parse apache log using python: print 5th field python -c “import csv,sys;f=csv.reader(sys.stdin, delimiter=’ ‘); print ‘\n’.join([r[5] for r read more