Python | Learn for Master - Part 2
  • Run external shell command in Python

    There is often a need to call shell command from python directly. In this post, I use examples to show how to run external shell commands in python.

    The recommended way to call shell command from python is using the subprocess library. See the following example:

    In this post, I use example to show how to run shell command in a python program. However, this method has some problems as the output is buffered into memory.

    We need to print out the shell output on the fly if the size is too large.

    [Read More...]
  • Popular python problems and solutions

    Python is a popular programming language that can be used to conduct almost any project. When you learn python, you may come up with different questions regarding various tasks such as file processing, list, dict usage, database, time, url, et al.  In this tutorial, we give clean solutions to some of the most frequently problems you may encounter when you learn python.

    1. File related questions
      How to check whether a file exists using Python?
      How to check whether a path is a file?
      How to make sure an directory exist?
    [Read More...]
  • Popular File related problems and solutions using Python

    We share clean solutions for some of the most popular questions you may encounter when you process files or directories using Python. 

    1. How to check whether a file exists using Python?

    2. How to check whether a path is a file?

      You can use the os.path.isfile function:
      It returns True if path is an existing regular file. This follows symbolic links, so both islink() andisfile() can be true for the same path.

    3. How to make sure an directory exist?
    [Read More...]
  • Popular Python libraries for Data Science and Machine Learning

    Python is almost a-must-have skill for data scientist, as you can see many data scientist positions require python programming skills. This post introduces some of the most popular python modules for data science. They are widely used to conducted projects related to data mining and machine learning, and normal data analysis.

    1. SciPy. SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering. It provides a wide range of algorithms and mathematical tools for data scientist. 

    2. NumPy. NumPy is the fundamental package for scientific computing with Python. 

    [Read More...]
  • Top 20 python libraries you must know

    There are so many python libraries, but sometimes we don’t know which one can be used to solve a certain problem. In this post, I will describe some of the most popular python libraries for different tasks. 

    1. Django: It is most famous framework to develop web applications.  It is a high-level Python Web framework that encourages rapid development and clean, pragmatic design.

    2. IPython.  IPython is a command line shell for interactive computing with many useful enhancements over the “default” Python interpreter.

       IPython Notebooks are a great environment for scientific computing: Not only to execute code,

    [Read More...]
  • LeetCode – Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

    Example:
    Given input array nums = [3,2,2,3], val = 3

    Your function should return length = 2, with the first two elements of nums being 2.

    Hint:

    1. Try two pointers.
    [Read More...]
  • Learn Python List by Examples

    Python List is a powerful data structure that you almost need it in every program. Python List can be used to hold a collection of items which can  be any type. In this post, I will use examples to describe how to use python List. 

    Python List Declaration

    It is easy to create a python list object. The following code declare a List L and initialize it with 4 values.

    In [1]:

    L = [1, 2, 'Jim', ('hello, word')]
    print L
    

     

    [1, 2, 'Jim', 'hello, word']
    

     

    Data look up in python List

    We can locate the elements in the List by index.

    [Read More...]
  • Python Dictionary Example

    Python dict structure is similiar to Java’s HashMap or HashTable.
    It aims to store key value pairs.

    Here is an example:
    we build a dict to map a username to his profile information, which is a tuple

    username => (Full_name, gender, age)

    Search in Dict

    Now we can do a look up by username with O(1) time complexity.
    Search the user with username equal to ‘jim’

    Key not exist exception

    you may get key not exist exception when the username is not in the table

    Do key check

    We can get around this issue by an exist check

    Use get with a default value

    A better method is to use the get() method with default value.

    [Read More...]
Page 2 of 212