Blog

python tutorials and learn python

Created with Sketch.

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

Python program that uses nested loops to transpose a given matrix

Python program that uses nested loops to transpose a given matrix The function transpose_matrix(matrix) takes a 2D matrix as an argument. It first determines the number of rows and columns of the matrix using the len() function. Then it creates a new 2D matrix with the number of rows and columns swapped, using list comprehension,…
Read more

Python program that uses nested loops to multiply two matrices:

Python program that uses nested loops to multiply two matrices: The function multiply_matrices(A, B) takes two 2D matrices A and B as arguments. It first determines the number of rows and columns of the matrices using the len() function. Then it creates a new 2D matrix C with rows of the first matrix and columns…
Read more

Python program that uses nested loops to add two matrices of the same size

Python program that uses nested loops to add two matrices of the same size The function add_matrices(A, B) takes two 2D matrices A and B as arguments. It first determines the number of rows and columns of the matrices using the len() function. Then it creates a new 2D matrix C of the same size,…
Read more

Python program that uses recursion to find the factorial of a given number:

Python Program: Recursion to Find Factorial Recursion is a powerful programming concept where a function calls itself, and it’s often used to solve problems that can be broken down into smaller, identical subproblems. In this blog post, we’ll explore a Python program that uses recursion to find the factorial of a given number. The post…
Read more