Programming Articles - Page 1226 of 3363

Python Program to Create a List of Tuples with the First Element as the Number and Second Element as the Square of the Number

AmitDiwan
Updated on 19-Apr-2021 11:04:09

3K+ Views

When it is required to create a list of tuple, and have the first element as the number, and the second element as the square of the element, list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [23, 42, 67, 89, 11, 32] print(“The list is “) print(my_list) print(“The resultant tuple is :”) my_result = [(val, pow(val, 2)) for val in my_list] print(my_result)OutputThe list is [23, 42, 67, 89, 11, 32] The resultant tuple is : [(23, 529), (42, 1764), (67, 4489), (89, 7921), (11, 121), (32, 1024)]ExplanationA list is defined, and is displayed on ... Read More

Python Program to Find the Second Largest Number in a List Using Bubble Sort

AmitDiwan
Updated on 19-Apr-2021 11:03:51

963 Views

When it is required to find the second largest number in a list using bubble sort, a method named ‘bubble_sort’ is defined, that sorts the elements of the list. Once this is done, another method named ‘get_second_largest’ is defined that returns the second element from the end as output.Below is the demonstration of the same −Example Live Demomy_list = [] my_input = int(input("Enter the number of elements...")) for i in range(1, my_input+1):    b=int(input("Enter the element..."))    my_list.append(b) for i in range(0, len(my_list)):    for j in range(0, len(my_list)-i-1):       if(my_list[j]>my_list[j+1]):          temp=my_list[j]       ... Read More

Python Program to Merge Two Lists and Sort it

AmitDiwan
Updated on 19-Apr-2021 11:03:33

2K+ Views

When it is required to merge two lists and sort them, a method can be defined that sorts the list using ‘sort’ method.Below is the demonstration of the same −Example Live Demodef merge_list(list_1, list_2):    merged_list = list_1 + list_2    merged_list.sort()    return(merged_list) list_1 = [20, 18, 9, 51, 48, 31] list_2 = [28, 33, 3, 22, 15, 20] print("The first list is :") print(list_1) print("The second list is :") print(list_2) print(merge_list(list_1, list_2))OutputThe first list is : [20, 18, 9, 51, 48, 31] The second list is : [28, 33, 3, 22, 15, 20] [3, 9, 15, 18, 20, ... Read More

Python Program to Find the Gravitational Force Acting Between Two Objects

AmitDiwan
Updated on 19-Apr-2021 11:02:59

492 Views

When it is required to find the gravitational force that acts between the two objects, a method named ‘find_gravity’ is used, and three parameters are passed to it.Below is the demonstration of the same −Example Live Demodef find_gravity(m_1, m_2, r):    G_val = 6.673*(10**-11)    F_val = (G_val*m_1*m_2)/(r**2)    return round(F_val, 2) m_1 = 6000000 m_2 = 1000000 r = 45 print("The gravitational force is: ") print(find_gravity(m_1, m_2, r), "N")OutputThe gravitational force is: 0.2 NExplanationA method named ‘find_gravity’ is defined, that takes three parameters.The gravitational force, and the gravitational constant are determined, and the force value is returned as ... Read More

Python Program to Check If Two Numbers are Amicable Numbers

AmitDiwan
Updated on 19-Apr-2021 10:53:57

884 Views

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. When it is required to check if two numbers are amicable numbers, a method can be defined that iterates over the number, and uses the modulus operator. Another method is defined that calls the previously defined function to determine if two numbers are amicable or not.Below is the demonstration of the same −Example Live Demoimport math def divided_sum_val(my_val) :    res = 0    for i in range(2, int(math.sqrt(my_val)) + 1) :       ... Read More

Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List

AmitDiwan
Updated on 19-Apr-2021 10:53:37

1K+ Views

When it is required to compute a polynomial equation when the coefficients of the polynomial are stored in a list, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demomy_polynomial = [2, 5, 3, 0] num = 2 poly_len = len(my_polynomial) my_result = 0 for i in range(poly_len):    my_sum = my_polynomial[i]    for j in range(poly_len - i - 1):       my_sum = my_sum * num    my_result = my_result + my_sum print("The polynomial equation for the given list of co-efficients is :") print(my_result)OutputThe polynomial equation for the given list of co-efficients ... Read More

Python Program to Check if a Number is a Strong Number

AmitDiwan
Updated on 19-Apr-2021 10:53:09

7K+ Views

Strong number is a number whose sum of all digits’ factorial is equal to the number ‘n’. Factorial implies when we find the product of all the numbers below that number including that number and is denoted by ! (Exclamation sign), For example: 5! = 5x4x3x2x1 = 120. When it is required to check if a number is a strong number or not, the remainder/modulus operator and the ‘while’ loop can be used.Below is the demonstration of the same −Example Live Demomy_sum=0 my_num = 296 print("The number is") print(my_num) temp = my_num while(my_num):    i=1    fact=1    remainder = my_num%10 ... Read More

Python Program to Check if a Number is a Perfect Number

AmitDiwan
Updated on 19-Apr-2021 10:52:51

3K+ Views

A number is said to be a Perfect Number when that is equal to the sum of all its positive divisors except itself. When it is required to check if a number is a perfect number, a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demon = 6 my_sum = 0 for i in range(1, n):    if(n % i == 0):       my_sum = my_sum + i if (my_sum == n):    print("The number is a perfect number") else:    print("The number is not a perfect number")OutputThe number is a perfect numberExplanationThe ... Read More

Python Program to Print the Pascal's triangle for n number of rows given by the user

AmitDiwan
Updated on 19-Apr-2021 10:52:34

663 Views

When it is required to print the pascal’s triangle for a specific number of rows, where the number is entered by the user, a simple ‘for’ loop is used.Below is the demonstration of the same −Example Live Demofrom math import factorial input = int(input("Enter the number of rows...")) for i in range(input):    for j in range(input-i+1):       print(end=" ")    for j in range(i+1):       print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") print()OutputEnter the number of rows...6       1      1 1     1 2 1    1 3 3 1   ... Read More

Python Program to Convert Binary to Gray Code

AmitDiwan
Updated on 19-Apr-2021 10:52:13

2K+ Views

When it is required to convert binary code to gray code, a method is defined that performs the ‘xor’ operation.Below is the demonstration of the same −Example Live Demodef binary_to_gray_op(n):    n = int(n, 2)    n ^= (n >> 1)    return bin(n)[2:] gray_val = input('Enter the binary number: ') binary_val = binary_to_gray_op(gray_val) print('Gray codeword is :', binary_val)OutputEnter the binary number: 101100110 Gray codeword is : 111010101ExplanationA method named ‘binary_to_gray_op’ is defined, that takes the binary number as its parameter.It performs the ‘xor’ operation.It returns the converted output.The input of binary number is taken from the user.The function ... Read More

Advertisements