## 12 Basic Commands with NumPy Array

NumPy (pronounced as Num-pee or Num-pai) is one of the important python packages (other being SciPy) for scientific computing. NumPy offers fast and flexible data structures for multi-dimensional arrays and matrices with numerous mathematical functions/operations associated with it. Core data structure in NumPy is “ndarray”, short for n-dimesional array for storing numeric values. Let us […]

## How to read entire text file in Python?

Often one might need to read the entire content of a text file (or flat file) at once in python. In this post, we showed an example of reading the whole file and reading a text file line by line. Here is another way to import the entire content of a text file.

## Three Ways to Merge (or flatten) Lists in Python

Often you may have to flatten a list of lists or merge multiple lists into a single list in python. Obviously there are multiple ways to go about it. Here are three ways to flatten or merge lists in to a list. Merge with “+” Operator” If you have three separate lists like [1, 2, […]

## How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like 12345678912345678912345678912345678912345678910 and want to split the long text to multi-line text, where each line is of length w, like w=9 123456789 123456789 123456789 123456789 123456789 10 In the above […]

## How to Run a Shell Command from Python and Get The Output?

In Python, often you may want to execute linux command and get the output of the command as string variable. Naive way to do that is to execeute the linux command, save the output in file and parse the file. A better way to get the output from executing a linux command in Python is […]

## How To Paste Code in Python Interpreter? Hint: Use iPython

If you have formatted code block with proper indentation and want to test it by copying and pasting on a python interpretor, you will be frustrated at Python for complaining at you. iPython offers great solutions for copying and pasting the code right from the terminal interpreter. One useful command in iPython to play with […]

## Three Ways to Write Text to a File in Python

If you are interested in writing text to a file in Python, there is probably many ways to do it. Here is three ways to write text to a output file in Python.  The first step in writing to a file is create the file object by using the built-in Python command “open”. To create and […]

## Mastering Lists in Python Using List Comprehensions

Ever faced the problem of creating a unique lists in Python? or Accessing only specific elements of lists in Python? Python’s “List Comprehensions” offers you the immense power to create and use lists. Here is a brief introduction to understand and getting started on using List Comprehensions in Python. Python’s List Comprehensions Example 1 The […]

## Getting Started With Map, Filter, and Reduce in Python

Python has a couple of nice built-in function in the flavor of functional programming paradigm. These built-in functions like map, filter, and apply combine very well with lambda functions. Map, filer, and reduce help you create, work with and tame lists in Python. For example sure, at some point you will have to deal with […]

## Getting Started with Lambda Functions in Python

Python lets you create a function on the go, but without really assigning a name to the function. These anonymous functions are called “Lambda Functions”. One typically writes a lambda function on the fly, if one wants to write a function for one-time use. Lambda Functions come handy in a variety of situations and are […]