Python Articles

Page 53 of 852

Python Program to Find Average of Array Elements

AYUSH MISHRA
AYUSH MISHRA
Updated on 04-Feb-2025 9K+ Views

In this article, we will learn how to find the average of array elements using Python, along with examples to demonstrate the implementation. In this problem, we are given an array of integers, which may contain both positive and negative numbers. Our task is to calculate the average of all elements in the array. Using the Brute Force Approach This is a simple and direct approach. This approach involves manually iterating through each element in the array to calculate the sum. Then, we divide the total sum by the count of elements to get the average. This approach is ...

Read More

Python Program to Toggle Each Character in a String

AYUSH MISHRA
AYUSH MISHRA
Updated on 04-Feb-2025 11K+ Views

In this problem, we are given a string, and the task is to toggle the case of each character in the string. This means that all lowercase letters should be converted to uppercase, and all uppercase letters should be converted to lowercase. In this article, we are going to explore different approaches to toggling each character in a string using Python. Let's look at the simple example, where we are going to provide input as "TutorialsPoint". Then output is like "tUTORIALSpOINT" In this case each character is toggled. Such that Uppercase letters become lowercase and lowercase becomes uppercase. Below are ...

Read More

Python Program for sum of the series: 1 + 1/2 + 1/3 + ... + 1/n

AYUSH MISHRA
AYUSH MISHRA
Updated on 04-Feb-2025 14K+ Views

Mathematical series is one of the problems that helps us to improve problem-solving and logical thinking skills One of the engaging problems is calculating the sum of the harmonic series. The series 1 + 1/2 + 1/3 + ⋯⋯ + 1/n is known as the harmonic series. In this article, we are going to learn how to find the sum of the series: 1 + 1/2 + 1/3 + ⋯⋯ + 1/n using Python. What is the Harmonic Series? The harmonic series is a mathematical sequence where each term is the reciprocal of a positive integer. It is represented as, ...

Read More

Python Program to Implement Ternary Search

AYUSH MISHRA
AYUSH MISHRA
Updated on 04-Feb-2025 10K+ Views

Searching is one of the most fundamental operations in computer science, and there are multiple algorithms to perform it efficiently. While Binary Search is mainly known to be applied to sorted array searching, Ternary Search is another efficient searching technique that also works on sorted arrays for searching. What is a Ternary Search Ternary Search is a divide-and-conquer algorithm that splits the array into three parts instead of two (as in Binary Search). It repeatedly reduces the search space by two-thirds in each step, making it useful in cases where the dataset is large, and constant-time comparisons are expensive. ...

Read More

Python Program to Find Cube of a Number

AYUSH MISHRA
AYUSH MISHRA
Updated on 31-Jan-2025 6K+ Views

The Cube of a number is a simple mathematical operation where a number is multiplied by itself three times. In Python, there are multiple ways to find the cube of a number. In this article, we are going to discuss various approaches to calculating the cube of a number in Python. How to find cube of a number? The formula for finding the cube of a given number N is: Cube = N × N × N or N3 Example 1 Input: 5 Output: 125 Explanation: The cube of ...

Read More

10 Python Code Snippets For Everyday Programming Problems

Tushar Sharma
Tushar Sharma
Updated on 31-Jan-2025 1K+ Views

Python has risen as one of the foremost favored programming languages all-inclusive, owing to its flexibility, user-friendliness, and extensive libraries. Whether a beginner or a prepared developer, having a collection of convenient code parts can spare you important time and energy. In this article, we'll delve into ten Python code fragments that can be employed to tackle routine programming challenges. We'll guide you through each fragment, elucidating how it operates in straightforward steps. Swapping two variables Switching the values of two variables is a frequent task in programming. In Python, this can be achieved without utilizing a temporary variable ...

Read More

How to open a binary file in append mode with Python?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 28-Jan-2025 3K+ Views

In Python, to open a binary file in append mode, we can use the open() function with the mode set to ab. This allows us to open an existing file or create a new one. Using the open() function with the appropriate mode enables us to work with the file as needed. Mode 'ab' In the mode 'ab', the 'a' stands for append mode, which allows us to add new data to the end of the file without truncating its existing content. The 'b' stands for binary mode, used when handling files containing non-text data or non-readable characters. Binary files ...

Read More

How will you compare namespaces in Python and C++?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 27-Jan-2025 984 Views

Namespaces help in organizing code, managing the scope of variables and preventing naming conflicts. Python and C++ use namespaces, but they do so in different ways. Below is an overview of namespaces in both. Namespaces in C++ In C++, namespaces are created using the keyword 'namespace'. They are mainly intended to organize code into logical groups and avoid name conflicts, particularly when working with multiple libraries. Example In the following example we are going to how to use a namespace by utilizing '::' to access functions within that namespace. #include using namespace std; // first namespace namespace first_space ...

Read More

How do we use re.finditer() method in Python regular expression?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 23-Jan-2025 19K+ Views

The re.finditer() method in Python's 're' module finds all occurrences of a specified pattern in a string. It returns an iterator containing match objects for each occurrence. This method is especially useful when you need to determine the position of each match within the input string. Following is the syntax for re.finditer() method. re.finditer(pattern,  string,  flags=0) How to Use 're.finditer()' Method Following are the steps involved in using re.finditer() method. Import the re module: You need to import the regular expression module before using it. ...

Read More

Python - Replace duplicate Occurrence in String

Akshitha Mote
Akshitha Mote
Updated on 22-Jan-2025 885 Views

In this article, we will explore different ways to replace duplicate occurrences in a string. Before understanding the solution let's try to understand the problem statement with an example. Consider a string "Ram lives in Hyderabad. Ram is studying in class 10. Ram is the class Monitor of the class." In this string "Ram" appears multiple times we need to replace the duplicate occurrences with 'He', keeping the first occurrence unchanged. Replacing Duplicate Occurrences in a String Following are the multiple ways to replace duplicate occurrences in a string − Using List comprehension ...

Read More
Showing 521–530 of 8,519 articles
« Prev 1 51 52 53 54 55 852 Next »
Advertisements