Python json basics

Introduction of JSON in Python :
The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted string which contains value in key-value mapping within { }. It is similar to the dictionary in Python. JSON shows an API similar to users of Standard Library marshal and pickle modules and Python natively supports JSON features.

Python program showing
 use of json package
 import json 
 {key:value mapping}
 a ={"name":"John", 
 "age":31, 
     "Salary":25000} 
 conversion to JSON done by dumps() function
 b = json.dumps(a) 
 printing the output
 print(b) 

Output:

{"age": 31, "Salary": 25000, "name": "John"}

As you can see, JSON supports primitive types, like strings and numbers, as well as nested lists, tuples and objects.

Python program showing that
 json support different primitive
 types
 import json 
 list conversion to Array
 print(json.dumps(['Welcome', "to", "GeeksforGeeks"])) 
 tuple conversion to Array
 print(json.dumps(("Welcome", "to", "GeeksforGeeks"))) 
 string conversion to String
 print(json.dumps("Hi")) 
 int conversion to Number
 print(json.dumps(123)) 
 float conversion to Number
 print(json.dumps(23.572)) 
 Boolean conversion to their respective values
 print(json.dumps(True)) 
 print(json.dumps(False)) 
 None value to null
 print(json.dumps(None)) 

Output:

["Welcome", "to", "GeeksforGeeks"]
["Welcome", "to", "GeeksforGeeks"]
"Hi"
123
23.572
true
false
null

Python django basics

Django is a Python-based web framework which allows you to quickly create web application without all of the installation or dependency problems that you normally will find with other frameworks.
When you’re building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website, forms, a way to upload files, etc. Django gives you ready-made components to use.

django-basics

Why Django?

  • Django is a rapid web development framework that can be used to develop fully fleshed web applications in a short period of time.
  • It’s very easy to switch database in Django framework.
  • It has built-in admin interface which makes easy to work with it.
  • Django is fully functional framework that requires nothing else.
  • It has thousands of additional packages available.
  • It is very scalable. For more visit When to Use Django? Comparison with other Development Stacks ?

Django architecture

Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application.

MVT Structure has the following three parts –

Model: Model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical data structure behind the entire application and is represented by a database (generally relational databases such as MySql, Postgres).

Python pandas basics

Pandas is an open-source library that is made mainly for working with relational or labeled data both easily and intuitively. It provides various data structures and operations for manipulating numerical data and time series. This library is built on the top of the NumPy library. Pandas is fast and it has high-performance & productivity for users.

Table of Content

  • History
  • Advantages
  • Getting Started
    • Series
    • DataFrame
  • Why Pandas is used for Data Science

History

Pandas was initially developed by Wes McKinney in 2008 while he was working at AQR Capital Management. He convinced the AQR to allow him to open source the Pandas. Another AQR employee, Chang She, joined as the second major contributor to the library in 2012. Over the time many versions of pandas have been released. The latest version of the pandas is 1.0.1

Advantages

  • Fast and efficient for manipulating and analyzing data.
  • Data from different file objects can be loaded.
  • Easy handling of missing data (represented as NaN) in floating point as well as non-floating point data
  • Size mutability: columns can be inserted and deleted from DataFrame and higher dimensional objects
  • Data set merging and joining.
  • Flexible reshaping and pivoting of data sets
  • Provides time-series functionality.
  • Powerful group by functionality for performing split-apply-combine operations on data sets.

Getting Started

After the pandas has been installed into the system, you need to import the library. This module is generally imported as –

import pandas as pd

Here, pd is referred to as an alias to the Pandas. However, it is not necessary to import the library using alias, it just helps in writing less amount of code everytime a method or property is called.

Python numpy basics

Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python.
Besides its obvious scientific uses, Numpy can also be used as an efficient multi-dimensional container of generic data.

Arrays in Numpy

Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array.A tuple of integers giving the size of the array along each dimension is known as shape of the array. An array class in Numpy is called as ndarray. Elements in Numpy arrays are accessed by using square brackets and can be initialized by using nested Python Lists.

Creating a Numpy Array
Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array. Arrays can also be created with the use of various data types such as lists, tuples, etc. The type of the resultant array is deduced from the type of the elements in the sequences.
Note: Type of array can be explicitly defined while creating the array.

<code># Python program for</code>
<code># Creation of Arrays</code>
<code>import</code> <code>numpy as np</code>
 
<code># Creating a rank 1 Array</code>
<code>arr =</code> <code>np.array([1, 2, 3])</code>
<code>print("Array with Rank 1: \n",arr)</code>
 
<code># Creating a rank 2 Array</code>
<code>arr =</code> <code>np.array([[1, 2, 3],</code>
<code>[4, 5, 6]])</code>
<code>print("Array with Rank 2: \n", arr)</code>
 
<code># Creating an array from tuple</code>
<code>arr =</code> <code>np.array((1, 3, 2))</code>
<code>print("\nArray created using "</code>
<code>"passed tuple:\n", arr)</code>

Output:

Array with Rank 1: 
 [1 2 3]
Array with Rank 2: 
 [[1 2 3]
 [4 5 6]]

Array created using passed tuple:
 [1 3 2]

 
Accessing the array Index
In a numpy array, indexing or accessing the array index can be done in multiple ways. To print a range of an array, slicing is done. Slicing of an array is defining a range in a new array which is used to print a range of elements from the original array. Since, sliced array holds a range of elements of the original array, modifying content with the help of sliced array modifies the original array content.

<code># Python program to demonstrate</code>
<code># indexing in numpy array</code>
<code>import</code> <code>numpy as np</code>
 
<code># Initial Array</code>
<code>arr =</code> <code>np.array([[-1, 2, 0, 4],</code>
<code>[4, -0.5, 6, 0],</code>
<code>[2.6, 0, 7, 8],</code>
<code>[3, -7, 4, 2.0]])</code>
<code>print("Initial Array: ")</code>
<code>print(arr)</code>
 
<code># Printing a range of Array</code>
<code># with the use of slicing method</code>
<code>sliced_arr =</code> <code>arr[:2, ::2]</code>
<code>print</code> <code>("Array with first 2 rows and"</code>
<code>" alternate columns(0 and 2):\n", sliced_arr)</code>
 
<code># Printing elements at</code>
<code># specific Indices</code>
<code>Index_arr =</code> <code>arr[[1, 1, 0, 3], </code>
<code>[3, 2, 1, 0]]</code>
<code>print</code> <code>("\nElements at indices (1, 3), "</code>
<code>"(1, 2), (0, 1), (3, 0):\n", Index_arr)</code>

Output:

Initial Array: 
[[-1.   2.   0.   4. ]
 [ 4.  -0.5  6.   0. ]
 [ 2.6  0.   7.   8. ]
 [ 3.  -7.   4.   2. ]]
Array with first 2 rows and alternate columns(0 and 2):
 [[-1.  0.]
 [ 4.  6.]]

Elements at indices (1, 3), (1, 2), (0, 1), (3, 0):
 [ 0. 54.  2.  3.]

Python collections basics

Counter is a container included in the collections module. Now you all must be wondering what is a container. Don’t worry first let’s discuss about the container.

What is Container?

Containers are objects that hold objects. They provide a way to access the contained objects and iterate over them. Examples of built in containers are Tuple, list, and dictionary. Others are included in Collections module.

A Counter is a subclass of dict. Therefore it is an unordered collection where elements and their respective count are stored as a dictionary. This is equivalent to a bag or multiset of other languages.

Syntax :

class collections.Counter([iterable-or-mapping])

Initialization :
The constructor of counter can be called in any one of the following ways :

With sequence of itemsWith dictionary containing keys and countsWith keyword arguments mapping string names to counts.

OrderedDict in Python

An OrderedDict is a dictionary subclass that remembers the order that keys were first inserted. The only difference between dict() and OrderedDict() is that:

OrderedDict preserves the order in which the keys are inserted. A regular dict doesn’t track the insertion order, and iterating it gives the values in an arbitrary order. By contrast, the order the items are inserted is remembered by OrderedDict.

A Python program to demonstrate working of OrderedDict
 from collections import OrderedDict 
 print("This is a Dict:\n") 
 d = {} 
 d['a'] = 1
 d['b'] = 2
 d['c'] = 3
 d['d'] = 4
 for key, value in d.items(): 
     print(key, value) 
 print("\nThis is an Ordered Dict:\n") 
 od = OrderedDict() 
 od['a'] = 1
 od['b'] = 2
 od['c'] = 3
 od['d'] = 4
 for key, value in od.items(): 
     print(key, value) 

Output:

This is a Dict:
('a', 1)
('c', 3)
('b', 2)
('d', 4)

This is an Ordered Dict:
('a', 1)
('b', 2)
('c', 3)
('d', 4)

Important Points:

  1. Key value Change: If the value of a certain key is changed, the position of the key remains unchanged in OrderedDict.
A Python program to demonstrate working of key
 value change in OrderedDict
 from collections import OrderedDict 
 print("Before:\n") 
 od = OrderedDict() 
 od['a'] = 1
 od['b'] = 2
 od['c'] = 3
 od['d'] = 4
 for key, value in od.items(): 
     print(key, value) 
 print("\nAfter:\n") 
 od['c'] = 5
 for key, value in od.items(): 
     print(key, value) 
Before: ('a', 1) ('b', 2) ('c', 3) ('d', 4) 
After: ('a', 1) ('b', 2) ('c', 5) ('d', 4)

Other Considerations:

  • Ordered dict in Python version 2.7 consumes more memory than normal dict. This is due to the underlying Doubly Linked List implementation for keeping the order. In Python 2.7 Ordered Dict is not dict subclass, it’s a specialized container from collections module.
  • Starting from Python 3.7, insertion order of Python dictionaries is guaranteed.
  • Ordered Dict can be used as a stack with the help of popitem function. Try implementing LRU cache with Ordered Dict.

Python regex basics

Regular Expression in Python with Examples | Set 1

Last Updated: 19-10-2020

Module Regular Expressions(RE) specifies a set of strings(pattern) that matches it. 
To understand the RE analogy, MetaCharacters are useful, important and will be used in functions of module re. 
There are a total of 14 metacharacters and will be discussed as they follow into functions: 

\   Used to drop the special meaning of character
    following it (discussed below)
[]  Represent a character class
^   Matches the beginning
$   Matches the end
.   Matches any character except newline
?   Matches zero or one occurrence.
|   Means OR (Matches with any of the characters
    separated by it.
*   Any number of occurrences (including 0 occurrences)
+   One or more occurrences
{}  Indicate number of occurrences of a preceding RE 
    to match.
()  Enclose a group of REs



  • Function compile() 
    Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. 
     
#Module Regular Expression is imported using <strong>import</strong>().
 import re 
# compile() creates regular expression character class [a-e],
# which is equivalent to [abcde].
# class [abcde] will match with string with 'a', 'b', 'c', 'd', 'e'.
 p = re.compile('[a-e]') 
# findall() searches for the Regular Expression and return a list upon finding
 print(p.findall("Aye, said Mr. Gibenson Stark")) 

Output: 

['e', 'a', 'd', 'b', 'e', 'a']




Understanding the Output: 
First occurrence is ‘e’ in “Aye” and not ‘A’, as it being Case Sensitive. 
Next Occurrence is ‘a’ in “said”, then ‘d’ in “said”, followed by ‘b’ and ‘e’ in “Gibenson”, the Last ‘a’ matches with “Stark”.
Metacharacter backslash ‘\’ has a very important role as it signals various sequences. If the backslash is to be used without its special meaning as metacharacter, use’\\’

\d   Matches any decimal digit, this is equivalent
     to the set class [0-9].
\D   Matches any non-digit character.
\s   Matches any whitespace character.
\S   Matches any non-whitespace character
\w   Matches any alphanumeric character, this is
     equivalent to the class [a-zA-Z0-9_].
\W   Matches any non-alphanumeric character. 

Exception handling in c++ and python

Exception handling exists in both C++ and Python, but the style and ergonomics are different. Understanding those differences helps engineers write cleaner and more predictable code when working across multiple languages.

The Shared Idea

In both languages, exceptions represent runtime problems that interrupt the normal flow of execution. Instead of returning error codes everywhere, a function can raise an exception and let another part of the program decide how to respond.

Python Example

def divide(a, b):
    if b == 0:
        raise ValueError("Division by zero")
    return a / b

try:
    print(divide(10, 0))
except ValueError as exc:
    print(exc)

C++ Example

#include <iostream>
#include <stdexcept>
using namespace std;

int divide(int a, int b) {
    if (b == 0) {
        throw runtime_error("Division by zero");
    }
    return a / b;
}

int main() {
    try {
        cout << divide(10, 0) << endl;
    } catch (const exception& e) {
        cout << e.what() << endl;
    }
}

The Practical Difference

Python makes exception handling feel lightweight and natural for many application tasks. C++ gives more control, but also requires more discipline around object lifetime, destructors, and performance-sensitive design.

Good Practices in Both Languages

  • Do not hide important failures silently.
  • Raise or throw meaningful exception types.
  • Catch errors where you can actually recover or add useful context.
  • Keep normal control flow separate from exceptional failure flow.

Final Thoughts

Exception handling is not about making code look advanced. It is about making failure understandable and manageable. Whether you write in C++ or Python, good error handling is part of writing reliable software.

Python projects worth building

Learning Python becomes much easier when you build things that solve real problems. Tutorials help, but projects are what turn syntax into skill. The best beginner and intermediate projects are small enough to finish and useful enough to teach good habits.

Project Ideas That Are Worth Building

1. Log Analyzer

Read a log file, count error messages, group them by level, and export a short report. This teaches file I/O, loops, string processing, and simple data structures.

2. API Data Fetcher

Call a public API, store selected fields, and print a summary. This teaches HTTP requests, JSON parsing, and error handling.

import requests

response = requests.get("https://api.github.com/repos/python/cpython")
data = response.json()
print(data["stargazers_count"])

3. CSV Cleaner

Take messy CSV input, normalize column names, remove invalid rows, and write a cleaned file. This is very close to real engineering work.

4. Deployment Helper Script

Build a CLI tool that runs tests, tags a release, or validates environment variables before deployment.

What Good Projects Teach

  • how to structure code into functions,
  • how to handle bad input,
  • how to log useful information,
  • and how to make tools that others can actually use.

A Good Rule

Do not chase huge project ideas too early. A finished 150-line script that solves one real problem is often more valuable than a half-built web app that never works.

Final Thoughts

If you want to learn Python seriously, build small tools with clear outcomes. Practical projects create momentum, and momentum is what turns learning into engineering ability.

Python for engineers

Python is one of the most practical languages you can learn as an engineer. It is easy to read, productive for day-to-day work, and flexible enough for automation, backend services, machine learning, testing, and DevOps tasks.

Why Python Is So Useful

  • It has simple syntax, so you can focus on the problem instead of fighting the language.
  • It has a huge ecosystem for web, data, AI, testing, and scripting.
  • It is excellent for glue code between services, files, APIs, and command-line tools.

A Small Example

The following script reads a log file and counts how many lines contain the word ERROR.

from pathlib import Path

log_path = Path("app.log")
error_count = 0

for line in log_path.read_text().splitlines():
    if "ERROR" in line:
        error_count += 1

print(f"Found {error_count} error lines")

This is a good example of why Python is popular. The code is short, readable, and immediately useful.

Common Use Cases

1. Automation

Python is great for repetitive tasks such as renaming files, processing CSV data, calling REST APIs, or sending reports by email.

import requests

response = requests.get("https://api.github.com")
print(response.status_code)

2. Data Processing

You can parse text, clean records, and transform data before loading it into another system.

3. Backend Development

Frameworks like Flask, FastAPI, and Django make Python a strong option for internal tools and web services.

4. Machine Learning

Libraries such as NumPy, pandas, scikit-learn, PyTorch, and TensorFlow make Python a default choice in many AI workflows.

Tips for Writing Better Python

  • Use virtual environments to keep dependencies isolated.
  • Write small functions with clear names.
  • Handle exceptions only where you can act on them.
  • Use type hints when they improve readability.
  • Add tests for scripts that affect production systems.

Final Thoughts

If you want one language that helps with automation, prototyping, and real production work, Python is hard to beat. It is not only beginner-friendly. It is genuinely powerful for experienced engineers as well.

Perceptron on the iris dataset in python

The Iris dataset is one of the best beginner examples for understanding the perceptron. It is small, well known, and easy to visualize. That makes it a practical way to see how a linear classifier learns from real feature values rather than only from toy Boolean inputs.

In this article, we use the Iris dataset to train a perceptron in Python and explain what the result actually teaches. The goal is not only to show code. The goal is to understand why this dataset works well as a first perceptron example.

What you will learn

  • why the Iris dataset is a good starting point
  • how to prepare a binary classification task for the perceptron
  • how the training loop works on real feature data
  • what to expect from the result and where the model starts to struggle

Why the Iris dataset is useful here

Scikit-learn’s Iris example describes the dataset as 150 samples with four features across three iris species. For a beginner, that is perfect because the data is simple enough to inspect while still being real tabular classification data.

A common first step is to simplify the task into a binary classification problem. For example, you can choose two classes and focus on two features such as petal length and petal width. That keeps the geometry easy to visualize and matches the perceptron’s linear nature.

Preparing the data

from sklearn.datasets import load_iris
import numpy as np

iris = load_iris()
X = iris.data[:, [2, 3]]  # petal length and petal width
y = iris.target

# keep only two classes for a binary perceptron example
mask = y < 2
X = X[mask]
y = y[mask]

This transforms the classic three-class dataset into a much cleaner binary problem that a single perceptron can handle more naturally.

Training a perceptron

You can train either a scratch implementation or the scikit-learn version. The scratch route is best for intuition. The scikit-learn route is best when you want a fast verified baseline.

from sklearn.linear_model import Perceptron

model = Perceptron(max_iter=1000, tol=1e-3, random_state=42)
model.fit(X, y)
predictions = model.predict(X)

Scikit-learn’s documentation notes that its perceptron classifier is implemented as a wrapper around `SGDClassifier` with a perceptron loss and constant learning rate. That is useful context because it shows the historical model inside a modern linear-learning framework.

What the result means

If you choose two well-separated classes and helpful features, the perceptron often performs very well on this simplified Iris task. That result should not be read as “the perceptron solves general machine learning.” It should be read more carefully:

  • the problem has been simplified into a binary task
  • the selected features support a fairly clean separation
  • the perceptron succeeds because the geometry is favorable

This is exactly why Iris is a teaching dataset. It helps you see when a linear classifier is a good fit.

What to inspect during training

When working through this example, pay attention to:

  • which two classes you selected
  • which two features you used
  • whether the points look roughly linearly separable
  • how stable the predictions become after training

If you change the task to something less separable, the perceptron can struggle. That is not surprising. It is the same structural limitation discussed in Why perceptrons fail on xor.

Why this example is worth keeping on the site

The Iris article is a strong supporting piece in the Perceptron cluster because it connects theory to data. The pillar article Perceptron explained for beginners teaches the concept. This article shows the concept on a familiar dataset. Together, they make the topic much easier to trust and understand.

Common mistakes or limitations

  • using all three Iris classes and expecting a simple binary explanation
  • not checking whether the chosen features are linearly separable enough
  • treating a clean toy result as proof that the model is broadly strong
  • confusing dataset convenience with real-world robustness

Key takeaways

  • The Iris dataset is a strong beginner example for the perceptron because it is small and interpretable.
  • A binary subset with suitable features fits the perceptron especially well.
  • The example teaches when a linear classifier works, not that it works everywhere.

Next steps

References