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 23 of 2547
Get Nth Column of Matrix in Python
When working with matrices in Python, you often need to extract a specific column. Python provides several methods to get the Nth column of a matrix, including list comprehension, the zip() function, and NumPy arrays. Using List Comprehension The most straightforward approach is using list comprehension to extract elements at a specific index ? matrix = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The matrix is:") print(matrix) N = 1 print(f"Getting column {N}:") # Extract Nth column using list comprehension nth_column = [row[N] for row in matrix] print(nth_column) ...
Read MoreMatrix creation of n*n in Python
When it is required to create a matrix of dimension n * n, a list comprehension is used. This technique allows us to generate a square matrix with sequential numbers efficiently. Below is a demonstration of the same − Example N = 4 print("The value of N is") print(N) my_result = [list(range(1 + N * i, 1 + N * (i + 1))) for i in range(N)] print("The matrix of dimension N * N is:") print(my_result) Output ...
Read MorePython Program to Read Height in Centimeters and convert the Height to Feet and Inches
Converting height from centimeters to feet and inches is a common unit conversion task. Python provides built-in functions like round() to handle decimal precision in calculations. Basic Conversion Method Here's a simple approach to convert centimeters to both feet and inches separately ? height_cm = int(input("Enter the height in centimeters: ")) # Convert to inches and feet height_inches = 0.394 * height_cm height_feet = 0.0328 * height_cm print("The height in inches is:", round(height_inches, 2)) print("The height in feet is:", round(height_feet, 2)) Enter the height in centimeters: 178 The height in inches ...
Read MorePython Program to Compute Simple Interest Given all the Required Values
When it is required to compute simple interest when the principal amount, rate, and time are given, we can use the standard formula: Simple Interest = (Principal × Time × Rate) / 100. Below is a demonstration of the same − Simple Interest Formula The mathematical formula for calculating simple interest is: Simple Interest = (Principal × Time × Rate) / 100 Where: Principal − The initial amount of money Time − Duration in years Rate − Annual interest rate (percentage) ...
Read MorePython Program to Check if a Date is Valid and Print the Incremented Date if it is
When it is required to check if a date is valid or not, and print the incremented date if it is a valid date, the 'if' condition is used along with date validation logic. Below is a demonstration of the same − Example my_date = input("Enter a date : ") dd, mm, yy = my_date.split('/') dd = int(dd) mm = int(mm) yy = int(yy) if(mm == 1 or mm == 3 or mm == 5 or mm == 7 or mm == 8 or mm == 10 or mm == 12): ...
Read MorePython Program to Read a Number n and Print the Natural Numbers Summation Pattern
When it is required to read a number and print the pattern of summation of natural numbers, a simple for loop can be used. This pattern displays progressive sums like "1 = 1", "1 + 2 = 3", "1 + 2 + 3 = 6", and so on. Example my_num = int(input("Enter a number... ")) for j in range(1, my_num + 1): my_list = [] for i in range(1, j + 1): print(i, sep=" ", end=" ") ...
Read MorePython Program to Read a Number n And Print the Series "1+2+.....+n=
When it is required to display the sum of all natural numbers within a given range and show the series format "1+2+...+n=sum", we can create a program that builds the series string and calculates the sum. Below is a demonstration of the same − Method 1: Using Loop to Build Series Example def print_series_sum(n): series = "" total = 0 for i in range(1, n + 1): total += i ...
Read MorePython Program to Print all Integers that are not Divisible by Either 2 or 3 and Lie between 1 and 50
When it is required to print all integers which are not divisible by either 2 or 3 and lie between 1 and 50, we can use a while loop with conditional statements to check each number. Numbers that satisfy both conditions (not divisible by 2 and not divisible by 3) will be printed. Example The following example demonstrates how to find integers between 1 and 50 that are not divisible by either 2 or 3 ? print("Integers not divisible by 2 and 3, that lie between 1 and 50 are:") n = 1 while n
Read MorePython Program to Find the Smallest Divisor of an Integer
When it is required to find the smallest divisor of an integer, a simple 'for' loop is used. The smallest divisor (other than 1) is the first number that divides the given integer evenly. Example Below is a demonstration of finding the smallest divisor ? first_num = int(input("Enter a number...")) divisors = [] print("The number is") print(first_num) for i in range(2, first_num + 1): if(first_num % i == 0): divisors.append(i) divisors.sort() print("The smallest divisor is :") print(divisors[0]) Output ...
Read MorePython Program to Accept Three Digits and Print all Possible Combinations from the Digits
When generating all possible combinations of three digits, we can use nested loops to create permutations where each digit appears exactly once in different positions. This approach uses three nested loops with conditional checks. Example first_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) third_num = int(input("Enter the third number...")) digits = [] print("The first number is") print(first_num) print("The second number is") print(second_num) print("The third number is") print(third_num) digits.append(first_num) digits.append(second_num) digits.append(third_num) print("All possible combinations:") for i in range(0, 3): for j in range(0, 3): ...
Read More