Skip to content

Instantly share code, notes, and snippets.

@Visnusah
Created January 1, 2025 07:23
Show Gist options
  • Save Visnusah/5c5835466521d1e9ae4e3d2680f86695 to your computer and use it in GitHub Desktop.
Save Visnusah/5c5835466521d1e9ae4e3d2680f86695 to your computer and use it in GitHub Desktop.
TeamWarica
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
@anandkanishkZ
Copy link

TO extract full name to first name and last name
image

@Visnusah
Copy link
Author

Visnusah commented Jan 3, 2025

Python Library Examples: Pandas, Numpy, Matplotlib, and Seaborn

This script demonstrates how to use popular Python libraries for data manipulation, numerical computation, and data visualization. It covers pandas, numpy, matplotlib, and seaborn in a single file.

Script Overview

The script includes examples of:

  1. Creating and displaying a DataFrame using pandas.
  2. Working with numerical arrays using numpy.
  3. Plotting a simple line graph using matplotlib.
  4. Creating a styled statistical plot using seaborn.

Full Code Example

# Example for pandas
import pandas as pd
from tabulate import tabulate  # For pretty printing of tables

data = pd.DataFrame({
    'Name': ['John', 'Anna', 'Peter', 'Linda'],
    'Age': [28, 24, 35, 32],
    'Country': ['USA', 'UK', 'Australia', 'Germany']
})
print(tabulate(data, headers='keys', tablefmt='grid'))  # Printing the DataFrame as a table

Example for numpy

import numpy as np  # Importing numpy library for numerical computing

arr = np.array([1, 2, 3, 4, 5])  # Creating a numpy array from a list
print(arr)  # Printing the numpy array

Example for matplotlib

import matplotlib.pyplot as plt  # Importing matplotlib for plotting

plt.plot([1, 2, 3, 4])  # Plotting a simple line graph
plt.ylabel('some numbers')  # Labeling the y-axis
plt.show()  # Displaying the plot

Example for seaborn

import seaborn as sns  # Importing seaborn for making statistical graphics

sns.set()  # Setting the seaborn style
sns.set_context("talk")  # Setting the context for the plot
sns.set_palette("colorblind")  # Setting the color palette
sns.set_style("whitegrid")  # Setting the style of the plot
plt.figure(figsize=(10, 6))  # Setting the size of the figure
sns.lineplot(x=[1, 2, 3, 4], y=[1, 4, 9, 16])  # Plotting a line graph
plt.show()  # Displaying the plot

Installation

  • Make sure to have Python installed on your system. Install the required libraries using pip:
pip install pandas numpy matplotlib seaborn tabulate

Libraries Used
pandas: For creating and manipulating data.
numpy: For numerical computations.
matplotlib: For creating static, interactive, and animated visualizations.
seaborn: For statistical data visualization.
tabulate: For formatting and displaying tabular data.
Purpose
This script provides a quick overview of working with Python libraries commonly used in data science and analytics. It is suitable for beginners and can be extended for advanced use cases.

Output
Pandas DataFrame: Displays a tabular dataset.
Numpy Array: Prints a numerical array.
Matplotlib Plot: Displays a simple line graph.
Seaborn Plot: Shows a styled statistical plot with custom configurations.

@Visnusah
Copy link
Author

Visnusah commented Jan 5, 2025

Exception Handling In Java

public class ExceptionHandling{
    public static void main(String[] args) {
        System.out.println("Start of program");

        // Attempting to divide by zero to demonstrate ArithmeticException
        try {
            int num1 = 100/0; // This line will throw ArithmeticException
            System.out.println("Value of num1: "+num1);
        } catch (ArithmeticException e) {
            System.out.println("ArithmeticException caught: " + e);
        }

        // try - catch - finally block to handle ArithmeticException
        try{
            int num2 = 100/0; // This line will throw ArithmeticException
            // If an exception occurs, the program will jump to the catch block
            // If an exception occurs, the following line will not be executed
            System.out.println("Value of num2: "+num2);
        }catch(ArithmeticException ex){
            // Catching ArithmeticException and printing the exception message
            System.out.println("ArithmeticException caught: " + ex);
        }

        // Array exception handling
        try
        {
            int[] numbers = {1, 2, 3, 4, 5};
            System.out.println(numbers[10]); // This line will throw ArrayIndexOutOfBoundsException
        }
        catch(ArrayIndexOutOfBoundsException ex)
        {
            // Catching ArrayIndexOutOfBoundsException and printing the exception message
            System.out.println("ArrayIndexOutOfBoundsException caught: " + ex);
        }

        // NULL pointer exception handling
        String name = null; // Setting a string to null to demonstrate NullPointerException
        try {
            {
                System.out.println(name.length()); // This line will throw NullPointerException
            }
        } catch (NullPointerException e) { 
            // Catching NullPointerException and printing the exception message
            System.out.println("NullPointerException caught: " + e);
        }

        // common Catch block to catch any type of exception
        try
        {
            int num2 = 100/0; // This line will throw ArithmeticException
        }
        catch (Exception e)
        { 
            // Catching any type of exception and printing the exception message
            System.out.println("Common Exception caught: " + e);
        }
        finally
        {
            // This block will always execute, regardless of whether an exception occurred or not
            System.out.println("Finally block executed");
        }

        // Multiple catch blocks to handle different types of exceptions
        // Attempting to access an array index that is out of bounds to demonstrate exception handling
        try{
            int[] numbers2 = {1, 2, 3, 4, 5};
            System.out.println(numbers2[10]); // This line will throw ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException error) {
            // Catching ArrayIndexOutOfBoundsException and printing the exception message
            System.out.println("ArrayIndexOutOfBoundsException caught: " + error);
        } catch(ArithmeticException error) {
            // Catching ArithmeticException and printing the exception message
            System.out.println("ArithmeticException caught: " + error);
        } catch (Exception error) {
            // Catching any other type of exception and printing the exception message
            System.out.println("Common Exception caught: " + error);
        }

        
        System.out.println("End of program");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment