atlas news
    
Life is short - you need Python
01  mai     00h40
Count frequency of items in a list using Python
   In this post I am going to show you how we can write a program in Python that can count the frequency of items in a list. First I shall implement it using the dictionary data structure in Python. Then I shall make it shorter and nicer using the defaultdict and Counter data structures from the...
27  avril     12h41
Python Program to Find Prime Factors of a Number
   nbsp;In this post I am going to write a program in python that finds all the prime factors of a number. I am going to start by writing an empty function and a test. def get prime factors number : prime factors return prime factors if name main : n ...
11  octobre     10h47
divmod - division and modulus together in Python
   divmod is a wonderful, nice, little built in function in Python that allows you to do division and modulus operation together. Using it, you can make your code slightly beautiful, thus Pythonic Check the following example gt; gt; gt; . gt; gt; gt; ...
16  septembre     14h52
Python all any built-in function
   I have been using Python for a long time, still learning lot of simple and new things. Recently I came to know about two built in functions named all and any. Today while I was solving a hackerrank problem, I used both functions which makes the code a bit nicer, in my opinion. The functions...
10  avril     06h45
Create 2D Array using List in Python
   If for some reason you want to create a two dimensional array in Python and don’t want to use external packages like numpy etc., the most obvious thing is to write a code like this : gt; gt; gt; n gt; gt; gt; m gt; gt; gt; ara m n gt; gt; gt; ara ,,, , ,,, ...
29  octobre     04h33
Table Driven Unit Test in Python
   Now days, table driven tests are pretty much industry standard. In my workplace, we use table driven tests when we write unit tests in golang though . Here I shall share a simple code example using pytest that shows how to write table driven tests in Python. In table driven test, what you need to...
12  juin     18h20
global and nonlocal variable in Python
   Most of us are already familiar with global variables in Python. If we declare those variables in a module, the functions inside that module can read python file or .py file can access the variable. For example, check the code below : x def myfnc : print inside myfnc , x def myfnc ...
08  janvier     03h14
Python Code Measure Time
   How to measure time of a Python code snippet We can do it using the timeit module. It has a function called timeit that runs a particular code snippet for N times one million by default and gives you the runtime. Today I just used it for a small purpose and thought of writing a note here. ...
03  février     17h19
Code School Python Courses
   As Python is getting more and more popular and becoming the de facto standard for starting to learn programming, we can see increasing amount of online content to learn Python. Last week got an email form Code School team to check out their couple of new Python courses. I spent around minutes...
25  février     10h43
How to get image size from url
   Today I needed to get image size width and height from image urls. I am sharing my python code here. import requests from PIL import Image from io import BytesIO def get image size url : data requests.get url .content im Image.open BytesIO data return im.size if name...
13  janvier     05h37
crawler controller
   I work on a project where I have written crawlers and the crawlers are running with good amount of sleep . Sometimes, I need to update restart the server. Then I have to start all the crawlers again. So, I have written a script that will control all the crawlers. It will first check if...
09  janvier     10h01
finding maximum element of a list - divide and conquer approach
   Here I share my Python implementation of divide and conquer approach of finding maximum element from a list : Divide and conquer approach to find the maximum from a list def find max li, left, right : if left right: return li left mid left right max ...
06  décembre     17h39
pow() function returns float or integer?
   Today, someone posted this code and asked why x is float gt; gt; gt; from math import pow gt; gt; gt; x pow, gt; gt; gt; x . It’s float because the pow function of math package returns float. But there is a built in pow function in Python which will return integer in this case. You...
11  novembre     13h59
AttributeError: ’module’ object has no attribute
   Are you getting this type of error in your Python program AttributeError: ’module’ object has no attribute ’whatever’ Well, then you might need to change the name of your python file. For example, save the following code in a file named json.py : import json print json.dumps ’foo’, ...
29  octobre     12h48
How to empty a list
   Say you have a list in your program and you are going to reuse the name of the list, or delete all items from the list empty the list . One option is to delete the list completely along with the name : gt; gt; gt; del li Another option is to delete all items inside the list: gt; gt; gt;...
28  août     01h04
fastest list index searching
   Someone was looking for fastest list index searching in Python in stackoverflow. There are only four elements in the list, still it was as issue for him, as it was used in an inner loop and the profiler identified that, this part was executing the most. He tried several approaches. . This is...
20  août     14h07
Python video tutorials in Bangla
   Finally, I have completed my video tutorial series on Python. The lectures are given in my native language Bangla Bengali . Click here to check the videos. In the course I assume that the student has prior programming experience in C Java. So this is not suitable for programming...
09  août     17h16
Python list of lists
   I just stumbled upon a post problem in stackoverflow. Here I discuss it and the solution. First check the following code segment : gt; gt; gt;visited False gt; gt; gt;visited True gt; gt; gt;print visited gt; gt; gt; False, True, False, False , False, True, False, False ,...
27  juillet     15h33
turtle triangle fun
   Just Wrote this piece of code. First just copy paste the code, run and have fun. Then try to write it yourself and have more fun. : import turtle def draw triangle side length, depth : if depth : return counter while counter lt; : counter ...
18  juillet     14h52
draw circle using squares
   I am checking out a Python course at udacity and found an interesting Python module named Turtle. Now let me share with you a simple code that I just have written. import turtle def draw square : window turtle.Screen window.bgcolor red brad turtle.Turtle brad...
01  novembre     12h14
Heap Sort Implementation in Python
   Here I share my Python implementation of heap sort algorithm. Heap sort is O n log n sorting algorithm though quicksort is used more frequently. You should learn heap sort to implement priority queue or at least learn the internals of priority queue. : In the Python code, I directly followed...
20  octobre     09h14
Merge Sort Implementation in Python
   Today I post the python implementation of merge sort. This is straight forward implementation of the algorithm presented in the book Introduction to Algorithms CLRS . If you think the implementation can be more Pythonic, feel free to comment. Here is my code: def merge li, start, mid, end : ...
15  octobre     19h09
recursive binary search in python
   In my previous post, I showed the iterative way to implement binary search algorithm in Python. Here comes the recursive way. def binary search recursive li, left, right, key : while True: if left right: return mid left right if li mid key: return mid ...
    19h06
iterative binary search in python
   Today I am going to post the code for binary search. This one is iterative and the next one will be recursive. If you don’t know about binary search, please read the wikipedia article first. Now, here is my Python implementation of binary search algorithm: def binary search iterative li, left,...
14  octobre     19h00
insertion sort implementation in python
   I am working on my Python coding skills and this time reviewing various algorithms with which I have not been in touch for many days. So I start with sorting, here is my code for insertion sort: def insertion sort ara : for j in range, len ara : key ara j i j ...