Python Articles

Page 531 of 855

Python program to sort a list of tuples alphabetically

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 2K+ Views

When it is required to sort a list of tuples in alphabetical order, the sort() method can be used. This method performs in-place sorting, meaning the original list gets modified. The sort() function sorts values in ascending order by default. If descending order is needed, you can specify reverse=True. A list of tuples contains multiple tuples enclosed within a list. We can sort these tuples based on any element within each tuple using a key function. Sorting by First Element Here's how to sort a list of tuples alphabetically by the first element ? ...

Read More

How long does it take to learn Python?

pawandeep
pawandeep
Updated on 25-Mar-2026 403 Views

How fast someone can learn or excel in something completely depends on one's interest in learning, dedication and persistence. The time to learn Python varies significantly based on your learning goals, available time, and programming background. Learning Timeline for Python Basics The basics of Python — including functions, loops, conditional statements, and data types — typically take about 1-2 weeks for a complete beginner. This timeframe assumes you're dedicating 1-2 hours daily to focused learning and practice. Basic Python Skills Include Variables and data types (strings, integers, lists, dictionaries) Control structures (if statements, for/while loops) ...

Read More

Is Python a scripting language?

pawandeep
pawandeep
Updated on 25-Mar-2026 6K+ Views

Yes, Python is a scripting language. It is interpreted rather than compiled, which is the key characteristic that defines scripting languages. Scripting Language vs Programming Language The main difference between scripting and programming languages lies in their execution process. Scripting languages do not require compilation − they are directly interpreted at runtime. For example, programs written in languages like C++ must be compiled into machine code before execution, whereas programs written in scripting languages like Python or JavaScript are interpreted and executed directly without a separate compilation step. Why is Python a Scripting Language? Python ...

Read More

Cummulative Nested Tuple Column Product in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 269 Views

When working with nested tuples in Python, you may need to find the cumulative column product of corresponding elements. This can be achieved using the zip() method and nested generator expressions. A generator expression is a memory-efficient way of creating iterators. It automatically implements __iter__() and __next__() methods while keeping track of internal states and raising StopIteration when no values remain. The zip() method takes multiple iterables, aggregates corresponding elements into tuples, and returns an iterator of tuples. Example Here's how to calculate the element-wise product of nested tuples − tuple_1 = ((11, 23), ...

Read More

Kth Column Product in Tuple List in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 288 Views

When we need to find the product of elements in a specific column (Kth position) across all tuples in a list, we can use list comprehension to extract the column values and then calculate their product. A tuple is an immutable data type that stores elements in a fixed order. Once created, tuple elements cannot be modified. A list of tuples represents tabular data where each tuple is a row and tuple positions represent columns. List comprehension provides a concise way to extract specific elements from each tuple and perform operations on them. Example Here's how ...

Read More

Remove nested records from tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 433 Views

When working with nested tuples, you may need to remove inner tuples and keep only the non-tuple elements. Python provides several approaches to accomplish this task using isinstance(), list comprehensions, or filtering methods. The isinstance() method checks if an object belongs to a specific data type. The enumerate() method adds a counter to an iterable and returns index-value pairs. Method 1: Using Loop with isinstance() Iterate through the tuple and check each element's type ? tuple_1 = (11, 23, (41, 25, 22), 19, (30, 40), 50) print("The original tuple is:") print(tuple_1) my_result = ...

Read More

Write a program to form a cumulative sum list in Python

pawandeep
pawandeep
Updated on 25-Mar-2026 3K+ Views

The cumulative sum till ith element refers to the total sum from 0th to ith element. We need to form a new list where each element represents the running total up to that position. For example, if we have [10, 20, 30, 40, 50], the cumulative sum list would be [10, 30, 60, 100, 150]. Example Input and Output Example 1 Input: [10, 20, 30, 40, 50] Output: [10, 30, 60, 100, 150] Example 2 Input: [1, 2, 3, 4, 5] Output: [1, 3, 6, 10, 15] Using a Custom Function ...

Read More

Palindrome in Python: How to check a number is palindrome?

pawandeep
pawandeep
Updated on 25-Mar-2026 844 Views

A palindrome is a string that reads the same forwards and backwards. In other words, a palindrome string is equal to its reverse. For example, "civic" and "madam" are palindromes, while "cat" is not (its reverse "tac" differs from the original). Method 1: Using String Slicing The simplest approach is to reverse the string using slicing and compare it with the original ? def isPalindrome(s): rev = s[::-1] if rev == s: return True return ...

Read More

How to print pattern in Python?

pawandeep
pawandeep
Updated on 25-Mar-2026 2K+ Views

Patterns in Python can be printed using nested for loops. The outer loop is used to iterate through the number of rows whereas the inner loop is used to handle the number of columns. The print statement is modified to form various patterns according to the requirement. The patterns can be star patterns, number patterns, alphabet patterns. The patterns can be of different shapes, triangle, pyramids, etc. Triangle Pattern * * * * * * ...

Read More

How to reverse a number in Python?

pawandeep
pawandeep
Updated on 25-Mar-2026 2K+ Views

Reversing an integer number is a common programming task. You may need to reverse a number for palindrome checking, mathematical operations, or data processing scenarios. Input: 12345 Output: 54321 There are two main approaches to reverse a number in Python: Convert the number into a string, reverse the string and reconvert it into an integer Reverse mathematically without converting into a string Using String Slicing This method converts the number to a string, reverses it using Python's slice notation [::-1], and converts back to integer ? def reverse_string_method(num): ...

Read More
Showing 5301–5310 of 8,546 articles
« Prev 1 529 530 531 532 533 855 Next »
Advertisements