Python Question
Python Question
Solution:
lst = list(range(10))
print(lst)
Output:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 2:
Solution:
lst = [1, 2, 3, 4, 5]
print(str_lst)
Output:
['1', '2', '3', '4', '5']
Exercise 3:
Solution:
lst = [1, 2, 3, 4, 5]
print(multiplied_lst)
Output:
[2, 4, 6, 8, 10]
Exercise 4:
Extract all odd numbers from a list of integers.
Solution:
print(odd_numbers)
Output:
[1, 3, 5, 7, 9]
Exercise 5:
Solution:
print(replaced_lst)
Copy
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:
print(boolean_lst)
Copy
Output:
[True, True, False, True, True]
Exercise 7:
Solution:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(neg_even_lst)
Copy
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:
import random
print(normalized_matrix)
Copy
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:
print(diagonal_sum)
Copy
Output:
15
Exercise 10:
Solution:
lst = [1, 2, 0, 0, 4, 0]
print(non_zero_indices)
Copy
Output:
[0, 1, 4]
Exercise 11:
Reverse a list.
Solution:
lst = [1, 2, 3, 4, 5]
reversed_lst = lst[::-1]
print(reversed_lst)
Copy
Output:
[5, 4, 3, 2, 1]
Exercise 12:
Solution:
print(identity_matrix)
Copy
Output:
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
Exercise 13:
Solution:
lst = list(range(10))
print(reshaped_lst)
Copy
Output:
[[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
Exercise 14:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(stacked_lst)
Copy
Output:
[[1, 2, 3], [4, 5, 6]]
Exercise 15:
Solution:
lst1 = [1, 2, 3, 4, 5]
lst2 = [3, 4, 5, 6, 7]
print(common_items)
Copy
Output:
[3, 4, 5]
Exercise 16:
Solution:
print(matrix)
Copy
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:
Solution:
print(max_index)
Copy
Output:
3
Exercise 18:
Solution:
print(normalized_lst)
Copy
Output:
[0.0, 0.375, 1.0, 0.125, 0.75]
Exercise 19:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(dot_product)
Copy
Output:
32
Exercise 20:
Solution:
print(count_within_range)
Copy
Output:
4
Exercise 21:
Solution:
import random
print(row_means)
Copy
Output:
[0.6915055628886714, 0.6582373120045442, 0.6397767328136329]
Exercise 22:
Create a random 4x4 list of lists and extract the diagonal elements.
Solution:
import random
print(diagonal_elements)
Copy
Output:
[0.21801982194433744, 0.6811834934253869, 0.7035225685261018,
0.2381369684708886]
Exercise 23:
Solution:
lst = [1, 2, 3, 4, 2, 3, 2, 1]
count_of_2 = lst.count(2)
print(count_of_2)
Copy
Output:
3
Exercise 24:
Solution:
print(lst)
Copy
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:
lst = [5, 2, 8, 1, 7]
max_index = lst.index(max(lst))
min_index = lst.index(min(lst))
Copy
Output:
Index of max: 2
Index of min: 3
Exercise 26:
Solution:
print(matrix)
Copy
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:
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("Counts:", counts)
Copy
Output:
Unique values: [1, 2, 3, 4, 5, 6]
Counts: {1: 2, 2: 2, 3: 1, 4: 2, 5: 1, 6: 1}
Exercise 28:
Solution:
print(matrix)
Copy
Output:
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
Exercise 29:
Solution:
import math
lst = [1, 2, 3, 4, 5]
print(exponential_lst)
Copy
Output:
[2.718281828459045, 7.38905609893065, 20.085536923187668, 54.598150033144236,
148.4131591025766]
Exercise 30:
Swap two rows in a 2D list.
Solution:
import random
print(matrix)
Copy
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:
import random
print(matrix)
Copy
Output:
[[0, 1, 1], [1, 0, 1], [0, 1, 1]]
Exercise 32:
N = 2
print(top_indices)
Copy
Output:
[3, 4]
Exercise 33:
Solution:
import random
print(column_means)
Copy
Output:
[0.5213504257085785, 0.6484573100091598, 0.47727280812201445]
Exercise 34:
Solution:
import random
print(normalized_matrix)
Copy
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:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(concatenated_lst)
Copy
Output:
[1, 2, 3, 4, 5, 6]
Exercise 36:
Solution:
import random
print(sorted_matrix)
Copy
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:
Solution:
lst = [1, 2, 3, 4, 5]
all_nonzero = all(lst)
print(all_nonzero)
Copy
Output:
True
Exercise 38:
Solution:
import random
print(max_indices_per_row)
Copy
Output:
[0, 3, 1]
Exercise 39:
Create a 2D list and replace all nan values with the mean of the list.
Solution:
import math
print(matrix)
Copy
Output:
[[1, 5.285714285714286, 3], [4, 5, 5.285714285714286], [7, 8, 9]]
Exercise 40:
Solution:
import math
print(row_means)
Copy
Output:
[1.5, 5.0, 8.0]
Exercise 41:
Solution:
import random
print(diagonal_sum)
Copy
Output:
0.4238758205862452
Exercise 42:
Solution:
import math
print(degrees_lst)
Copy
Output:
[90.0, 180.0, 270.0]
Exercise 43:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(euclidean_distance)
Copy
Output:
5.196152422706632
Exercise 44:
Create a list and set the values between the 25th and 75th percentile to 0.
Solution:
print(lst)
Copy
Output:
[10, 0, 0, 0, 50]
Exercise 45:
Solution:
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
print(squared_difference)
Copy
Output:
[9, 9, 9]
Exercise 46:
Replace all even numbers in a list with the next odd number.
Solution:
print(next_odd_lst)
Copy
Output:
[3, 5, 9, 13, 15]
Exercise 47:
Solution:
import random
print(normalized_matrix)
Copy
Output:
[[0.0, 0.9791524044681176, 0.0], [1.0, 1.0, 0.6427236160933483],
[0.12545213413765516, 0.0, 1.0]]
Exercise 48:
import random
print(cumulative_sum_axis1)
Copy
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:
Solution:
lst = [0, 0, 0, 1, 0]
any_nonzero = any(lst)
print(any_nonzero)
Copy
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 _ in
range(3)]
threshold = 75
print(matrix)
Copy
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)
print(median)
Copy
Output:
3
Exercise 52:
Solution:
import math
Copy
Output:
[0.0, 1.0, 2.0, 3.0]
Exercise 53:
Solution:
lst = [1, 2, 2, 3, 3, 3, 4, 4]
mode = Counter(lst).most_common(1)[0][0]
print(mode)
Copy
Output:
3
Exercise 54:
Solution:
print(flat_lst)
Copy
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Exercise 55:
Transpose a 2D list.
Solution:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(transposed_matrix)
Copy
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Exercise 56:
Solution:
lst = [1, 2, 2, 3, 4, 4, 5]
seen = set()
print(unique_lst)
Copy
Output:
[1, 2, 3, 4, 5]
Exercise 57:
Solution:
lst1 = [1, 2, 3, 4]
lst2 = [3, 4, 5, 6]
print(intersection)
Copy
Output:
[3, 4]
Exercise 58:
Solution:
print(merged_dict)
Copy
Output:
{'a': 1, 'b': 3, 'c': 4}
Exercise 59:
Solution:
print(sorted_lst)
Copy
Output:
[{'name': 'Matteo', 'age': 22}, {'name': 'Anej', 'age': 25}, {'name': 'Eliza',
'age': 28}]
Exercise 60:
Solution:
d = {'a': 1, 'b': 2, 'c': 3}
print(filtered_dict)
Copy
Output:
{'b': 2, 'c': 3}
Exercise 61:
Solution:
values = [1, 2, 3]
print(dict_from_lists)
Copy
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 62:
Solution:
max_val = max(d.values())
print(max_val)
Copy
Output:
3
Exercise 63:
Invert a dictionary (swap keys and values).
Solution:
print(inverted_dict)
Copy
Output:
{1: 'a', 2: 'b', 3: 'c'}
Exercise 64:
Solution:
default_value = 0
print(default_dict)
Output:
{'a': 0, 'b': 0, 'c': 0}
Exercise 65:
Solution:
list_of_tuples = list(d.items())
print(list_of_tuples)
Output:
[('a', 1), ('b', 2), ('c', 3)]
Exercise 66:
Solution:
print(max_length)
Copy
Output:
6
Exercise 67:
Solution:
print(reversed_sentence)
Copy
Output:
world Hello
Exercise 68:
Solution:
s = "racecar"
is_palindrome = s == s[::-1]
print(is_palindrome)
Copy
Output:
True
Exercise 69:
Solution:
import string
s = "Hello, world!"
print(s_without_punctuation)
Copy
Output:
Hello world
Exercise 70:
Solution:
s = "hello"
char_count = Counter(s)
print(char_count)
Copy
Output:
Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})
Exercise 71:
if not lst:
longest_common_prefix = ""
else:
longest_common_prefix = ""
for i in range(len(shortest_str)):
longest_common_prefix = shortest_str[:i+1]
else:
break
print(longest_common_prefix)
Output:
fl
Exercise 72:
Solution:
s = "hello"
char_list = list(s)
print(char_list)
Copy
Output:
['h', 'e', 'l', 'l', 'o']
Exercise 73:
Generate a list of random integers.
Solution:
import random
print(random_integers)
Copy
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)
Copy
Output:
[4, 5, 2, 1, 3]
Exercise 75:
Solution:
import string
import random
length = 8
password = ''.join(random.choice(string.ascii_letters +
string.digits) for _ in range(length))
print(password)
Copy
Output:
TwfnMdEq
Exercise 76:
Solution:
def factorial(n):
print(factorial(5))
Copy
Output:
120
Exercise 77:
Solution:
def fibonacci(n):
fib_sequence = [0, 1]
fib_sequence.append(fib_sequence[-1] + fib_sequence[-
2])
return fib_sequence[:n]
print(fibonacci(10))
Copy
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Exercise 78:
Solution:
def is_prime(n):
if n <= 1:
return False
if n % i == 0:
return False
return True
print(is_prime(17))
Copy
Output:
True
Exercise 79:
Solution:
while b:
a, b = b, a % b
return a
print(gcd(48, 18))
Copy
Output:
6
Exercise 80:
Solution:
while b:
a, b = b, a % b
return a
return a * b // gcd(a, b)
print(lcm(4, 5))
Copy
Output:
20
Exercise 81:
Solution:
print(sorted_lst)
Copy
Output:
[(3, 1), (2, 2), (1, 3)]
Exercise 82:
Find the second largest number in a list.
Solution:
lst = [1, 2, 3, 4, 5]
second_largest = sorted(set(lst))[-2]
print(second_largest)
Copy
Output:
4
Exercise 83:
Solution:
lst = [1, 2, 3, 2, 1]
print(is_palindrome)
Copy
Output:
True
Exercise 84:
Solution:
num = 12345
print(digit_sum)
Copy
Output:
15
Exercise 85:
Solution:
num = 12345
digit_product = 1
digit_product *= int(digit)
print(digit_product)
Copy
Output:
120
Exercise 86:
Solution:
s = "123.45"
print(is_valid_number)
Copy
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)
Copy
Output:
5
Exercise 88:
Solution:
d = dict(lst)
print(d)
Copy
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 89:
Solution:
print(filtered_lst)
Copy
Output:
[{'name': 'Vivek', 'age': 25}, {'name': ' Neassa', 'age': 28}]
Exercise 90:
lst = [(' Aisha', 'A', 25), (' Remy', 'B', 22), ('Meine', 'A',
28)]
print(sorted_lst)
Copy
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:
values = [1, 2, 3]
print(merged_dict)
Copy
Output:
{'a': 1, 'b': 2, 'c': 3}
Exercise 92:
Solution:
n = 5
print(squared_dict)
Copy
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Exercise 93:
Solution:
s1 = "listen"
s2 = "silent"
print(are_anagrams)
Output:
True
Exercise 94:
Solution:
s = "hello world"
print(vowel_count)
Copy
Output:
3
Exercise 95:
Solution:
s = "12345"
is_digit_only = s.isdigit()
print(is_digit_only)
Copy
Output:
True
Exercise 96:
Solution:
s = "swiss"
print(non_repeated_char)
Copy
Output:
Exercise 97:
Solution:
print(reversed_words_sentence)
Copy
Output:
olleH dlrow
Exercise 98:
Solution:
def fibonacci_up_to(n):
fib_sequence = [0, 1]
fib_sequence.append(fib_sequence[-1] + fib_sequence[-
2])
return fib_sequence[:-1]
print(fibonacci_up_to(100))
Copy
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Exercise 99:
Solution:
s = " a b c d "
print(s_without_whitespace)
Copy
Output:
abcd
Exercise 100:
Solution:
print(new_s)
Output: