Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 429 of 2547
Kth Column Product in Tuple List in Python
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 MoreRemove nested records from tuple in Python
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 MoreWrite a program to form a cumulative sum list in Python
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 MorePalindrome in Python: How to check a number is palindrome?
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 MoreHow to print pattern in Python?
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 MoreHow to reverse a number in Python?
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 MoreHow to print in same line in Python?
The print() function in Python automatically prints in the next line each time. By default, print() adds a newline character () at the end of each output. Default Behavior Here's how print() works by default ? for i in range(5): print(i) 0 1 2 3 4 Using the end Parameter The print() function accepts an end parameter that controls what gets printed at the end of each output. By changing this parameter, you can print on the same line. Syntax print("text", end="character") ...
Read MoreHow to declare a variable in Python?
In Python, we need not declare a variable with some specific data type. Python has no command for declaring a variable. A variable is created when some value is assigned to it. The value assigned to a variable determines the data type of that variable. Thus, declaring a variable in Python is very simple: Just name the variable Assign the required value to it The data type of the variable will be automatically determined from the value assigned, we need not define it explicitly. Declare an ...
Read MoreHow to convert int to string in Python?
Type conversion is often needed when you want to convert one data type into another according to your requirements. Python provides several methods to convert integers to strings. Python has a built-in function str() to convert an integer to a string. We will discuss various methods to convert int to string in Python. Using str() Function This is the most commonly used method to convert int to string in Python. The str() function takes an integer variable as a parameter and converts it into a string ? Syntax str(integer_variable) Example ...
Read MoreHow to compare two lists in Python?
The list in Python is a collection of items that we often need to compare. Python provides several methods to compare two lists, each with different behaviors regarding element order and frequency. Using == Operator (Order Matters) The simplest method compares lists element by element in the same positions. This method considers both values and order ? def compare_lists(list1, list2): if list1 == list2: return "Equal" else: return "Not equal" # ...
Read More