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
Server Side Programming Articles
Page 63 of 2109
Python program to test for Non-neighbours in List
When working with lists in Python, it can be valuable to identify non-neighbors, which are elements that are not adjacent to each other. Whether it's finding elements that are at least a certain distance apart or identifying gaps in a sequence, the ability to test for non-neighbors can provide valuable insights and facilitate specific operations. In this article, we will explore a Python program that tests for non-neighbors in a list. We will discuss the importance of identifying non-neighbors in various scenarios and provide a step-by-step explanation of the approach and algorithm used. Understanding the Problem Before ...
Read MorePython Program to swap two numbers without using third variable
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 MorePython Program to swap the First and the Last Character of a string
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 MoreFiltering a PySpark dataframe using isin by Exclusion
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 MorePython program to Swap Keys and Values in Dictionary
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 MoreFilter Pandas DataFrame Based on Index
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 MorePython Program to Swap dictionary item_s position
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 MoreFileExtensionValidator – Validate File Extensions in Django
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 MorePython Program to Square Each Odd Number in a List using List Comprehension
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 MorePython Program to split string into k sized overlapping strings
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