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 […]

“with” statement in Python to Open a file

A common way to work with files in Python is to create file handler with “open” statement and work with the file. After finishing the work with the file, we need to close the file handler with close statement. For example, if we want to read all lines of a file using Python , we use […]

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 […]

Eleven IPython Keyboard Shortcuts to Move Around Easily

Eleven IPython keyboard tips/shortcuts to Move Around Easily. Ctrl-p or up-arrow: Access command history in backward Ctrl-n or down-arrow: Access command history  in forward Ctrl-a: Move cursor to beginning of line Ctrl-e: Move cursor to end of line Ctrl-f: Move cursor forward one character Ctrl-b: Move cursor back one character Ctrl-l: Clear screen Ctrl-r: reverse history […]

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 […]

7 Tips to Play With File Names, Directory Names, and Paths Using Python OS.path Module

Python’s OS.path module have a lots of commands/methods that are greatly useful for all things files/directories/paths. Here is seven tips for playing with files, directories, and paths using using Python os.path. How To Check If a “Name” is a Directory? To make sure if a name is directory or not, one can use “os.path.dir” to […]

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 […]

Running Python Command Line and Getting Arguments

When you are writing a python program and want to run the Python script as a command from the terminal, you may want to get the arguments to the python program from command line. For example, you might want to execute the Python script and use its arguments. Python input1 input2 Python’s sys module’s […]

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 […]