Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 17 of 2547
Python program to change the value of a dictionary if it equals K
A dictionary is one of Python's most versatile data structures, storing data in key-value pairs using {} curly braces. Dictionary keys are unique, values can be repeated, and the structure is mutable, allowing modifications after creation. In this article, we'll explore different techniques to change dictionary values that equal a specific value K. Let's examine each approach with practical examples. Using a For Loop The for loop iterates through dictionary items and updates values when they match our target value K ? Example def change_value_if_equals_K(dictionary, K, new_value): for key, value ...
Read MorePython program to calculate gross pay
Gross pay is the total amount paid to an employee before taxes and deductions. It includes base salary, bonuses, overtime, and reimbursements. The basic formula is: Hours worked × Pay per hour = Gross pay For example, if an employee worked 40 hours at ₹500 per hour: 40 × 500 = ₹20, 000 gross pay Let's explore different methods to calculate gross pay using Python. Basic Gross Pay Calculation The simplest approach calculates gross pay using hours worked and hourly rate ? def calculate_gross_pay(hours, hourly_rate): ...
Read MorePython program to calculate square of a given number
A square is defined as the multiplication of a number by itself. The square of a number n is given as n2. Mathematically, we can represent the square of a number as: n² = n × n Python provides several approaches to calculate the square of a number. Let's explore the most common methods. Using Exponential Operator (**) The exponential operator (**) performs exponent arithmetic operations. To find the square, we raise the number to the power of 2. Syntax: n ** 2 Example Here we calculate the square of 25 using the ...
Read MorePython program to calculate Date, Month and Year from Seconds
When working with timestamps, you often need to convert seconds (Unix epoch time) into readable date components. Python provides several approaches using the datetime module, time module, or manual calculations with divmod(). Using datetime Module The datetime module offers classes to manipulate dates and times. The utcfromtimestamp() function converts seconds since epoch (January 1, 1970) into a datetime object. Syntax datetime.datetime.utcfromtimestamp(seconds) Example Here we convert 1706472809 seconds to extract the day, month, and year components ? import datetime def calculate_date(seconds): date = datetime.datetime.utcfromtimestamp(seconds) ...
Read MorePython program to calculate age in year
Generally, the age of a person is calculated by subtracting the birth year from the current year. In Python, we can calculate age more precisely using modules like datetime, dateutil, and timedelta to handle dates properly. Basic Age Calculation The simplest approach uses basic arithmetic ? Current year - Birth year Example This example implements the basic method by passing current year and birth year to a function ? def age_calculator(current_year, birth_year): age = current_year - birth_year print("The age of the person in ...
Read MorePython program to calculate acceleration, final velocity, initial velocity and time
Acceleration, final velocity, initial velocity and time are fundamental concepts in physics that describe motion and mechanics. Python can help us calculate these values using their mathematical relationships. Acceleration Acceleration is the rate at which an object changes its velocity over time. It is denoted by a and measured in meters per second squared (m/s²). The formula is: a = (vf - vi) / t Where: vi is the initial velocity vf is the final velocity t is the time duration a is the acceleration Example Let's calculate acceleration when an ...
Read MoreProfiling in Python
Python profiling is the process of measuring performance of different parts of a program to identify optimization areas and bottlenecks. Python provides several built-in modules and third-party tools for profiling code execution time, memory usage, and function calls. Using cProfile for Function Profiling Function profiling measures execution time of individual functions in your program. Python's built-in cProfile module is the most common tool for this purpose. Basic Function Profiling The cProfile.run() function executes code and provides detailed statistics − import cProfile def calculate_sum(n): total = 0 ...
Read MoreProfile Application using Python Flask and MySQL
Flask is a lightweight web framework for Python that provides libraries to build web applications quickly. It is a micro framework developed by Armin Ronacher, working on WSGI toolkit and Jinja2 template engines. This tutorial will guide you through creating a profile registration application using Flask and MySQL. Setting up Virtual Environment First, install the virtual environment to isolate your project dependencies − pip install virtualenv Create a new project directory and set up the virtual environment − mkdir Flask cd Flask virtualenv venv Activate the virtual environment based on ...
Read MorePriority Queue using Queue and Heapdict module in Python
A priority queue is an abstract data type similar to a regular queue, but each element has an associated priority that determines the order of removal. Elements with higher priority are dequeued before those with lower priority. Priority queues are commonly implemented using heaps, arrays, or balanced trees. The most efficient implementation uses a heap, which is a binary tree where each node's value is greater than or equal to its children's values. Types of Priority Queues There are two main types of priority queues ? Min Priority Queue − Elements with lower priority values ...
Read MoreShow Normal Inverse Gaussian Distribution in Statistics using Python
The Normal Inverse Gaussian (NIG) distribution is a continuous probability distribution commonly used in finance and risk management. It's characterized as a normal variance-mean mixture with the inverse Gaussian distribution as the mixing density. Understanding NIG Distribution The NIG distribution has four parameters: alpha (α) − Controls the steepness of the distribution beta (β) − Controls the asymmetry (skewness) mu (μ) − Location parameter (mean) delta (δ) − Scale parameter Implementing NIG Distribution We'll use Python's scipy.stats.norminvgauss to create and visualize the distribution ? import numpy as np import matplotlib.pyplot as ...
Read More