Server Side Programming Articles

Page 12 of 2108

Add list elements with a multi-list based on index in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 661 Views

When working with nested lists, you may need to add elements from a simple list to elements within a nested list based on their index positions. This operation pairs each element from the simple list with the corresponding sublist in the nested list and adds the simple list element to each item in that sublist. If the lists have different lengths, the operation is limited by the shorter list. Below are three efficient methods to accomplish this task. Using for Loop This method uses nested loops to iterate through both lists simultaneously. We take the length of ...

Read More

Add trailing Zeros to a Python string

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 4K+ Views

When processing strings in Python, you may need to add trailing zeros for formatting purposes such as padding numbers or creating fixed-width strings. Python provides several methods to accomplish this task efficiently. Using ljust() Method The ljust() method returns a string left-justified within a specified width, padding with a given character. Combined with len(), it allows dynamic zero padding ? Example # Add trailing zeros to a Python string # initializing string text = 'Jan-' print("The given input: " + text) # Number of zeros required n = 3 # Using ljust() to ...

Read More

Programs for printing pyramid patterns in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 2K+ Views

Taking advantage of the for loop and range function in Python, we can draw a variety of pyramid structures. The key to the approach is designing the appropriate for loop which will leave both vertical and horizontal space for the position of the symbol we choose for drawing the pyramid structure. Pattern 1: Right-Angled Triangle We draw a right angle based pattern where each row contains an increasing number of stars ? def pyramid(p): for m in range(0, p): for n in range(0, m+1): ...

Read More

Program to make Indian Flag in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 3K+ Views

Python's libraries for drawing graphs have very extensive features which can not only give us charts but also provide flexibility to draw other diagrams like flags. In that sense, those modules have an artistic touch. In this article we will see how to draw the Indian flag using the libraries NumPy and Matplotlib. Approach Create three rectangles of same width and draw them with appropriate colors and borders representing the tricolor. Use pyplot function to draw the circle of the Ashoka Chakra at the center of the middle rectangle. ...

Read More

Program to create grade calculator in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 3K+ Views

In academics, it is a common requirement to calculate student grades based on their assessment scores. In this article, we will create a Python program that assigns grades using a predefined grading criteria. Grading Criteria Below is the grading criteria used in our program − Score >= 90 : "O" (Outstanding) Score >= 80 : "A+" (Excellent) Score >= 70 : "A" (Very Good) Score >= 60 : "B+" (Good) Score >= 50 : "B" (Above Average) Score >= 40 : "C" (Average) Score < 40 : "F" (Fail) Program Structure Our ...

Read More

Print number with commas as 1000 separators in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 558 Views

Numbers with three or more digits often need to be displayed with commas for better readability, especially in accounting and finance applications. Python provides several methods to format numbers with commas as thousand separators. Using f-strings (Python 3.6+) The most modern approach uses f-string formatting with the comma specifier ? # Format integers with commas num1 = 1445 num2 = 140045 num3 = 5000000 print(f'{num1:, }') print(f'{num2:, }') print(f'{num3:, }') 1, 445 140, 045 5, 000, 000 Formatting Float Numbers For floating-point numbers, specify the decimal precision along with ...

Read More

Print first n distinct permutations of string using itertools in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 440 Views

When working with permutations of strings that contain duplicate characters, we often need to find only the distinct permutations. Python's itertools.permutations() generates all possible arrangements, including duplicates, so we need additional logic to filter unique results. Syntax from itertools import permutations def get_distinct_permutations(string, n): # Sort characters to group duplicates together sorted_chars = sorted(list(string)) # Generate all permutations all_perms = permutations(sorted_chars) # Use set to store ...

Read More

Prime or not in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 811 Views

Prime numbers play a central role in many applications like cryptography. So it is a necessity to check for prime numbers using Python programs in various applications. A prime number is a number which doesn't have any factors other than one and itself. Below we'll see programs that can find out if a given number is prime or not. Basic Approach We take the following approach to decide whether a number is prime or not ? Check if the number is positive or not. As only positive numbers can be prime numbers. We divide the number ...

Read More

Maximum length of consecutive 1's in a binary string in Python using Map function

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 502 Views

When working with binary strings, you may need to find the maximum length of consecutive 1's. Python provides several approaches using built-in functions like split() with map() and regular expressions. Using Split and Map The split() function divides a string by a delimiter. When we split by '0', we get segments of consecutive 1's. The map() function applies len() to each segment, and max() finds the longest one ? Example data = '11110000111110000011111010101010101011111111' def max_consecutive_ones(binary_string): return max(map(len, binary_string.split('0'))) result = max_consecutive_ones(data) print("Maximum Number of consecutive one's:", result) ...

Read More

Usage of Asterisks in Python

Pradeep Elance
Pradeep Elance
Updated on 15-Mar-2026 340 Views

Python programming language uses both * and ** in different contexts. In this article, we will explore how these operators are used and their practical applications. As an Infix Operator When * is used as an infix operator, it performs mathematical multiplication on numbers. Let's see examples with integers, floats, and complex numbers ? # Integers x = 20 y = 10 z = x * y print(z) # Floats x1 = 2.5 y1 = 5.1 z1 = x1 * y1 print(z1) # Complex Numbers x2 = 4 + 5j y2 = 5 + ...

Read More
Showing 111–120 of 21,079 articles
« Prev 1 10 11 12 13 14 2108 Next »
Advertisements