Python Tutorials and Examples

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 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 lowercase and uppercase

To convert a string to lowercase of uppercase following code can be used: s_lower = s.lower() s_upper = s.upper() string lowercase example string uppercase example

Python string contains check

To check if a string is part of another string (case sensitive) we can use these approaches Using in operator Using string find() method ## read more

Python int to string

Python int (or any other data type like float) needs to be converted to string before we can do concatenation operation using plus (+). It read more

Python string to int

Python string can be converted to int using int(strval). Note that it will throw ValueError for invalid string. Here is a quick example to convert read more

Python list (array) basics

Python list (or array) – some basic operations. Initialize list access ith item access last item list length/size using len() Note than len() can also read more

Python list – append or prepend a value

Appending or prepending a value to a list in Python. Append value Prepend value

Python list/array – remove an item

To remove an item from python list method pop([i]) can be used. Remove last item Remove first item Remove ith item

Python merge two lists (arrays)

To merge two (or more) lists (arrays) the operator plus (+) can be used. It will append items of 2nd list to first list and read more

Python array slice

Slicing a list (array) in python is very handy way to get a sub list of an array. Its syntax is ## slice from start read more

Python clone/copy array

Python cloning an array (list) using slice operator.

Python initialize large list with same value

To initialize a large list with same value (all items having same value), the following syntax can be used. a = [val] * num_items Example

Python value in array (list) check

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 dictionary basics

Python dictionary (associative arrays) – some basic operations. Initialize dictionary Check if a key exists access value for a key list length/size using len() len() 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 – 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 if-else and if-elif-else

Python if else statements can be expressed on two ways Simple if-else statement if-elif-else statement with one or more occurrences of elif. if else example 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 re search vs match

Python re.match() checks for a match only at the beginning of the string, while checks for a match anywhere in the string. Re search read more

Python re (regex) match examples

Python regular expression module (re) can be used to check a string begins with a pattern using re.match(). Note that it is different from read more

Python re (regex) search examples

Python regular expression module (re) can be used to find presence of a pattern in a string using Basic usage of is:, read more

Python re (regex) replace examples

Python regular expression module can be used to replace a pattern in a string using re.sub. Basic usage of re.sub is: re.sub(pattern, repl, string, count=0, 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

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

Python etl petl – read table from csv file

Python elt library petl can be used to perform extract/load – reading/writing tables from files and databases. In this tutorial we’ll read a table in read more

Python file read write examples

Reading and writing files in python is very common use case. Here are some examples: Write to file using “with open()” Here file gets closed 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

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 itertools ifilter examples

Python itertools.ifilter is efficient way of filtering and looping over a large iterable. Here is the usage for ifilter: itertools.ifilter(function, iterable) The function should take read more

Python itertools imap examples

Python itertools imap is efficient way of mapping and looping over a large iterable. Here is the usage for imap: itertools.imap(function, *iterables) The function should read more

Python find length of string or list using len()

Python len can be used to return length of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or read more

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 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 range examples

Python range can be used to generate a range of numbers with desired start number and gap. It is often used in for loops. Usage: read more

Python rstrip – remove training spaces and newline

Python rstrip can be used to removed trailing whitespaces (spaces, newlines, tabs, etc.) from a string. It Usage newstring = s.rstrip([chars]) If chars string is read more

Python type – find type of a variable

Python in-built function type() can be use to find type of any variable or value. Example – type() of string variable Example – type() of read more

Python xrange examples

Python xrange can be used to generate a sequence of numbers with desired start number and gap. It is often used in for loops. It read more