Python Articles

Page 688 of 855

Roman to Integer in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 37K+ Views

Converting Roman numerals to integers is a common programming problem. Roman numerals use specific symbols with defined values, and some combinations follow subtraction rules. Roman Numeral Values The basic Roman numeral symbols and their integer values are ? Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 Subtraction Rules Some Roman numerals use subtraction instead of addition ? I can be placed before V (5) and X (10) ...

Read More

Two Sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 19K+ Views

The Two Sum problem is a classic coding challenge where you find two numbers in an array that add up to a specific target. Given an array of integers and a target sum, return the indices of the two numbers that add up to the target. For example, if the array is [2, 8, 12, 15] and the target is 20, the solution returns indices [1, 2] because 8 + 12 = 20. Algorithm Approach We use a hash map (dictionary) to store each number and its index as we iterate through the array. For each element, ...

Read More

Alternate range slicing in list (Python)

Yaswanth Varma
Yaswanth Varma
Updated on 25-Mar-2026 979 Views

Slicing is used to extract a portion of a sequence (such as a list, tuple, or string) or other iterable objects. Python provides a flexible way to perform slicing using the slice notation with optional start, stop, and step parameters. Syntax list[start:stop:step] What is Alternate Range Slicing? Alternate range slicing retrieves elements from a list by skipping elements at a fixed interval using the step parameter. For example, if we want every second element from a list, we set step=2. Basic Alternate Slicing Example 1: Every Second Element Extract every second ...

Read More

Boolean Indexing in Pandas

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 2K+ Views

Boolean indexing in Pandas allows you to select data from DataFrames using boolean vectors. This powerful feature lets you filter rows based on True/False values, either through boolean indices or by passing boolean arrays directly to the DataFrame. Creating a DataFrame with Boolean Index First, let's create a DataFrame with a boolean index vector − import pandas as pd # data data = { 'Name': ['Hafeez', 'Srikanth', 'Rakesh'], 'Age': [19, 20, 19] } # creating a DataFrame with boolean index vector data_frame = pd.DataFrame(data, index=[True, False, ...

Read More

Calendar Functions in Python -(monthrange(), prcal(), weekday()?)

Hafeezul Kareem
Hafeezul Kareem
Updated on 25-Mar-2026 657 Views

Python's calendar module provides useful functions for working with dates and calendars. We'll explore three key methods: monthrange(), prcal(), and weekday() for date calculations and calendar display. calendar.monthrange(year, month) The monthrange() method returns a tuple containing the starting weekday number (0=Monday, 6=Sunday) and the number of days in the specified month ? Example import calendar # Getting month information for January 2019 year = 2019 month = 1 weekday, no_of_days = calendar.monthrange(year, month) print(f'First weekday of the month: {weekday}') print(f'Number of days: {no_of_days}') # Let's also check what weekday 1 means weekdays ...

Read More

Multi-Line printing in Python

Pavitra
Pavitra
Updated on 25-Mar-2026 963 Views

Python offers several ways to print multiple lines of text. Instead of using multiple print() statements, you can use triple quotes, escape characters, or other techniques for cleaner multi-line output. Using Triple Quotes Triple quotes allow you to create multi-line strings that preserve formatting and line breaks ? print(''' Motivational Quote: Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it. ''') Motivational Quote: Sometimes later becomes never, Do it now. Great things ...

Read More

Adding two Python lists elements

Farhan Muhamed
Farhan Muhamed
Updated on 25-Mar-2026 3K+ Views

In Python, a list is a built-in data structure that stores an ordered collection of multiple items in a single variable. Lists are mutable, meaning we can add, remove, and change their elements. This article demonstrates how to add corresponding elements of two Python lists element-wise. Given two equal-sized lists, we need to create a new list containing the sum of corresponding elements from both lists. Example Scenario # Input lists list1 = [3, 6, 9, 45, 6] list2 = [11, 14, 21, 0, 6] # Expected output: [14, 20, 30, 45, 12] # Explanation: ...

Read More

Python-program-to-convert-pos-to-sop

Pavitra
Pavitra
Updated on 25-Mar-2026 297 Views

In this article, we will learn how to convert a Product of Sum (POS) form to its equivalent Sum of Product (SOP) form in Boolean algebra. This conversion is useful in digital logic design and Boolean expression simplification. Problem statement − We are given a POS form and need to convert it into its equivalent SOP form. The conversion involves counting the number of variables in the POS form, extracting maxterms, and then finding the corresponding minterms to generate the SOP expression. Understanding POS to SOP Conversion In Boolean algebra: POS form: Product of sums ...

Read More

Python program to print all Prime numbers in an Interval

Pavitra
Pavitra
Updated on 25-Mar-2026 622 Views

In this article, we will learn how to find and print all prime numbers within a given range using Python. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Problem statement − We are given an interval and need to compute all the prime numbers in the given range. We'll use a brute-force approach based on the basic definition of prime numbers. For each number in the range, we check if it has any divisors between 2 and itself (excluding 1 and the number itself). Basic ...

Read More

Python program to interchange first and last elements in a list

Pavitra
Pavitra
Updated on 25-Mar-2026 826 Views

In this article, we will learn how to interchange the first and last elements in a Python list. This is a common programming task with multiple implementation approaches. Problem statement − We are given a list, and we need to swap the first element with the last element. There are 4 approaches to solve this problem as discussed below − Using Temporary Variable The most straightforward approach uses a temporary variable to hold one element during the swap operation ? def swapLast(data_list): size = len(data_list) # ...

Read More
Showing 6871–6880 of 8,549 articles
« Prev 1 686 687 688 689 690 855 Next »
Advertisements