Tuesday, April 28, 2020
Python recursion,memorization,decorator memorization
Python recursion example1
Python recursion with memorization
Python recursion with memorization with python decorator
Sunday, April 26, 2020
Python data structures list,dic,set,counter etc.. explained in malayalam
I tried to explain some of the python built in data structures in Malayalam here.
Python example for list
Python example for dictionary
Python example for Set
Python example for Counter
Python example for defaultdictonary
Python example for list
Python example for dictionary
Python example for Set
Python example for Counter
Python example for defaultdictonary
Thursday, April 23, 2020
Traverse through 2 dimensional list
A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list
Slicing the list and manipulating the list is easy in python. Since its a sequence is iterable, we can use for loop on list. "Sequence in Python, sequence is the generic term for an ordered set.Lists are the most versatile sequence type. The elements of a list can be any object, and lists are mutable - they can be changed. Elements can be reassigned or removed, and new elements can be inserted" python two dimensional list
Wednesday, April 22, 2020
Python TRIE
Python implementation for TRIE
TRIE is very interesting data structure and its very much used in word suggestion algorithms. Python implementation for this would easiest way to learn it.
Python Trie example 1
Tuesday, April 21, 2020
Python Generator
Have you ever think through how to build an function that generate infinite series ?
You might be thinking why can't we use an infinite loop! the problem is you can't come of the loop and again resume from the position/state where you stopped .
But generators helps in this case, they are just like functions but the difference is they have yield keyword in the definition which is like a return wrapped with generator-iterator object
"When you call either a function or a generator, a stackframe is created. It has the local variables
(including the arguments passed into the function),a code pointer to the active opcode, and a stack for pending try-blocks,with-blocks, or loops.
In a regular function, execution begins immediately.When return is encountered, the final result is kept and the stackframe is freed along with everything it referenced.
In a generator function, the stackframe is wrapped in a generator-iterator object and returned immediately. The code in the generator function only runs when called by next(g) or g.send(v). Execution is suspended when yield is encountered. "
Python generator infinite sequence example
Python generator things to be known ( exhausted generator )
Monday, April 20, 2020
Python inheritance - Examples
Python inheritance explained with Visualization
Python inheritance example 1
Python inheritance ( with child methods) example 2
Python inheritance and overriding example3
Sunday, April 19, 2020
How to deploy python programs to Docker
Running python programs in Docker
It is quite easy to run your python programs in docker. For getting started with python in docker, we need to install docker in our system.
I am using Ubuntu and followed the instruction details beginners commands to start with Docker.
Once you have installed docker, we can start building our docker. We can create a separate folder/directory for this in our system.
FROM tells Docker which image you base your image on (in the example, Python 3).
RUN tells Docker which additional commands to execute.
CMD tells Docker to execute the command when the image loads.
Dockerfile:
#base image
FROM python:3
#adding our first program/application to docker image
ADD app.py /
ADD app2.py /
#this time we are using a script to run our applications.
ADD script.sh /
#make the script executable
RUN ["chmod", "+x", "./script.sh"]
#changed the command to run the script
CMD ./script.sh
#you can read more about commands in docker at https://docs.docker.com
#add the command instruction
#CMD ["python","./app.py"]
script file:
You can specify your python programs in the script.
Note: I have explicitly used python3 to run the programs.
#!bin/bash
#first process
python3 app.py
#second process
python3 app2.py
Python applications
print("this is my first pythong docker program")