Mastering Python: 100 Exercises with Solutions
Welcome to "100 Exercises with Solutions for Python Language"! This comprehensive collection of exercises is designed to help you master the fundamentals and advanced features of Python, a versatile and powerful programming language. Whether you're a beginner just starting out or an experienced developer looking to sharpen your skills, these exercises cover a wide range of topics. They provide practical challenges to enhance your understanding and proficiency in Python, enabling you to write efficient, maintainable, and scalable code.
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
Exercise 1:
Create a list with values ranging from 0 to 9.
Solution:
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 2:
Convert a list of integers to a list of strings.
Solution:
Output:
['1', '2', '3', '4', '5']
Exercise 3:
Multiply all elements in a list by 2.
Solution:
Output:
[2, 4, 6, 8, 10]
Exercise 4:
Extract all odd numbers from a list of integers.
Solution:
Output:
[1, 3, 5, 7, 9]
Exercise 5:
Replace all odd numbers in a list with -1.
Solution:
Output:
[-1, 2, -1, 4, -1, 6, -1, 8, -1, 10]
Exercise 6:
Convert a list of integers to a list of booleans where all non-zero values become True.
Solution:
Output:
[True, True, False, True, True]
Exercise 7:
Replace all even numbers in a list with their negative.
Solution:
Output:
[1, -2, 3, -4, 5, -6, 7, -8, 9]
Exercise 8:
Create a 3x3 list of lists with random values and normalize it.
Solution:
Output:
[[0.34306706561420186, 1.7297679842916804, -1.443812122309246], [-1.1000344556487114, 0.44974827677363144, -1.2563329881914924], [0.02737489828434499, 0.4974073324300242, 0.7528140087555657]]
Exercise 9:
Calculate the sum of the diagonal elements of a 3x3 matrix (list of lists).
Solution:
Output:
15
Exercise 10:
Find the indices of non-zero elements in a list.
Solution:
Output:
[0, 1, 4]
Exercise 11:
Reverse a list.
Solution:
Output:
[5, 4, 3, 2, 1]
Exercise 12:
Create a 3x3 identity matrix as a list of lists.
Solution:
Output:
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Exercise 13:
Reshape a 1D list to a 2D list with 2 rows.
Solution:
Output:
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
Exercise 14:
Stack two lists vertically.
Solution:
Output:
[[1, 2, 3], [4, 5, 6]]
Exercise 15:
Get the common items between two lists.
Solution:
Output:
[3, 4, 5]
Exercise 16:
Create a 5x5 list of lists with row values ranging from 0 to 4.
Solution:
Output:
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
Exercise 17:
Find the index of the maximum value in a list.
Solution:
Output:
3
Exercise 18:
Normalize the values in a list between 0 and 1.
Solution:
Output:
[0.0, 0.375, 1.0, 0.125, 0.75]
Exercise 19:
Calculate the dot product of two lists.
Solution:
Output:
32
Exercise 20:
Count the number of elements in a list within a specific range.
Solution:
Output:
4
Exercise 21:
Find the mean of each row in a 2D list.
Solution:
Output:
[0.6915055628886714, 0.6582373120045442, 0.6397767328136329]
Exercise 22:
Create a random 4x4 list of lists and extract the diagonal elements.
Solution:
Output:
[0.21801982194433744, 0.6811834934253869, 0.7035225685261018, 0.2381369684708886]
Exercise 23:
Count the number of occurrences of a specific value in a list.
Solution:
Output:
3
Exercise 24:
Replace all values in a list with the mean of the list.
Solution:
Output:
[30.0, 30.0, 30.0, 30.0, 30.0]
Exercise 25:
Find the indices of the maximum and minimum values in a list.
Solution:
Output:
Index of max: 2 Index of min: 3
Exercise 26:
Create a 2D list with 1 on the border and 0 inside.
Solution:
Output:
[[1, 1, 1, 1, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 0, 0, 0, 1], [1, 1, 1, 1, 1]]
Exercise 27:
Find the unique values and their counts in a list.
Solution:
Output:
Unique values: [1, 2, 3, 4, 5, 6] Counts: {1: 2, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}
Exercise 28:
Create a 3x3 list of lists with values ranging from 0 to 8.
Solution:
Output:
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
Exercise 29:
Calculate the exponential of all elements in a list.
Solution:
Output:
[2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236, 148.4131591025766]
Exercise 30:
Swap two rows in a 2D list.
Solution:
Output:
[[0.9076338086781157, 0.7017330953175929, 0.9071101156414131, 0.7963804534021283], [0.1506094428868956, 0.717811641354629, 0.199640773755009, 0.5015662651200953], [0.6747176907056143, 0.9966157783211159, 0.22320361325507043, 0.6811201297753695]]
Exercise 31:
Create a random 3x3 list of lists and replace all values greater than 0.5 with 1 and all others with 0.
Solution:
Output:
[[0, 1, 1], [1, 0, 1], [0, 1, 1]]
Exercise 32:
Find the indices of the top N maximum values in a list.
Solution:
Output:
[3, 4]
Exercise 33:
Calculate the mean of each column in a 2D list.
Solution:
Output:
[0.5213504257085785, 0.6484573100091598, 0.47727280812201445]
Exercise 34:
Normalize the values in each column of a 2D list.
Solution:
Output:
[[0.0, 0.0, 1.0], [0.5862533402007957, 0.2971854665761528, 0.30676715747976807], [1.0, 1.0, 0.4559427452444642], [0.8555205149635996, 0.9893735744208437, 0.0]]
Exercise 35:
Concatenate two lists.
Solution:
Output:
[1, 2, 3, 4, 5, 6]
Exercise 36:
Create a 2D list with random values and sort each row.
Solution:
Output:
[[0.28068707911702817, 0.574715722056814, 0.6091238772389816, 0.7243160686088788], [0.08077308781723325, 0.2634152550419555, 0.3714086587664981, 0.5881583318738046], [0.2507019854506827, 0.4647657324258968, 0.7275116612000609, 0.8479103996736468]]
Exercise 37:
Check if all elements in a list are non-zero.
Solution:
Output:
True
Exercise 38:
Find the indices of the maximum value in each row of a 2D list.
Solution:
Output:
[0, 3, 1]
Exercise 39:
Create a 2D list and replace all nan values with the mean of the list.
Solution:
Output:
[[1, 5.285714285714286, 3], [4, 5, 5.285714285714286], [7, 8, 9]]
Exercise 40:
Calculate the mean of each row in a 2D list ignoring nan values.
Solution:
Output:
[1.5, 5.0, 8.0]
Exercise 41:
Compute the sum of diagonal elements in a 2D list.
Solution:
Output:
0.4238758205862452
Exercise 42:
Convert radians to degrees for each element in a list.
Solution:
Output:
[90.0, 180.0, 270.0]
Exercise 43:
Calculate the pairwise Euclidean distance between two lists.
Solution:
Output:
5.196152422706632
Exercise 44:
Create a list and set the values between the 25th and 75th percentile to 0.
Solution:
Output:
[10, 0, 0, 0, 50]
Exercise 45:
Calculate the element-wise square of the difference between two lists.
Solution:
Output:
[9, 9, 9]
Exercise 46:
Replace all even numbers in a list with the next odd number.
Solution:
Output:
[3, 5, 9, 13, 15]
Exercise 47:
Create a 2D list and normalize each column by its range.
Solution:
Output:
[[0.0, 0.9791524044681176, 0.0], [1.0, 1.0, 0.6427236160933483], [0.12545213413765516, 0.0, 1.0]]
Exercise 48:
Compute the cumulative sum of elements along a given axis in a 2D list.
Solution:
Output:
[[0.7546284663316909, 1.31481004728942, 1.8713995335850906, 2.308478041923004], [0.9197238360510473, 0.938647086950884, 1.24603043690484, 1.4655356220488251], [0.5592125265374753, 0.7216087739641586, 1.3660828220854073, 2.3651024686873945]]
Exercise 49:
Check if any element in a list is non-zero.
Solution:
Output:
True
Exercise 50:
Create a 2D list with random integers and replace all values greater than a certain threshold with that threshold.
Solution:
Output:
[[75, 54, 43, 75], [58, 3, 16, 27], [42, 23, 4, 75]]
Exercise 51:
Find the median of a list of numbers.
Solution:
Output:
3
Exercise 52:
Convert a list of numbers to a list of their logarithms.
Solution:
Output:
[0.0, 1.0, 2.0, 3.0]
Exercise 53:
Find the mode of a list of numbers.
Solution:
Output:
3
Exercise 54:
Flatten a list of lists.
Solution:
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 55:
Transpose a 2D list.
Solution:
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Exercise 56:
Remove duplicates from a list while preserving order.
Solution:
Output:
[1, 2, 3, 4, 5]
Exercise 57:
Find the intersection of two lists.
Solution:
Output:
[3, 4]
Exercise 58:
Merge two dictionaries.
Solution:
Output:
{'a': 1, 'b': 3, 'c': 4}
Exercise 59:
Sort a list of dictionaries by a key.
Solution:
Output:
[{'name': 'Matteo', 'age': 22}, {'name': 'Anej', 'age': 25}, {'name': 'Eliza', 'age': 28}]
Exercise 60:
Filter a dictionary based on its values.
Solution:
Output:
{'b': 2, 'c': 3}
Exercise 61:
Create a dictionary from two lists.
Solution:
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 62:
Find the maximum value in a dictionary.
Solution:
Output:
3
Exercise 63:
Invert a dictionary (swap keys and values).
Solution:
Output:
{1: 'a', 2: 'b', 3: 'c'}
Exercise 64:
Create a dictionary with a default value.
Solution:
Output:
{'a': 0, 'b': 0, 'c': 0}
Exercise 65:
Convert a dictionary to a list of tuples.
Solution:
Output:
[('a', 1), ('b', 2), ('c', 3)]
Exercise 66:
Find the length of the longest string in a list.
Solution:
Output:
6
Exercise 67:
Reverse the words in a sentence.
Solution:
Output:
world Hello
Exercise 68:
Check if a string is a palindrome.
Solution:
Output:
True
Exercise 69:
Remove punctuation from a string.
Solution:
Output:
Hello world
Exercise 70:
Count the occurrences of each character in a string.
Solution:
Output:
Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
Exercise 71:
Find the longest common prefix among a list of strings.
Solution:
Output:
fl
Exercise 72:
Convert a string to a list of characters.
Solution:
Output:
['h', 'e', 'l', 'l', 'o']
Exercise 73:
Generate a list of random integers.
Solution:
Output:
[80, 44, 74, 37, 71, 93, 14, 52, 20, 21]
Exercise 74:
Shuffle a list.
Solution:
Output:
[4, 5, 2, 1, 3]
Exercise 75:
Generate a random password of a given length.
Solution:
Output:
TwfnMdEq
Exercise 76:
Calculate the factorial of a number.
Solution:
Output:
120
Exercise 77:
Calculate the Fibonacci sequence up to a given number of terms.
Solution:
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Exercise 78:
Check if a number is prime.
Solution:
Output:
True
Exercise 79:
Find the greatest common divisor (GCD) of two numbers.
Solution:
Output:
6
Exercise 80:
Find the least common multiple (LCM) of two numbers.
Solution:
Output:
20
Exercise 81:
Sort a list of tuples by the second element.
Solution:
Output:
[(3, 1), (2, 2), (1, 3)]
Exercise 82:
Find the second largest number in a list.
Solution:
Output:
4
Exercise 83:
Check if a list is a palindrome.
Solution:
Output:
True
Exercise 84:
Find the sum of the digits of a number.
Solution:
Output:
15
Exercise 85:
Find the product of the digits of a number.
Solution:
Output:
120
Exercise 86:
Check if a string is a valid number.
Solution:
Output:
True
Exercise 87:
Find the length of the longest word in a sentence.
Solution:
Output:
5
Exercise 88:
Convert a list of tuples to a dictionary.
Solution:
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 89:
Filter a list of dictionaries based on a key value.
Solution:
Output:
[{'name': 'Vivek', 'age': 25}, {'name': ' Neassa', 'age': 28}]
Exercise 90:
Sort a list of tuples by multiple keys.
Solution:
Output:
[(' Aisha', 'A', 25), ('Meine', 'A', 28), (' Remy', 'B', 22)]
Exercise 91:
Merge two lists into a dictionary, using one as keys and the other as values.
Solution:
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 92:
Create a dictionary with keys as numbers and values as their squares.
Solution:
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Exercise 93:
Check if two strings are anagrams.
Solution:
Output:
True
Exercise 94:
Count the number of vowels in a string.
Solution:
Output:
3
Exercise 95:
Check if a string contains only digits.
Solution:
Output:
True
Exercise 96:
Find the first non-repeated character in a string.
Solution:
Output:
w
Exercise 97:
Reverse each word in a sentence.
Solution:
Output:
olleH dlrow
Exercise 98:
Generate a list of Fibonacci numbers up to a given number.
Solution:
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Exercise 99:
Remove all whitespaces from a string.
Solution:
Output:
abcd
Exercise 100:
Replace all occurrences of a substring in a string.
Solution:
Output:
Hello universe, welcome to the universe of Python family.
These exercises cover a wide range of basic and intermediate Python concepts, helping users to strengthen their understanding and proficiency in Python.
Python Code Editor:
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.
Test your Python skills with w3resource's quiz