w3resource

Python Data Types - Exercises, Practice, Solution

Python Data Types [31 exercises with solution]

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

1. Write a Python program to calculate the length of a string. Go to the editor

Click me to see the sample solution

2. Write a Python program to sum all the items in a list. Go to the editor

Click me to see the sample solution

3. Write a Python program to multiplies all the items in a list. Go to the editor

Click me to see the sample solution

4. Write a Python program to get the largest number from a list. Go to the editor

Click me to see the sample solution

5. Write a Python program to get the smallest number from a list. Go to the editor

Click me to see the sample solution

6. Write a Python program to count the number of characters in a string. Go to the editor
Sample String : 'google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}

Click me to see the sample solution

7. Write a Python program to count the number of characters (character frequency) in a string. Go to the editor
Sample String : google.com'
Expected Result : {'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1}

Click me to see the sample solution

8. Write a Python program to count the number of strings where the string length is 2 or more and the first and last character are same from a given list of strings. Go to the editor
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2

Click me to see the sample solution

9. Write a Python program to get a list, sorted in increasing order by the last element in each tuple from a given list of non-empty tuples. Go to the editor
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]

Click me to see the sample solution

10. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a string. If the string length is less than 2, return instead the empty string. Go to the editor
Sample String : 'w3resource'
Expected Result : 'w3ce'
Sample String : 'w3'
Expected Result : 'w3w3'
Sample String : ' w'
Expected Result : Empty String

Click me to see the sample solution

11.Write a Python program to get a string from a given string where all occurrences of its first char have been changed to '$', except the first char itself. Go to the editor
Sample String : 'restart'
Expected Result : 'resta$t'

Click me to see the sample solution

12. Write a Python program to get a single string from two given strings, separated by a space and swap the first two characters of each string. Go to the editor
Sample String : 'abc', 'xyz'
Expected Result : 'xyc abz'

Click me to see the sample solution

13. Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string is already ends with 'ing' then add 'ly' instead. If the string length of the given string is less than 3, leave it unchanged. Go to the editor
Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringly'

Click me to see the sample solution

14. Write a Python program to find the first appearance of the substring 'not' and 'poor' from a given string, if 'bad' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'. Return the resulting string. Go to the editor
Sample String : 'The lyrics is not that poor!'
Expected Result : 'The lyrics is good!'

Click me to see the sample solution

15. Write a Python function that takes a list of words and returns the length of the longest one. Go to the editor

Click me to see the sample solution

16. Write a Python program to test whether an input is an integer. Go to the editor

Click me to see the sample solution

17. Write a Python program to sort (ascending and descending) a dictionary by value. Go to the editor

Click me to see the sample solution

18. Write a Python program to sort (ascending and descending) a dictionary by key value. Go to the editor

Click me to see the sample solution

19. Write a Python program to add key to a dictionary. Go to the editor

Sample Dictionary : {0: 10, 1: 20}
Expected Result : {0: 10, 1: 20, 2: 30}

Click me to see the sample solution

20. Write a Python program to concatenate following dictionaries to create a new one. Go to the editor

Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

Click me to see the sample solution

21. Write a Python program to check if a given key already exists in a dictionary. Go to the editor

Click me to see the sample solution

22. Write a Python program to iterate over dictionaries using for loops. Go to the editor

Click me to see the sample solution

23. Write a Python program to remove duplicates from a list. Go to the editor

Click me to see the sample solution

24. Write a Python program to check a list is empty or not. Go to the editor

Click me to see the sample solution

25. Write a Python program to clone or copy a list. Go to the editor

Click me to see the sample solution

26. Write a Python program to remove the nth index character from a non empty string. Go to the editor

Click me to see the sample solution

27. Write a Python program to change a given string to a new string where the first and last chars have been exchanged. Go to the editor

Click me to see the sample solution

28. Write a Python program to remove the characters which have odd index values of a given string. Go to the editor

Click me to see the sample solution

29. Write a Python program to find the list of words that are longer than n from a given list of words. Go to the editor

Click me to see the sample solution

30. Write a Python program to count the occurrences of each word in a given sentence. Go to the editor

Click me to see the sample solution

31. Write a Python function that takes two lists and returns True if they have at least one common member. Go to the editor

Click me to see the sample solution

 

Go Top

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/python-data-types.php