Get N-th Column of Matrix in Python

AmitDiwan
Updated on 16-Apr-2021 12:14:57

418 Views

When it is required to get the ‘n’th column of a matrix, the ‘any’ method can be used.Below is a demonstration of the same −Example Live Demomy_list = [[34, 67, 89], [16, 27, 86], [48, 30, 0]] print("The list is : ") print(my_list) N = 1 print("The value of N has been initialized to -") print(N) elem = 30 my_result = any(sub[N] == elem for sub in my_list) print("Does the element exist in a particular column ? ") print(my_result)OutputThe list is : [[34, 67, 89], [16, 27, 86], [48, 30, 0]] The value of N has been ... Read More

Matrix Creation of n x n in Python

AmitDiwan
Updated on 16-Apr-2021 12:12:23

675 Views

When it is required to create a matrix of dimension n * n, a list comprehension is used.Below is a demonstration of the same −Example Live DemoN = 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 * 0 is :") print(my_result)OutputThe value of N is 4 The matrix of dimension N * 0 is : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]ExplanationThe value of N is ... Read More

Convert Height from Centimeters to Feet and Inches in Python

AmitDiwan
Updated on 16-Apr-2021 12:11:44

4K+ Views

When it is required to read the height in ‘cm’ and convert it into ‘feet’ and ‘inches’, the ‘round’ method can be used.Below is a demonstration of the same −Example Live Demoin_cm=int(input("Enter the height in centimeters...")) in_inches=0.394*in_cm in_feet=0.0328*in_cm print("The length in inches is ") print(round(in_inches, 2)) print("The length in feet is") print(round(in_feet, 2))OutputEnter the height in centimeters...178 The length in inches is 70.13 The length in feet is 5.84ExplanationThe input is taken by user, as ‘cm’.It can be converted into inches by multiplying it with 0.394.This is assigned to a variable.It can be converted into feet by multiplying it with 0.0328.This ... Read More

Compute Simple Interest in Python

AmitDiwan
Updated on 16-Apr-2021 12:11:26

345 Views

When it is required to compute a simple interest when the amount, rate and interest are given, a simple formula can be defined, and the elements can be plugged into the formula.Below is a demonstration of the same −Example Live Demoprinciple_amt = float(input("Enter the principle amount...")) my_time = int(input("Enter the time in years...")) my_rate = float(input("Enter the rate...")) my_simple_interest=(principle_amt*my_time*my_rate)/100 print("The computed simple interest is :") print(my_simple_interest)OutputEnter the principle amount...45000 Enter the time in years...3 Enter the rate...6 The computed simple interest is : 8100.0ExplanationThe principal amount, the rate of interest, and the time are taken as user inputs.Another formula is defined ... Read More

Check Valid Date and Print Incremented Date in Python

AmitDiwan
Updated on 16-Apr-2021 12:11:08

593 Views

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.Below is a demonstration of the same −Example Live Demomy_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):    max_val = 31 elif(mm==4 or mm==6 or mm==9 or mm==11):    max_val = 30 elif(yy%4==0 and yy%100!=0 or yy%400==0):    max_val = 29 else:    max_val = 28 if(mm12 or dd max_val):    print("The date ... Read More

Print Natural Numbers Summation Pattern in Python

AmitDiwan
Updated on 16-Apr-2021 12:09:38

428 Views

When it is required to read a number and print the pattern of summation of natural numbers, a simple ‘for’ loop can be used.Below is a demonstration of the same −Example Live Demomy_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=" ")       if(i

Python Program to Print Series 1 + 2 + ... + n

AmitDiwan
Updated on 16-Apr-2021 12:06:38

400 Views

When it is required to display the sum all the natural numbers within a given range, a method can be defined that uses a loop to iterate over the elements, and returns the sum of these numbers as output.Below is a demonstration of the same −Example Live Demodef sum_natural_nums(val):    my_sum = 0    for i in range(1, val + 1):       my_sum += i * (i + 1) / 2    return my_sum val = 9 print("The value is ") print(val) print("The sum of natural numbers upto 9 is : ") print(sum_natural_nums(val))OutputThe value is 9 The sum ... Read More

Print Integers Not Divisible by 2 or 3 Between 1 and 50

AmitDiwan
Updated on 16-Apr-2021 12:06:16

2K+ Views

When it is required to print all the elements which can’t be divided by 2 or 3 and lie between 1 and 50, the constraints are mentioned in the form of a ‘while’ loop and ‘if’ condition.Below is a demonstration of the same −Example Live Demoprint("Integers not divisible by 2 and 3, that lie between 1 and 50 are : ") n = 1 while n

Find Smallest Divisor of an Integer in Python

AmitDiwan
Updated on 16-Apr-2021 12:05:59

3K+ Views

When it is required to find the smallest divisor of a an integer, a simple ‘for’ loop is used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter a number...")) my_list = [] print("The number is ") print(first_num) for i in range(2, first_num+1):    if(first_num%i==0):       my_list.append(i) my_list.sort() print("The smallest divisor is : ") print(my_list[0])OutputEnter a number...56 The number is 56 The smallest divisor is : 2ExplanationThe number is taken as an input from the user.An empty list is defined.The number taken from user is displayed on the console.The number range is iterated over.It is ... Read More

Read Two Numbers and Print Their Quotient and Remainder in Python

AmitDiwan
Updated on 16-Apr-2021 12:05:07

2K+ Views

When it is required to read two numbers and print the quotient and remainder when they are divided, the ‘//’ and ‘%’ operators can be used.Below is a demonstration of the same −Example Live Demofirst_num = int(input("Enter the first number...")) second_num = int(input("Enter the second number...")) print("The first number is ") print(first_num) print("The second number is ") print(second_num) quotient_val = first_num//second_num remainder_val = first_num%second_num print("The quotient is :") print(quotient_val) print("The remainder is :") print(remainder_val)OutputEnter the first number...44 Enter the second number...56 The first number is 44 The second number is 56 The quotient is : 0 The remainder is : ... Read More

Advertisements