Link

Jump Start with Python: Dictionary

Dictionary is an abstract data type, composed of a collection of key value pairs enclosed within the braces, where a key is immutable and must be unique. A dictionary is an unordered collection and is mutable in nature. It can contain the mixed types, as the keys and their corresponding values.

Python accounts for the dictionary under mapping types. Dictionaries are also called the hash tables or associative arrays. Below is a simple example of Python dictionary:

Dictionary Example
Dictionary Example

Creating a Dictionary

A dictionary in Python can be created in two different ways:

  1. Just enclose (key, value) pairs between the braces.
    Syntax: dict_name={ “key1”: “val”, “key2”: “val”, “key3”: “val”, —, “keyn”: “val” }
  2. Using dict constructor.
    Syntax: dict_name=dict(key1=“val”, key2=“val”, key3=“val”, —, keyn=“val”)

Some Facts About Dictionary

  • A dictionary without any positional argument is called an empty dictionary.
  • A dictionary is an unordered set of the keys, value pairs and are indexed, using the keys specified in it.
  • The keys in a dictionary are unique and if multiple assignments to a key are attempted, the latter value will be assigned to the respective key discarding the value, previously assigned to it.

Basic Operations

  • Accessing a Dictionary
  • Updating a Dictionary
  • Deleting Dictionary Elements
  • Deleting a Dictionary

Dictionary Methods & Functions

  • Length
  • String
  • Clear Method
  • Pop Method
  • Pop Item Method
  • Copy Method
  • From Keys Method
  • Get Method
  • Items Method
  • Keys Method
  • Values Method
  • Update Method

My article “Jump Start With Python: Dictionary – Part Eight” is the eighth article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start With Python – Part 7 (Tuples)“, Click Here

Dynamic jQuery Charts for Your Website

If you have ever worked with large data sets, then you must know how difficult it is to work with raw data alone. Making sense out of long rows of data is not easy. Add some visualization to it and you suddenly start seeing patterns that were never visible before. But how do you visualize data for your website?

Enter JavaScript chart libraries. They make it very easy to add beautiful charts to any web page.

In this tutorial I will cover the process of creating such charts in detail. You will have a working chart with full source-code by the end of this article. We will do it in jQuery – one of the most popular JavaScript libraries. For making charts, we will make use of FusionCharts’ JavaScript charts library. And to make our task easier, we will take help of its jQuery charts plugin. If you are reading this tutorial for integrating charts in your personal project, then you make use of its free personal license.

A quick look into what we will be making:

See the Pen Dynamic Charts for Your Website in jQuery by Gagan Sikri (@sikrigagan) on CodePen.0

We will cover following 3 sections to get to our final goal:

  • Part-1: Loading required JavaScript files for the project
  • Part-2: Defining HTML for chart
  • Part-3: Rendering the chart

Part 1: Loading Required JavaScript Files

For this project, we need these four JavaScript files:

  • jQuery library: download compressed/minified version from linked page or include it via a CDN.
  • FusionCharts’ library: download package contains both minified and un-minified versions. As shown below include fusioncharts.js and fusioncharts.powercharts.js. Even if you don’t include fusioncharts.powercharts.js explicitly, it will pick it up itself if it is present in the same folder.
  • jQuery charts plugin: this has to be downloaded separately from linked page. Does not come with download package.

We will Include all the files listed above in the HTML using <script> tags. Here’s the code for that:

<head>
    <!-- jQuery -->
    <script type="text/javascript" src="js/jquery.min.js"></script>

    <!-- FusionCharts files -->
    <script type="text/javascript" src="/js/fusioncharts.js"></script>
    <script type="text/javascript" src="/js/fusioncharts.powercharts.js"></script>

    <!-- jQuery charts Plugin -->
    <script type="text/javascript" src="/js/jquery-plugin.js"></script>
</head>


Part 2: Defining HTML for Chart

We need an HTML container element inside which our charts will go. I recommend using a <div> element for this purpose. Here is the HTML code for that:

<div id="spline-chartcontainer">
    Area chart is on its way!
</div>


Part 3: Rendering the Chart

We now need to create a chart instance using insertFusionCharts jQuery method which was added by the jQuery plugin we added in Step-1. insertFusionCharts will be called on the container we defined in Step-2.

Here is the JavaScript code to achieve that:

$("#spline-chartcontainer").insertFusionCharts({
        type: "splinearea",
        width: "100%",
        height: "550",
        dataFormat: "json",
        dataSource: {
      "chart": {
        // caption options
        "caption": "Acme Holiday Club Weekly Visitors",
        "captionPadding": "32",
        "captionFontSize": "22",
        "captionFontColor": "#EEF3F5",
        
        // sub-caption options
        "subCaption": "Week 33 (2015)",
        "subCaptionFontSize": "16",
        
        // font cosmetics
        "baseFont": "Nunito",
        "baseFontColor": "#CFD0D2",
        "outCnvBaseFontColor": "#CFD0D2",
        "baseFontSize": "14",
        "outCnvBaseFontSize": "14",
        "labelDisplay": "Auto",
        
        // x-axis options
        "xAxisName": "Day",
        "xAxisNameFontColor": "#EEF3F5",
        "xAxisNameFontBold": "0",
        "xAxisNameFontSize": "16",
        "showXAxisLine": "0",
        
        // y-axis options
        "yAxisName": "No. of Visitors",
        "yAxisNameFontColor": "#EEF3F5",
        "yAxisNameFontBold": "0",
        "yAxisNameFontSize": "16",
        
        // chart margins and padding options
        "chartLeftMargin": "13",
        "chartRightMargin": "10",
        "chartTopMargin": "10",
        "chartBottomMargin": "13",
        "canvasPadding": "25",
        
        // tooltip options
        "toolTipPadding": "10",
        "toolTipBorderThickness": "1",
        "ToolTipBorderRadius": "2",
        "toolTipBorderAlpha": "60",
        "toolTipBgColor": "#000000",
        "toolTipBorderColor": "#FFFFFF",
        "toolTipBgAlpha": "60",
        "toolTipSepChar": ": ",
        "toolTipColor": "#FFFFFF",
        
        // anchor cosmetics
        "drawAnchors": "1",
        "anchorRadius": "6",
        "anchorHoverEffect": "1",
        "anchorBgColor": "#00FF00",
        //"anchorBorderColor": "#6AE6B3",
        "anchorBorderThickness": "3",
        "anchorTrackingRadius": "40",
        "anchorHoverRadius": "8",
        
        // other chart options
        "showBorder": "0",
        "formatNumberScale": "0",
        "showValues": "0",
        "bgColor": "#3E414A",
        "bgAlpha": "100",
        "canvasBgColor": "3E414A",
        "canvasBgAlpha": "0",
        "showCanvasBorder": "0",
        "showAlternateHGridColor": "0",
        "paletteColors": "#46C28C",
        "usePlotGradientColor": "0"
      },

      "data": [{
        "label": "Monday",
        "value": "1200"
      }, {
        "label": "Tuesday",
        "value": "1050"
      }, {
        "label": "Wednesday",
        "value": "900"
      }, {
        "label": "Thursday",
        "value": "1200"
      }, {
        "label": "Friday",
        "value": "950"
      }, {
        "label": "Saturday",
        "value": "1350"
      }, {
        "label": "Sunday",
        "value": "1450"
      }]
    }
});


type
attribute defines the chart type we are going to plot – splinearea in this example. Dimension of the chart is defined using height and width attributes. chart object under dataSource contains chart configuration options like caption, background color, data plot color and display formats for numbers etc. data array contains the data being plotted in the chart. To learn about customizing a chart’s look and feel, you can visit this documentation page.

That’s it. That is all you need to make a dynamic chart in jQuery. If you followed everything up to this point correctly, you should have a working chart with you. But if there was any problem with your code, you can download the full source-code for the chart here.

More Resources on jQuery Charts

My objective with this tutorial was to get you started with making charts in jQuery for your website. But this is just the tip of the iceberg, and there is a lot more you can do with it. If you are interested in exploring further, check out following resources:

  • Render charts directly from an HTML table. If you have some data present in HTML table, you can use convertToFusionCharts method to make a chart out of that.
  • Get better control over the chart using events. There are a lot of possibilities to add event handlers for adding advanced interactions on your chart.
Link

Jump Start with Python Tuples

Tuples are immutable lists. In Python, tuples can be written as comma separated values (items) enclosed between parentheses irrespective of their type. Tuples are similar to lists only difference is that it cannot be changed in any way once it is created. This is example of simple tuple in Python:

Tuples Basic Example
Tuples Basic Example

Creating Tuples

It is very easy to create a tuple in Python, just enclose comma separated values (items) between parentheses.
Syntax: tuple_name=(item1, item2, item3, ---- , itemn)

Some facts about tuples:

  • Declaring zero and one element tuple:If empty parentheses are assigned to a variable then it is termed as zero element tuple.
    Syntax: tuple_name=()

    One element tuple is declared in Python using the following syntax:
    Syntax: tuple_name=("item1", )

  • In Python any set of elements which are declared as comma separated values (items) are considered as tuples.
  • Indexes for tuples start from 0. Positive indexes start from left to right while negative indexes start from right to left.
  • Python also supports slicing index in Tuples which allows accessing multiple elements of tuple at once. Below is an example for slicing in tuples.

Basic Operations in Tuples

  • Packing & Unpacking
  • Deletion Operation
  • Addition Operation
  • Multiplication Operation
  • Iteration Operation
  • Membership Operation

Tuple Methods

  • Length Method
  • Convert to Tuple Method
  • Maximum Method
  • Minimum Method
  • Membership Operation

My article “Jump Start with Python – Part 7 (Tuples)” is the seventh article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start with Python Numbers – Part 6 (Lists)“, Click Here

Link

Jump Start with Python Lists

The most basic data structure in Python is sequence. A sequence is an abstract data type that represents an ordered sequence of values (items), where the same value may occur more than once. Python has six built-in types of sequences but among them most common are lists and tuples.
List is the most versatile data type in Python, It can also be termed as “Python’s Workhorse Data Type“. Lists in Python can be written as a list of comma-separated values (items) enclosed between square brackets irrespective of their type.

Creating a List:

Syntax:
list_name=["item1", "item2", "item3", --------- , "itemn"]

Example:
fruits=["Mango", "Orange", "Banana", "Grapes", "Pomegranate", "Apple", "Pineapple"]

Python List Example

Accessing Lists:

Python allows accessing values of a list using list indices i.e. list element index which starts from 0. Multiple elements of list can be accessed using slicing range of elements from the list. Some other access operations performed on lists are as below:

  • Updating Lists
  • Deleting Lists
Basic List Operations:

List responds to almost all basic operations and some of them are listed as below:

  • Length Operation
  • Concatenation Operation
  • Repetition Operation
  • Membership Operation
  • Iteration Operation
List Methods & Functions:

  • Maximum Method
  • Minimum Method
  • Convert to List Method
  • Append Method
  • Count Method
  • Extend Method
  • Object Index Method
  • Index Insert Method
  • Remove Method
  • Pop Method
  • Sort Method
  • Reverse Method

My article “Jump Start with Python Lists – Part Six” is the sixth article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start with Python Numbers – Part 5“, Click Here

Link

Jump Start with Python Numbers – Part 5

Number is a mathematical object used to count, measure, label and manipulate. In Python, numbers are handled and manipulated using numeric data types. Python supports four different types of numeric data types and they are explained below:

  • int (Integers): They are positive or negative whole numbers with no decimal point.
  • long: They are also known as long integers of unlimited size. They are usually followed by postfix (l/L).
  • float: Floating point numbers represent real numbers and are written with decimal point dividing integer and fractional parts.
  • complex: Complex numbers are of the form (x + yJ), where x and y are floating point real values and J represents the square root of -1 i.e. an imaginary number.
Python Numbers Methods & Modules:
  • Numeric Data Type Detection Methods
  • Number System Prefix
  • Data Type Conversion
  • Python Decimals
  • Python Fractions
  • Methods on Integer types
  • Methods on Float types
Mathematical Functions & Constants:
  • PI
  • e (Exponential)
  • Absolute
  • Ceiling Value
  • Exponential Method
  • Absolute Method (fabs)
  • Floor Method
  • Natural Logarithm Method
  • Base-10 Logarithm Method
  • Max Method
  • Min Method
  • Modf Method
  • Power Method
  • Round Method
  • Square Root Method
  • Truncation Method
  • Copy Sign Method
  • IsNaN Method
  • Factorial Method
Random Functions:
  • Choice Method
  • Random Range Method
  • Random Method
  • Seed Method
  • Shuffle Method
  • Uniform Method
  • Get State Method
  • Random Bits Method
  • Random Integer Method
  • Sample Method
  • Triangular Method
  • Betavariate Method
  • Expovariate Method
  • Gammavariate Method
  • Normal Variate Method
  • Gauss Method
  • Log Normal Variate Method
  • Vonmises Variate Method
  • Pareto Variate Method
  • Weibull Variate Method
Trigonometric Functions:
  • Sine Method
  • Cosine Method
  • Tangent Method
  • Arc Sine Method
  • Arc Cosine Method
  • Arc Tangent Method
  • Arc Tangent Double Method
  • Euclidean Method
  • Degrees Method
  • Radians Method

My article “Jump Start with Python Numbers – Part 5” is the fifth article in series, and explains how to get started with programming in Python at C# Corner. Click the link below to read the article:

Read Article

To read “Jump Start with Python Strings – Part 4“, Click Here