Blog

python tutorials and learn python

Created with Sketch.

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

Python program that uses a for loop and the modulus operator to print the elements of an array that are present on odd positions:

Python program that uses a for loop and the modulus operator to print the elements of an array that are present on odd positions: The function print_odd_elements(arr) takes an array as an argument. It uses a for loop to iterate over the indices of the array, using the range() function to generate a range of…
Read more

Python program that uses a for loop and the modulus operator to print the elements of an array that are present on even positions:

Python program that uses a for loop and the modulus operator to print the elements of an array that are present on even positions: The function print_even_elements(arr) takes an array as an argument. It uses a for loop to iterate over the indices of the array, using the range() function to generate a range of…
Read more

Python program that uses a for loop to print the elements of an array in reverse order:

Python program that uses a for loop to print the elements of an array in reverse order: The function print_array_reverse(arr) takes an array as an argument. It uses a for loop to iterate over the indices of the array in reverse order, starting from the last index and ending at the first index. It uses…
Read more

Python program that uses a for loop to print the elements of an array:

Python program that uses a for loop to print the elements of an array: The function print_array(arr) takes an array as an argument. It uses a for loop to iterate over the array, and for each element, it prints the element. The example usage creates an array and calls the print_array() function to print the…
Read more