Category: Other

python tutorials and learn python

Created with Sketch.

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

Python program that uses a dictionary to print the duplicate elements of an array:

Python program that uses a dictionary to print the duplicate elements of an array: The function print_duplicates(arr) takes an array as an argument. It first creates an empty dictionary freq. Then, it uses a for loop to iterate over the array, and for each element, it checks if the element already exists in the dictionary…
Read more

Python program that uses the deque class from the collections module to left rotate the elements of an array:

Python program that uses the deque class from the collections module to left rotate the elements of an array: The function left_rotate(arr, n) takes an array arr and an integer n as arguments. It first creates a deque object d from the array arr using the deque() constructor from the collections module. Then, it uses…
Read more

Python program that uses a dictionary to find the frequency of each element in an array:

Python program that uses a dictionary to find the frequency of each element in an array: The function frequency(arr) takes an array as an argument. It first creates an empty dictionary freq. Then, it uses a for loop to iterate over the array, and for each element, it checks if the element already exists in…
Read more

Python program that uses the copy() method to copy all elements of one array into another array:

Python program that uses the copy() method to copy all elements of one array into another array: The function copy_array(arr1) takes an array as an argument. array.array() method is used to create a new array with the same typecode as the original array arr1 and an empty list as the initializer. The extend() method is…
Read more

Python program that uses the str.translate() method along with the string.punctuation property to remove punctuation from a given string

Python program that uses the str.translate() method along with the string.punctuation property to remove punctuation from a given string The function remove_punctuation(input_string) takes a string as an argument. The string.punctuation property is a string containing all ASCII punctuation characters. str.maketrans() method is used to create a translation table that can be used with str.translate() method.…
Read more

Python program that uses the sorted() function to sort a list of words in alphabetic order:

Python program that uses the sorted() function to sort a list of words in alphabetic order: The function sort_words(words) takes a list of words as an argument. The sorted() function is used to sort the list of words in alphabetic order. It returns the sorted list of words. The example usage prompts the user to…
Read more