Category: Other

python tutorials and learn python

Created with Sketch.

Python program that uses a while loop and the split() method to check if a given number is a happy number:

A happy number is a number that eventually reaches 1 when replaced by the sum of the squares of its digits in a process called digit-square-sum. For example, the number 7 is a happy number because 7 -> 49 -> 97 -> 130 -> 10 -> 1. Here is a Python program that uses a…
Read more

Python program that uses a for loop and the is_disarium() function to print all Disarium numbers between 1 to 100:

Python program that uses a for loop and the is_disarium() function to print all Disarium numbers between 1 to 100:   This program uses a for loop to iterate over the range 1 to 100, and for each number, it checks if it is a Disarium number by calling the is_disarium(n) function. If the function…
Read more

Python program that uses a for loop and the pow() function to check if a given number is a Disarium number:

A Disarium number is a number such that the sum of its digits powered with its corresponding position is equal to the number itself. For example, the number 89 is a Disarium number because 8^1 + 9^2 = 89. Here is a Python program that uses a for loop and the pow() function to check…
Read more

Python program that uses the sort() method and the reverse argument to sort the elements of an array in descending order:

Python program that uses the sort() method and the reverse argument to sort the elements of an array in descending order: The function sort_descending(arr) takes an array as an argument. It uses the sort() method with the reverse argument set to True to sort the elements of the array in descending order. The function returns…
Read more

Python program that uses the sort() method to sort the elements of an array in ascending order:

Python program that uses the sort() method to sort the elements of an array in ascending order: The function sort_ascending(arr) takes an array as an argument. It uses the sort() method to sort the elements of the array in ascending order. By default, the sort() method sorts the elements of the array in ascending order.…
Read more

Python program to right-rotate the elements of an array

Python program to right rotate the elements of an array The function right_rotate(arr, k) takes an array arr and an integer k as arguments. It uses the modulus operator to ensure that k is within the range of the length of the array. It creates a new array by concatenating the last k elements of…
Read more

Python program that uses a for loop to print the sum of all elements in an array

Python program that uses a for loop to print the sum of all elements in an array The function sum_elements(arr) takes an array as an argument. It uses a for loop to iterate over the elements of the array and a variable total to keep track of the sum of the elements. For each element,…
Read more

Python program that uses the len() function to print the number of elements present in an array:

Python program that uses the len() function to print the number of elements present in an array: The function count_elements(arr) takes an array as an argument. It uses the len() function to find the number of elements present in the array and returns the value. The example usage creates an array and calls the count_elements()…
Read more

Python program that uses a for loop and an initial minimum value to print the smallest element in an array:

Python program that uses a for loop and an initial minimum value to print the smallest element in an array: The function smallest_element(arr) takes an array as an argument. It uses a for loop to iterate over the elements of the array. The function declares a variable min_val and assigns the value of the first…
Read more

Python program that uses a for loop and an initial maximum value to print the largest element in an array:

Python program that uses a for loop and an initial maximum value to print the largest element in an array: The function largest_element(arr) takes an array as an argument. It uses a for loop to iterate over the elements of the array. The function declares a variable max_val and assigns the value of the first…
Read more