Tag Archives: python example

Python Example – Write a Python program to copy of a deque object and verify the shallow copying process

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to copy of a deque object and verify the shallow copying process.   Sample Solution: Python Code: import collections tup1 = (1,3,5,7,9) dq1 = collections.deque(tup1) dq2 = dq1.copy() print(“Content of dq1:”) print(dq1) print(“dq2 id:”) print(id(dq1)) print(“nContent of dq2:”) print(dq2) print(“dq2 id:”) …

Python Example – Write a Python program to remove all the elements of a given deque object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove all the elements of a given deque object.   Sample Solution: Python Code: import collections odd_nums = (1,3,5,7,9) odd_deque = collections.deque(odd_nums) print(“Original Deque object with odd numbers:”) print(odd_deque) print(“Deque length: %d”%(len(odd_deque))) odd_deque.clear() print(“Deque object after removing all numbers-“) print(odd_deque) …

Python Example – Write a Python program to add more number of elements to a deque object from an iterable object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to add more number of elements to a deque object from an iterable object.   Sample Solution: Python Code: import collections even_nums = (2, 4, 6, 8, 10) even_deque = collections.deque(even_nums) print(“Even numbers:”) print(even_deque) more_even_nums = (12, 14, 16, 18, 20) …

Python Example – Write a Python program to create a deque from an existing iterable object.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a deque from an existing iterable object.   Sample Solution: Python Code: import collections even_nums = (2, 4, 6) print(“Original tuple:”) print(even_nums) print(type(even_nums)) even_nums_deque = collections.deque(even_nums) print(“\nOriginal deque:”) print(even_nums_deque) even_nums_deque.append(8) even_nums_deque.append(10) even_nums_deque.append(12) even_nums_deque.appendleft(2) print(“New deque from an existing iterable …

Python Example – Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a deque and append few elements to the left and right, then remove some elements from the left, right sides and reverse the deque.   Sample Solution: Python Code: import collections deque_colors = collections.deque([“Red”,”Green”,”White”]) print(deque_colors) print(“\nAdding to the left: …

Python Example – Write a Python program to find the occurrences of 10 most common words in a given text.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the occurrences of 10 most common words in a given text.   Sample Solution: Python Code: from collections import Counter import re text = “””The Python Software Foundation (PSF) is a 501(c)(3) non-profit corporation that holds the intellectual property …

Python Example – Write a Python program to find the most common elements and their counts of a specified text.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the most common elements and their counts of a specified text.   Sample Solution: Python Code: from collections import Counter s = ‘lkseropewdssafsdfafkpwe’ print(“Original string: “+s) print(“Most common three characters of the said string:”) print(Counter(s).most_common(3)) Sample Output: Original string: …

Python Example – Write a Python program that iterate over elements repeating each as many times as its count.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program that iterate over elements repeating each as many times as its count. Note: Elements are returned in arbitrary order.   Sample Solution: Python Code: from collections import Counter c = Counter(p=4, q=2, r=0, s=-2) print(list(c.elements())) Sample Output: [‘p’, ‘p’, ‘p’, ‘p’, …

Python Example – Write a Python program to check a given set has no elements in common with other given set.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check a given set has no elements in common with other given set.   Sample Solution: Python Code: sn1 = {1,2,3} sn2 = {4,5,6} sn3 = {3} print(“Original sets:”) print(sn1) print(sn2) print(sn3) print(“Check sn1 set has no elements in common …

Python Example – Write a Python program to check if a given set is superset of itself and superset of another given set

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check if a given set is superset of itself and superset of another given set.   Sample Solution: Python Code: nums = {10,20,30,40,50} print(“Original set: “,nums) print(“If nums is superset of itself?”) print(nums.issuperset(nums)) num1 = {1, 2, 3, 4, 5, …