Articles on Trending Technologies

Technical articles with clear explanations and examples

How can I do Python Tuple Slicing?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 516 Views

Tuple slicing allows you to extract a portion of a tuple using the slice operator :. The syntax is tuple[start:stop:step], where start is the beginning index, stop is the ending index (exclusive), and step is optional. Basic Tuple Slicing The basic syntax uses start:stop to extract elements from index start to stop-1 − numbers = (10, 50, 20, 9, 40, 25, 60, 30, 1, 56) # Extract elements from index 2 to 4 (exclusive) slice1 = numbers[2:4] print("numbers[2:4]:", slice1) # Extract elements from index 1 to 6 (exclusive) slice2 = numbers[1:6] print("numbers[1:6]:", slice2) ...

Read More

What are the differences and similarities between tuples and lists in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 1K+ Views

Both lists and tuples are sequence data types in Python. They store comma-separated collections of items that can be of different types, but they have important differences in mutability and usage. Similarities Both lists and tuples support concatenation, repetition, indexing, and slicing operations ? List Operations # List operations numbers = [1, 2, 3] more_numbers = [4, 5, 6] # Concatenation combined = numbers + more_numbers print("Concatenation:", combined) # Repetition repeated = numbers * 3 print("Repetition:", repeated) # Indexing print("Index 4:", combined[4]) # Slicing print("Slice [2:4]:", combined[2:4]) Concatenation: ...

Read More

What's the difference between lists and tuples in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 688 Views

Python has two main sequence data types for storing collections: lists and tuples. Both can store multiple items of different types, but they differ significantly in mutability, syntax, and use cases. Key Differences Overview Feature List Tuple Mutability Mutable (changeable) Immutable (unchangeable) Syntax Square brackets [ ] Parentheses ( ) Performance Slower Faster Use Case Dynamic data Fixed data Creating Lists and Tuples # Creating a list fruits_list = ['apple', 'banana', 'orange'] print("List:", fruits_list) # Creating a tuple fruits_tuple = ...

Read More

How to get the second-to-last element of a list in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 26K+ Views

Python lists support negative indexing, where -1 refers to the last element and -2 refers to the second-to-last element. This makes accessing elements from the end of a list straightforward. Using Negative Indexing The most direct way is using index -2 ? numbers = [1, 2, 3, 4, 5] second_last = numbers[-2] print(second_last) 4 Handling Edge Cases For lists with fewer than 2 elements, accessing [-2] raises an IndexError. Use a conditional check ? def get_second_last(items): if len(items) >= 2: ...

Read More

How do I sort a list of dictionaries by values of the dictionary in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 870 Views

In this article, we will show how to sort a list of dictionaries by the values of the dictionary in Python. Sorting has always been a useful technique in everyday programming. Python dictionaries are frequently used in applications ranging from competitive programming to web development (handling JSON data). Being able to sort dictionaries by their values is essential for data processing and analysis. We'll explore two main approaches to accomplish this task ? Using sorted() and itemgetter Using sorted() and lambda functions Understanding Python Dictionaries A dictionary is Python's implementation of an associative ...

Read More

How can I get last 4 characters of a string in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 3K+ Views

Getting the last 4 characters of a string is a common task in Python. You can achieve this using string slicing with negative indices, which count from the end of the string. Using Negative Slicing The slice operator [-4:] starts from the 4th character from the end and goes to the end of the string ? text = "Thanks. I am fine" last_four = text[-4:] print(last_four) fine Examples with Different Strings Here are more examples showing how negative slicing works ? # Different string examples word1 = "Python" ...

Read More

Can we assign a reference to a variable in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 1K+ Views

In Python, variables work differently than in languages like C/C++. Understanding this difference is crucial for Python programming. In C/C++, a variable represents a named location in memory, but Python variables are simply names that refer to objects. Variables in C/C++ vs Python C++ Variable Behavior In C++, variables are memory locations. When you assign one variable to another, it creates a copy ? int x = 5; int y = x; // Creates a copy of x's value Each variable has its own memory address ? cout

Read More

How can we unpack a string of integers to complex numbers in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 265 Views

A string containing two integers separated by a comma can be unpacked and converted into a complex number. Python provides several approaches to achieve this conversion. Method 1: Using split() and Indexing First, split the string into a list of string digits, then convert each part to integers for the complex() function − s = "1, 2".split(", ") print("Split result:", s) # Convert to complex number result = complex(int(s[0]), int(s[1])) print("Complex number:", result) Split result: ['1', '2'] Complex number: (1+2j) Method 2: Using Unpacking with map() Use map() to ...

Read More

What is the best way to handle list empty exception in Python?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 2K+ Views

When working with lists in Python, you may encounter situations where you need to handle empty list exceptions. A list is an ordered sequence of elements accessed using indices from 0 to length-1. If you try to access an index beyond this range or perform operations on an empty list, Python raises an IndexError exception. Understanding IndexError with Empty Lists The most common scenario is when using methods like pop() on an empty list. Here's how to handle it properly using try-except ? numbers = [1, 2, 3] while True: try: ...

Read More

How to replace Digits into String using Java?

Malhar Lathkar
Malhar Lathkar
Updated on 15-Mar-2026 2K+ Views

In Java, you can replace digits in a string with their corresponding word representations using a HashMap to map each digit to its text equivalent. This approach allows you to convert numeric characters to readable words efficiently. Creating the Digit-to-Word Mapping First, create a HashMap object from the java.util package to store digit-to-word mappings ? Map map = new HashMap(); This HashMap associates each digit with its corresponding word representation ? map.put("0", "zero"); map.put("1", "one"); map.put("2", "two"); // ... and so on for all digits Algorithm Steps The process involves iterating through each character in the string, checking ...

Read More
Showing 1–10 of 61,299 articles
« Prev 1 2 3 4 5 6130 Next »
Advertisements