w3resource
Python Exercises

Python Basic(Part-II) - Exercises, Practice, Solution

Python basic (Part-II) [31 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python function that takes a sequence of numbers and determines if all the numbers are different from each other. Go to the editor
Click me to see the sample solution

2. Write a Python program to create all possible strings by using 'a', 'e', 'i', 'o', 'u'. Use the characters exactly once. Go to the editor
Click me to see the sample solution

3. Write a Python program to remove and print every third number from a list of numbers until the list becomes empty.
Click me to see the sample solution

4. Write a Python program to find unique triplets whose three elements gives the sum of zero from an array of n integers. Go to the editor
Click me to see the sample solution

5. Write a Python program to create the combinations of 3 digit combo. Go to the editor
Click me to see the sample solution

6. Write a Python program to print a long text, convert the string to a list and print all the words and their frequencies. Go to the editor
Click me to see the sample solution

7. Write a Python program to count the number of each character of a given text of a text file. Go to the editor
Click me to see the sample solution

8. Write a Python program to get the top stories from Google news. Go to the editor
Click me to see the sample solution

9. Write a Python program to get a list of locally installed Python modules. Go to the editor
Click me to see the sample solution

10. Write a Python program to display some information about the OS where the script is running. Go to the editor
Click me to see the sample solution

11. Write a Python program to check the sum of three elements (each from an array) from three arrays is equal to a target value. Print all those three-element combinations. Go to the editor
Sample data:
/*
X = [10, 20, 20, 20]
Y = [10, 20, 30, 40]
Z = [10, 30, 40, 20]
target = 70
*/
Click me to see the sample solution

12. Write a Python program to create all possible permutations from a given collection of distinct numbers.Go to the editor
Click me to see the sample solution

13. Write a Python program to get all possible two digit letter combinations from a digit (1 to 9) string. Go to the editor
string_maps = {
"1": "abc",
"2": "def",
"3": "ghi",
"4": "jkl",
"5": "mno",
"6": "pqrs",
"7": "tuv",
"8": "wxy",
"9": "z"
}
Click me to see the sample solution

14. Write a Python program to add two positive integers without using the '+' operator. Go to the editor
Note: Use bit wise operations to add two numbers.
Click me to see the sample solution

15. Write a Python program to check the priority of the four operators (+, -, *, /). Go to the editor
Click me to see the sample solution

16. Write a Python program to get the third side of right angled triangle from two given sides. Go to the editor
Click me to see the sample solution

17. Write a Python program to get all strobogrammatic numbers that are of length n. Go to the editor
A strobogrammatic number is a number whose numeral is rotationally symmetric, so that it appears the same when rotated 180 degrees. In other words, the numeral looks the same right-side up and upside down (e.g., 69, 96, 1001).
For example,
Given n = 2, return ["11", "69", "88", "96"].
Given n = 3, return ['818', '111', '916', '619', '808', '101', '906', '609', '888', '181', '986', '689']

Click me to see the sample solution

18. Write a Python program to find the median among three given numbers. Go to the editor
Click me to see the sample solution

19. Write a Python program to find the value of n where n degrees of number 2 are written sequentially in a line without spaces. Go to the editor
Click me to see the sample solution

20. Write a Python program to fnd the number of zeros at the end of a factorial of a given positive number. Go to the editor
Range of the number(n): (1 = n = 2*109).
Click me to see the sample solution

21. Write a Python program to find the number of notes (Sample of notes: 10, 20, 50, 100, 200 and 500 ) against an given amount. Go to the editor
Range - Number of notes(n) : n (1 = n = 1000000).
Click me to see the sample solution

22. Write a Python program to create a sequence where the first four members of the sequence are equal to one, and each successive term of the sequence is equal to the sum of the four previous ones. Find the Nth member of the sequence. Go to the editor
Click me to see the sample solution

23. Write a Python program that accept a positive number and subtract from this number the sum of its digits and so on. Continues this operation until the number is positive. Go to the editor
Click me to see the sample solution

24. Write a Python program to find the number of divisors of a given integer is even or odd. Go to the editor
Click me to see the sample solution

25. Write a Python program to find the digits which are absent in a given mobile number. Go to the editor
Click me to see the sample solution

26. Write a Python program to compute the summation of the absolute difference of all distinct pairs in an given array (non-decreasing order). Go to the editor
Sample array: [1, 2, 3]
Then all the distinct pairs will be:
1 2
1 3
2 3
Click me to see the sample solution

27. Write a Python program to find the type of the progression (arithmetic progression/geometric progression) and the next successive member of a given three successive members of a sequence. Go to the editor
According to Wikipedia, an arithmetic progression (AP) is a sequence of numbers such that the difference of any two successive members of the sequence is a constant. For instance, the sequence 3, 5, 7, 9, 11, 13, . . . is an arithmetic progression with common difference 2. For this problem, we will limit ourselves to arithmetic progression whose common difference is a non-zero integer.
On the other hand, a geometric progression (GP) is a sequence of numbers where each term after the first is found by multiplying the previous one by a fixed non-zero number called the common ratio. For example, the sequence 2, 6, 18, 54, . . . is a geometric progression with common ratio 3. For this problem, we will limit ourselves to geometric progression whose common ratio is a non-zero integer.
Click me to see the sample solution

28. Write a Python program to print the length of the series and the series from the given 3rd term, 3rd last term and the sum of a series. Go to the editor
Input data:
3rd term - 3
3rd last term - 118 55
Sum of the series - 91
Click me to see the sample solution

29. Write a Python program to find common divisors between two numbers in a given pair. Go to the editor
Click me to see the sample solution

30. Write a Python program to reverse the digits of a given number and add it to the original, If the sum is not a palindrome repeat this procedure. Go to the editor
Note: A palindrome is a word, number, or other sequence of characters which reads the same backward as forward, such as madam or racecar.
Click me to see the sample solution

31. Write a Python program to count the number of carry operations for each of a set of addition problems. Go to the editor
According to Wikipedia " In elementary arithmetic, a carry is a digit that is transferred from one column of digits to another column of more significant digits. It is part of the standard algorithm to add numbers together by starting with the rightmost digits and working to the left. For example, when 6 and 7 are added to make 13, the "3" is written to the same column and the "1" is carried to the left".
Click me to see the sample solution

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.