Python Program to swap two numbers without using third variable

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:58:57

873 Views

Swapping the values of two variables is a common operation in programming. Typically, the swap is performed using a third variable to temporarily store one of the values. However, there are several clever techniques to swap two numbers without using an additional variable, which can be useful for memory optimization or in constrained environments. In this article, we will explore different Python approaches to swap two numbers without using a third variable, including arithmetic operations and bitwise XOR operations. Method 1: Using Arithmetic Operations (Addition and Subtraction) This method uses basic arithmetic to swap values ? ... Read More

Python Program to swap the First and the Last Character of a string

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:58:31

3K+ Views

In this article, we will explore a Python program to swap the first and last character of a string. Swapping characters within a string can be a useful operation in various scenarios, such as data manipulation, text processing, or even string encryption. We will discuss different approaches to solve this problem efficiently using Python, provide step-by-step implementations, and include test cases to validate the program's functionality. Understanding the Problem Before we dive into solving the problem, let's define the requirements and constraints more explicitly. Problem Statement − We need to write a Python program that swaps ... Read More

Filtering a PySpark dataframe using isin by Exclusion

Jaisshree
Updated on 27-Mar-2026 11:58:04

2K+ Views

PySpark DataFrames are distributed collections of data organized into named columns, similar to tables in a database. When working with large datasets, you often need to filter out specific rows based on whether column values match a predefined list. The isin() function combined with the negation operator (~) provides an efficient way to exclude rows by filtering out unwanted values. Understanding isin() Function The isin() function checks whether DataFrame values are present in a list of values. It returns a boolean result − True if the column value exists in the provided list, False otherwise. Syntax ... Read More

Python program to Swap Keys and Values in Dictionary

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:57:36

6K+ Views

Dictionaries are a fundamental data structure in Python, providing a way to store key-value pairs. Sometimes you need to reverse the roles of keys and values in a dictionary. This operation is useful for data restructuring, creating value-based lookups, or removing duplicates. In this article, we will explore different methods to swap keys and values in a Python dictionary with practical examples. Understanding the Problem Swapping keys and values in a dictionary means creating a new dictionary where the original values become keys and the original keys become values. This is useful in scenarios such as: ... Read More

Filter Pandas DataFrame Based on Index

Jaisshree
Updated on 27-Mar-2026 11:57:12

686 Views

Pandas DataFrame filtering based on index is a fundamental operation for data analysis. The filter() method and boolean indexing provide flexible ways to select specific rows and columns based on their index labels. Syntax df.filter(items=None, like=None, regex=None, axis=None) Parameters items: List of labels to keep. Returns only rows/columns with matching names. like: String pattern. Keeps labels containing this substring. regex: Regular expression pattern for matching labels. axis: 0 for rows, 1 for columns. Default is None (columns). Filtering by Numeric Index Positions Use iloc[] to filter rows by their ... Read More

Python Program to Swap dictionary item_s position

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:56:47

329 Views

Dictionaries in Python are versatile data structures that store key-value pairs. Sometimes we need to swap the values of two keys within a dictionary. This article demonstrates different approaches to swap dictionary item values efficiently. Understanding Dictionary Value Swapping When we swap dictionary items, we exchange the values associated with two specific keys. For example, given {'A': 1, 'B': 2, 'C': 3}, swapping values of keys 'A' and 'B' results in {'A': 2, 'B': 1, 'C': 3}. Method 1: Using Tuple Unpacking The most Pythonic way uses simultaneous assignment with tuple unpacking ? def ... Read More

FileExtensionValidator – Validate File Extensions in Django

Jaisshree
Updated on 27-Mar-2026 11:56:21

1K+ Views

Django's FileExtensionValidator is a built-in validator that ensures uploaded files have specific extensions. This validator helps maintain security and data integrity by preventing unwanted file types from being uploaded to your application. What is FileExtensionValidator? The FileExtensionValidator allows you to specify which file extensions are allowed or forbidden for FileField and ImageField uploads. It's particularly useful for controlling file types in forms and preventing security vulnerabilities. Basic Usage Here's how to use FileExtensionValidator in your Django models ? from django.db import models from django.core.validators import FileExtensionValidator class Document(models.Model): # ... Read More

Python Program to Square Each Odd Number in a List using List Comprehension

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:55:56

1K+ Views

List comprehension is a powerful feature in Python that allows for concise and expressive code when working with lists. It provides a compact way to perform operations on elements and create new lists based on certain conditions. In this tutorial, we'll explore how to square each odd number in a list while keeping even numbers unchanged. Understanding the Problem We need to write a Python program that takes a list of numbers and squares only the odd numbers. For example, given the list [1, 2, 3, 4, 5], the program should return [1, 2, 9, 4, 25], where ... Read More

Python Program to split string into k sized overlapping strings

Mrudgandha Kulkarni
Updated on 27-Mar-2026 11:55:31

647 Views

Splitting a string into smaller overlapping parts is a common task in text processing, data analysis, and pattern recognition. In this tutorial, we'll explore how to write a Python program that splits a given string into k-sized overlapping strings. Understanding the Problem We need to create overlapping substrings of fixed size k from a given string. For example, if we have the string "Hello" and k=3, we want to generate: "Hel", "ell", "llo". Each substring has length k and starts one position after the previous substring, creating an overlap of k-1 characters. Basic Implementation Here's ... Read More

Fibonacci Search Visualizer using PyQt5

Jaisshree
Updated on 27-Mar-2026 11:55:02

388 Views

The Fibonacci Search is an efficient algorithm for searching sorted arrays using Fibonacci numbers. Here, we'll create a visual demonstration of this algorithm using PyQt5 to help understand how it divides the search space. How Fibonacci Search Works The algorithm uses Fibonacci numbers to divide the sorted array into unequal parts, similar to binary search but with golden ratio proportions. It finds two consecutive Fibonacci numbers that are greater than or equal to the array length. Algorithm Steps Step 1: Find the smallest Fibonacci number greater than or equal to the array length. Step 2: ... Read More

Advertisements