Mastering Python 100 Exercises With Solutions
Mastering Python 100 Exercises With Solutions
Mastering Python
[An editor is available at the bottom of the page to write and execute
the scripts. Go to the editor]
Exercise 1:
Solution:
lst = list(range(10))
print(lst)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 2:
Solution:
1 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
lst = [1, 2, 3, 4, 5]
str_lst = list(map(str, lst))
print(str_lst)
Output:
Exercise 3:
Solution:
lst = [1, 2, 3, 4, 5]
multiplied_lst = [x * 2 for x in lst]
print(multiplied_lst)
Output:
[2, 4, 6, 8, 10]
Exercise 4:
Solution:
Output:
[1, 3, 5, 7, 9]
Exercise 5:
Solution:
Output:
2 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 6:
Solution:
Output:
Exercise 7:
Solution:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
neg_even_lst = [-x if x % 2 == 0 else x for x in lst]
print(neg_even_lst)
Output:
Exercise 8:
Create a 3x3 list of lists with random values and normalize it.
Solution:
import random
matrix = [[random.random() for _ in range(3)] for _ in range
mean = sum(sum(row) for row in matrix) / 9
std = (sum((x - mean) ** 2 for row in matrix for x in row
normalized_matrix = [[(x - mean) / std for x in row] for row
print(normalized_matrix)
Output:
3 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 9:
Calculate the sum of the diagonal elements of a 3x3 matrix (list of lists).
Solution:
Output:
15
Exercise 10:
Solution:
lst = [1, 2, 0, 0, 4, 0]
non_zero_indices = [i for i, x in enumerate(lst) if x !=
print(non_zero_indices)
Output:
[0, 1, 4]
Reverse a list.
Solution:
lst = [1, 2, 3, 4, 5]
reversed_lst = lst[::-1]
print(reversed_lst)
Output:
[5, 4, 3, 2, 1]
Exercise 12:
Solution:
4 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
Exercise 13:
Solution:
lst = list(range(10))
reshaped_lst = [lst[:5], lst[5:]]
print(reshaped_lst)
Output:
Exercise 14:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
stacked_lst = [lst1, lst2]
print(stacked_lst)
Output:
Exercise 15:
Solution:
lst1 = [1, 2, 3, 4, 5]
lst2 = [3, 4, 5, 6, 7]
common_items = list(set(lst1) & set(lst2))
print(common_items)
5 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
[3, 4, 5]
Exercise 16:
Solution:
Output:
Exercise 17:
Solution:
Output:
Exercise 18:
Solution:
Output:
Exercise 19:
6 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
dot_product = sum(x * y for x, y in zip(lst1, lst2))
print(dot_product)
Output:
32
Exercise 20:
Solution:
Output:
Exercise 21:
Solution:
import random
matrix = [[random.random() for _ in range(4)] for _ in range
row_means = [sum(row) / len(row) for row in matrix]
print(row_means)
Output:
Exercise 22:
Create a random 4x4 list of lists and extract the diagonal elements.
Solution:
import random
7 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
import random
matrix = [[random.random() for _ in range(4)] for _ in range
diagonal_elements = [matrix[i][i] for i in range(4)]
print(diagonal_elements)
Output:
Exercise 23:
Solution:
lst = [1, 2, 3, 4, 2, 3, 2, 1]
count_of_2 = lst.count(2)
print(count_of_2)
Output:
Exercise 24:
Solution:
Output:
Exercise 25:
Solution:
lst = [5, 2, 8, 1, 7]
max_index = lst.index(max(lst))
min_index = lst.index(min(lst))
print("Index of max:", max_index)
8 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
Index of max: 2
Index of min: 3
Exercise 26:
Solution:
Output:
Exercise 27:
Solution:
lst = [1, 2, 3, 2, 4, 1, 5, 4, 6]
unique_values = list(set(lst))
counts = {x: lst.count(x) for x in unique_values}
print("Unique values:", unique_values)
print("Counts:", counts)
Output:
Exercise 28:
Solution:
Output:
9 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 29:
Solution:
import math
lst = [1, 2, 3, 4, 5]
exponential_lst = [math.exp(x) for x in lst]
print(exponential_lst)
Output:
Exercise 30:
Solution:
import random
matrix = [[random.random() for _ in range(4)] for _ in range
matrix[0], matrix[1] = matrix[1], matrix[0]
print(matrix)
Output:
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:
import random
matrix = [[random.random() for _ in range(3)] for _ in range
matrix = [[1 if x > 0.5 else 0 for x in row] for row in matrix
print(matrix)
Output:
10 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 32:
Solution:
Output:
[3, 4]
Exercise 33:
Solution:
import random
matrix = [[random.random() for _ in range(3)] for _ in range
column_means = [sum(row[i] for row in matrix) / len(matrix
print(column_means)
Output:
Exercise 34:
Solution:
import random
matrix = [[random.random() for _ in range(3)] for _ in range
normalized_matrix = [[(x - min(col)) / (max(col) - min(col
normalized_matrix = list(map(list, zip(*normalized_matrix
print(normalized_matrix)
Output:
Exercise 35:
11 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
concatenated_lst = lst1 + lst2
print(concatenated_lst)
Output:
[1, 2, 3, 4, 5, 6]
Exercise 36:
Solution:
import random
matrix = [[random.random() for _ in range(4)] for _ in range
sorted_matrix = [sorted(row) for row in matrix]
print(sorted_matrix)
Output:
Exercise 37:
Solution:
lst = [1, 2, 3, 4, 5]
all_nonzero = all(lst)
print(all_nonzero)
Output:
True
Exercise 38:
Solution:
12 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
import random
matrix = [[random.random() for _ in range(4)] for _ in range
max_indices_per_row = [row.index(max(row)) for row in matrix
print(max_indices_per_row)
Output:
[0, 3, 1]
Exercise 39:
Create a 2D list and replace all nan values with the mean of the list.
Solution:
import math
matrix = [[1, float('nan'), 3], [4, 5, float('nan')], [7
mean_val = sum(x for row in matrix for x in row if not math
matrix = [[mean_val if math.isnan(x) else x for x in row
print(matrix)
Output:
Exercise 40:
Solution:
import math
matrix = [[1, 2, float('nan')], [4, float('nan'), 6], [7
row_means = [sum(x for x in row if not math.isnan(x)) /
print(row_means)
Output:
Exercise 41:
Solution:
import random
matrix = [[random.random() for in range(3)] for in range
13 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
0.4238758205862452
Exercise 42:
Solution:
import math
lst = [math.pi / 2, math.pi, 3 * math.pi / 2]
degrees_lst = [math.degrees(x) for x in lst]
print(degrees_lst)
Output:
Exercise 43:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
euclidean_distance = sum((x - y) ** 2 for x, y in zip(lst1
print(euclidean_distance)
Output:
5.196152422706632
Exercise 44:
Create a list and set the values between the 25th and 75th percentile to 0.
Solution:
14 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
print(lst)
Output:
[10, 0, 0, 0, 50]
Exercise 45:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
squared_difference = [(x - y) ** 2 for x, y in zip(lst1, lst2
print(squared_difference)
Output:
[9, 9, 9]
Exercise 46:
Replace all even numbers in a list with the next odd number.
Solution:
Output:
Exercise 47:
Solution:
import random
matrix = [[random.random() for _ in range(3)] for _ in range
min_col = [min(row[i] for row in matrix) for i in range(
max_col = [max(row[i] for row in matrix) for i in range(
normalized_matrix = [[(row[i] - min_col[i]) / (max_col[i
print(normalized_matrix)
15 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
print(normalized_matrix)
Output:
Exercise 48:
Solution:
import random
matrix = [[random.random() for _ in range(4)] for _ in range
cumulative_sum_axis1 = [[sum(row[:i+1]) for i in range(len
print(cumulative_sum_axis1)
Output:
Exercise 49:
Solution:
lst = [0, 0, 0, 1, 0]
any_nonzero = any(lst)
print(any_nonzero)
Output:
True
Exercise 50:
Create a 2D list with random integers and replace all values greater than a
certain threshold with that threshold.
Solution:
import random
matrix = [[random.randint(0, 100) for _ in range(4)] for _
threshold = 75
matrix = [[min(x, threshold) for x in row] for row in matrix
print(matrix)
Output:
16 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
[[75, 54, 43, 75], [58, 3, 16, 27], [42, 23, 4, 75]]
Exercise 51:
Solution:
lst = [2, 5, 1, 3, 4]
lst.sort()
n = len(lst)
median = (lst[n//2] if n % 2 != 0 else (lst[n//2 - 1] + lst
print(median)
Output:
Exercise 52:
Solution:
import math
lst = [1, 10, 100, 1000]
log_lst = [math.log10(x) for x in lst]
print(log_lst)
Output:
Exercise 53:
Solution:
Output:
17 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 54:
Solution:
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 55:
Transpose a 2D list.
Solution:
Output:
Exercise 56:
Solution:
lst = [1, 2, 2, 3, 4, 4, 5]
seen = set()
unique_lst = [x for x in lst if not (x in seen or seen.add
print(unique_lst)
Output:
[1, 2, 3, 4, 5]
Exercise 57:
18 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
lst1 = [1, 2, 3, 4]
lst2 = [3, 4, 5, 6]
intersection = list(set(lst1) & set(lst2))
print(intersection)
Output:
[3, 4]
Exercise 58:
Solution:
Output:
Exercise 59:
Solution:
Output:
[{'name': 'Matteo', 'age': 22}, {'name': 'Anej', 'age': 25}, {'name': 'Eliza', 'a
Exercise 60:
Solution:
19 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Output:
{'b': 2, 'c': 3}
Exercise 61:
Solution:
Output:
Exercise 62:
Solution:
Output:
Exercise 63:
Solution:
Output:
20 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 64:
Solution:
Output:
Exercise 65:
Solution:
Output:
Exercise 66:
Solution:
Output:
Exercise 67:
21 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
Output:
world Hello
Exercise 68:
Solution:
s = "racecar"
is_palindrome = s == s[::-1]
print(is_palindrome)
Output:
True
Exercise 69:
Solution:
import string
s = "Hello, world!"
s_without_punctuation = s.translate(str.maketrans('', ''
print(s_without_punctuation)
Output:
Hello world
Exercise 70:
Solution:
22 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
char_count = Counter(s)
print(char_count)
Output:
Exercise 71:
Solution:
Output:
fl
Exercise 72:
Solution:
s = "hello"
char_list = list(s)
print(char_list)
Output:
Exercise 73:
23 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
import random
random_integers = [random.randint(0, 100) for _ in range
print(random_integers)
Output:
[80, 44, 74, 37, 71, 93, 14, 52, 20, 21]
Exercise 74:
Shuffle a list.
Solution:
import random
lst = [1, 2, 3, 4, 5]
random.shuffle(lst)
print(lst)
Output:
[4, 5, 2, 1, 3]
Exercise 75:
Solution:
import string
import random
length = 8
password = ''.join(random.choice(string.ascii_letters + string
print(password)
Output:
TwfnMdEq
Exercise 76:
Solution:
def factorial(n):
24 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
print(factorial(5))
Output:
120
Exercise 77:
Solution:
def fibonacci(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence
return fib_sequence[:n]
print(fibonacci(10))
Output:
Exercise 78:
Solution:
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(17))
Output:
True
Exercise 79:
25 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
Output:
Exercise 80:
Solution:
Output:
20
Exercise 81:
Solution:
Output:
Exercise 82:
26 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
lst = [1, 2, 3, 4, 5]
second_largest = sorted(set(lst))[-2]
print(second_largest)
Output:
Exercise 83:
Solution:
lst = [1, 2, 3, 2, 1]
is_palindrome = lst == lst[::-1]
print(is_palindrome)
Output:
True
Exercise 84:
Solution:
num = 12345
digit_sum = sum(int(digit) for digit in str(num))
print(digit_sum)
Output:
15
Exercise 85:
Solution:
num = 12345
digit_product = 1
for digit in str(num):
( )
27 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
digit_product *= int(digit)
print(digit_product)
Output:
120
Exercise 86:
Solution:
s = "123.45"
is_valid_number = s.replace('.', '', 1).isdigit()
print(is_valid_number)
Output:
True
Exercise 87:
Solution:
sentence = "The quick brown fox jumps over the lazy dog"
longest_word_length = len(max(sentence.split(), key=len)
print(longest_word_length)
Output:
Exercise 88:
Solution:
Output:
28 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 89:
Solution:
Output:
Exercise 90:
Solution:
lst = [(' Aisha', 'A', 25), (' Remy', 'B', 22), ('Meine'
sorted_lst = sorted(lst, key=lambda x: (x[1], x[2]))
print(sorted_lst)
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:
Exercise 92:
29 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Solution:
n = 5
squared_dict = {i: i**2 for i in range(1, n+1)}
print(squared_dict)
Output:
Exercise 93:
Solution:
s1 = "listen"
s2 = "silent"
are_anagrams = sorted(s1) == sorted(s2)
print(are_anagrams)
Output:
True
Exercise 94:
Solution:
s = "hello world"
vowel_count = sum(1 for char in s if char in 'aeiou')
print(vowel_count)
Output:
Exercise 95:
Solution:
s = "12345"
is_digit_only = s.isdigit()
print(is_digit_only)
30 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
print(is_digit_only)
Output:
True
Exercise 96:
Solution:
s = "swiss"
non_repeated_char = next((char for char in s if s.count(
print(non_repeated_char)
Output:
Exercise 97:
Solution:
Output:
olleH dlrow
Exercise 98:
Solution:
def fibonacci_up_to(n):
fib_sequence = [0, 1]
while fib_sequence[-1] < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence
return fib_sequence[:-1]
print(fibonacci_up_to(100))
Output:
31 of 32 12/14/2024, 8:49 PM
Mastering Python: 100 Exercises with Solutions https://www.w3resource.com/python-exercises/python_100_exercises...
Exercise 99:
Solution:
s = " a b c d "
s_without_whitespace = s.replace(' ', '')
print(s_without_whitespace)
Output:
abcd
Exercise 100:
Solution:
Output:
32 of 32 12/14/2024, 8:49 PM