Monday, 19 October 2020

How to make a Dice Rolling Simulator using Python ? [ Source code included ]

 Snake and Ladder is a very popular game. We all are familiar with the dice that we use in this game. Here I am explaining a simple program that simulates the dice. I will be using random() function in python to implement this feature. 


 

The random module has so many functionalities and features. Here we will be using the random integer generation feature of the random module.

First lets get the simple program ready. 

import random
dice_value = random.randint(1,6)
print(dice_value)

The random.randint() function is the core logic in this function. The arguments 1 and 6 are the lower and upper limits of the random integer values. The function will return values between both these numbers including these numbers. 

Now let us try to give some more look and feel to the program. I have added some more lines of code to improve usability. This will give a visual effect also.



If you want a more advanced dice simulator, let us modify the program further to with a Graphical User Interface. We will use TKinter package in python to implement this simple user interface.

Sunday, 11 October 2020

How to calculate the area of a triangle using a python program ?

 Calculating the area of a triangle is a very fundamental problem statement. Here I am explaining the calculation of the area of a triangle using Heron's Formula.

What is Heron's Formula ?

This is a formula which has been in use from the past 2000 year. We can easily calculate the area of a triangle if we know the measurement of all the three sides of a triangle.


 As shows in the picture above, if we know all the sides of the triangle a, b and c. We will be able to apply Heron's formula.

Area = (s(s-a) * (s-b) * (s-c)) ^ 0.5

where s = (a+b+c) / 2

We will use the above formula in a python program and make a utility to calculate the area in a programmatic way. 

The sample python program to calculate the area of triangle in given below

Monday, 28 September 2020

How to convert a pandas dataframe into list of dictionaries ?

 Pandas is a high-level data manipulation tool commonly used in python. It is build on Numpy and the key data structure is DataFrame.

Here I am explaining a sample python program to convert a Pandas Dataframe into a list of dictionaries.

Program to convert Dataframe into list of dictionaries

In the below program, we are reading a csv file and loading it in pandas. You can configure the delimiter as per your requirement. The required_fields is an optional parameter that can be used to limit or select the required fields in the Dataframe from the csv. By default, it picks up all the fields in the file.

Sunday, 27 September 2020

Python Program to Split a large list into list of smaller lists

There are requirements where we have to split a large list into a set of smaller lists. This is a common requirement in places where we use parallel processing. Here is a sample program in python to split a larger list into a set of smaller lists.

 


Simple LED blinking circuit using Raspberry Pi and Python

Introduction

Raspberry Pi is one of the powerful device invented in this era. During my school days and college days, all the automation circuits and hobby circuits were developed  either directly by soldering components in the generic circuit board or by using a micro controllers like Arduino. The set up and debug effort was more and the programming was limited to Arduino processing language.

With the invention of Raspberry Pi, the possibilities are endless. It is a mini computer with all the facilities of a desktop PC. Users can develop and run any programs as they wish. The device also comes with options to connect to several peripherals and extension circuit boards.

Here I will be explaining a very simple program, like the baby step towards the world of automation using Raspberry Pi. We will use Raspberry Pi to control an LED (Turn OFF and ON).

Requirements

  •  Raspberry Pi
  • SD Card with Raspbian OS
  • Bread Board
  • Jumper wires
  • LED (2 nos)

GPIO Pins in Raspberry Pi 

A powerful feature of the Raspberry Pi is the row of GPIO (general-purpose input/output) pins along the top edge of the board. Any of the GPIO pins can be designated (in software) as an input or output pin and used for a wide range of purposes. In this example, we are using a python program to control the GPIO pins and turn ON/OFF LED.

Connection

Connect negative leg of both LED to PIN 6(Ground), one positive leg to PIN 8 and another positive LEG to PIN 10

Program

Raspbian OS will be having Thonny Python IDE by default, so it’s easy to write and execute python codes in Raspberry Pi 
 
Install the RPi.GPIO package in the Raspberry Pi. 
 
       
       pip install RPi.GPIO

 
Copy paste the below program in the IDE. Make the connections between LED and GPIO pins as explained in the previous step.
 

Execute the program. You will see the lights blinking.



 


Saturday, 25 April 2020

Anaconda Vs Miniconda Vs Vanilla Python - Which one to use ? Confused ?

Have you heard about Anaconda or Miniconda ?


If you are a python developer or someone who is interested to learn python, you should definitely know this.

Here I will be explaining these from an individual user's perspective.

Anaconda - This is one of the world's popular opensource python distribution with lot of machine learning and datascience packages. This is the best option for someone who is interested to learn datascience & machine learning. This is like a complete ecosystem of libraries and packages for a data scientist or an advanced python programmer.

Miniconda - This is a minimal version of anaconda.  It is a small, bootstrap version of Anaconda that includes only conda, Python, the packages they depend on, and a small number of other useful packages, including pip, zlib and a few others.

Both Anaconda and Miniconda comes with conda package manager. Using conda, we can install the required package from several thousands of cloud repositories. So it is very easy to find and install dependent packages using conda.

Official Python - This is the standard version of Python. This uses pip package manager.

I would recommend miniconda for using as the development environment in windows for basic level python developers as it will be easy to handle the dependencies. Some of the packages will cause trouble while installing using pip in windows.

How to check the memory utilization of cluster nodes in a Kubernetes Cluster ?

 The memory and CPU utilization of a Kubernetes cluster can be checked by using the following command. kubectl top nodes The above command...