Makers Academy · Introduction to Python · All Core Exercises Solved
Functions, arithmetic, expressions, state & variables
just_return_it(4) returns: 4
add_one(6) returns: 7
def add_two(num):
return num + 2
Function: add_two EXPECTED: 8 ACTUAL: 8 That's correct! (1 checks right so far)
def multiply_numbers(num_a, num_b):
return num_a * num_b # That `*` means multiply
multiply_numbers(2, 3) is: 6 multiply_numbers(3, 5) is: 15
def add_numbers(num_a, num_b):
return num_a + num_b
add_numbers(2, 3) is: EXPECTED: 5 ACTUAL: 5 That's correct! (1 checks right so far) add_numbers(3, 5) is: EXPECTED: 8 ACTUAL: 8 That's correct! (2 checks right so far)
2 + 3 = 5 (should be 5) 2 * 3 = 6 (should be 6) 2 - 3 = -1 (should be -1) 2 / 3 = 0.6666666666666666 (should be 0.6666666666666666) 3 % 2 = 1 (should be 1) 2 // 3 = 0 (should be 0) 2 ** 3 = 8 (should be 8)
My favourite number is: 99 --- Today's day is: 20 ---
a is 20 b is 20
a is 20 b is 20
def add_one_and_divide_by_two_with_statements(num):
added = num + 1
halved = added / 2
return halved
def add_one_and_divide_by_two_with_an_expression(num):
return (num + 1) / 2
def divide_by_two_and_add_one(num):
# Divide num by two and add one to the result
return num / 2 + 1
def multiply_by_forty_and_add_sixty(num):
# Multiply num by forty, and then add sixty
return num * 40 + 60
def add_together_and_double(num_a, num_b):
# Add together num_a and num_b, then double the result
return (num_a + num_b) * 2
add_one_and_divide_by_two_with_statements(5) is: 3.0 add_one_and_divide_by_two_with_an_expression(5) is: 3.0 Function: divide_by_two_and_add_one EXPECTED: 4.0 ACTUAL: 4.0 That's correct! (1 checks right so far) Function: multiply_by_forty_and_add_sixty ...
Creation, indexing, operations & concatenation
Kay Claude
def get_first_letter(the_str):
# Return the first letter of the string
return the_str[0]
def get_last_letter(the_str):
# Return the last letter of the string
return the_str[-1]
def get_nth_letter(the_str, n):
# Return the letter of the string at the specified index
return the_str[n]
def get_letters_between_four_and_eight(the_str):
# Return the section of the string between indexes four and eight
return the_str[4:8]
The Most Perfect Crab T b s The Function: get_first_letter EXPECTED: T ACTUAL: T That's correct! (1 checks right so far) EXPECTED: F ...
def make_uppercase(string):
# Return the string in uppercase
return string.upper()
def make_lowercase(string):
# Return the string in lowercase
return string.lower()
def strip_whitespace(string):
# Return the string with any whitespace removed from the start and end
return string.strip()
The string is 6 characters long Function: uppercase EXPECTED: HELLO ACTUAL: HELLO That's correct! (1 checks right so far) EXPECTED: WORLD ACTUAL: WORLD That's correct! (2 checks right so far) ...
def greet(name):
# Return the string "Hello, Kay!" where "Kay" is the name provided
return f"Hello, {name}!"
Anteater Forty2 Hello, Kay! Your name is 3 characters long Function: greet EXPECTED: Hello, Chuang-tzu! ACTUAL: Hello, Chuang-tzu! That's correct! (1 checks right so far) EXPECTED: Hello, Crab! ACTUAL: Hello, Crab! ...
If/else, comparison & logical operators
def is_first_of_the_month(day_number):
# Return "First of the month!" if the day number is 1.
# Return "Not first of the month" otherwise.
if day_number == 1:
return "First of the month!"
else:
return "Not first of the month"
def has_five_chars(the_str):
# Return "STRING is five characters long" if the string is five characters
# long.
# Otherwise, return "Not five characters".
if len(the_str) == 5:
return f"{the_str} is five characters long"
else:
return "Not five characters"
It must be winter — or a dead tree It must be winter — or a dead tree Function: is_first_of_the_month EXPECTED: First of the month! ACTUAL: First of the month! That's correct! (1 checks right so far) EXPECTED: Not first of the month ACTUAL: Not first of the month That's correct! (2 checks right so far) ...
def a_is_equal_to_b(a, b):
return a == b
def a_is_less_than_b(a, b):
return a < b
def a_is_greater_than_b(a, b):
return a > b
def a_is_less_than_or_equal_to_b(a, b):
return a <= b
def a_is_greater_than_or_equal_to_b(a, b):
return a >= b
def a_is_not_equal_to_b(a, b):
return a != b
def a_is_within_b(a, b):
return a in b
Function: a_is_equal_to_b EXPECTED: True ACTUAL: True That's correct! (1 checks right so far) EXPECTED: True ACTUAL: True That's correct! (2 checks right so far) EXPECTED: False ACTUAL: False That's correct! (3 checks right so far) ...
def starts_with_x_or_y(the_str):
first_letter = the_str[0]
# VV look at this!
if first_letter == "x" or first_letter == "y":
return "It does!"
else:
return "It does not."
def a_or_b(a, b):
return a or b
def a_and_b(a, b):
return a and b
def not_a(a):
return not a
Function: a_or_b EXPECTED: True ACTUAL: True That's correct! (1 checks right so far) EXPECTED: True ACTUAL: True That's correct! (2 checks right so far) EXPECTED: True ACTUAL: True That's correct! (3 checks right so far) ...
Lists, dictionaries, while/for loops, map, filter & summarise
[1, 2, 3, 4, 5]
def get_first_item(the_list):
return the_list[0]
def get_last_item(the_list):
return the_list[-1]
def get_nth_item(the_list, n):
return the_list[n]
def get_items_between_one_and_three(the_list):
return the_list[1:3]
Function: get_first_item EXPECTED: a ACTUAL: a That's correct! (1 checks right so far) EXPECTED: 34 ACTUAL: 34 That's correct! (2 checks right so far) Function: get_last_item EXPECTED: e ...
def append_item_to_list(the_list, item):
the_list.append(item)
return the_list
def remove_item_from_list(the_list, item):
the_list.remove(item)
return the_list
def count_items_in_list(the_list, item):
return the_list.count(item)
def get_index_of_item(the_list, item):
return the_list.index(item)
def reverse_list(the_list):
the_list.reverse()
return the_list
def list_length(the_list):
return len(the_list)
['a', 'b', 'c', 'd'] 3 ['a', 'b', 'c', 'd'] ['a', 'b', 'c'] ['a', 'b', 'c', 'd'] Function: append_item_to_list EXPECTED: ['a', 'b', 'c'] ACTUAL: ['a', 'b', 'c'] That's correct! (1 checks right so far) EXPECTED: [3, 1, 6] ...
def add_cats_repeatedly(word_list, count):
i = 0
while i < count:
word_list.append("cats")
i = i + 1
return word_list
Hello, Kay! The number is now 0 The number is now 1 The number is now 2 The number is now 3 The number is now 4 The number is now 5 The number is now 6 The number is now 7 The number is now 8 The number is now 9 ...
def print_numbers_in_range():
for number in range(0, 10):
print(f"This number is {number}")
def print_numbers_in_range_with_a_while():
number = 0
while number < 10:
print(f"This number is {number}")
number = number + 1
This letter is a This letter is b This letter is c
def add_up_numbers(numbers):
total = 0
for number in numbers:
total = total + number
return total
My King, I need another five years. Then your crab will be ready. Sincerely, Chuang-tzu Function: add_up_numbers EXPECTED: 10 ACTUAL: 10 That's correct! (1 checks right so far) ...
def add_one_hundred_to_numbers(numbers):
result = []
for number in numbers:
result.append(number + 100)
return result
['I', 'need', 'another', 'five', 'years'] ['I', 'n', 'a', 'f', 'y'] Function: add_one_hundred_to_numbers EXPECTED: [101, 102, 103, 104] ACTUAL: [101, 102, 103, 104] That's correct! (1 checks right so far) EXPECTED: [102, 103, 104, 105] ACTUAL: [102, 103, 104, 105] That's correct! (2 checks right so far)
def only_positive_numbers(numbers):
result = []
for number in numbers:
if number > 0:
result.append(number)
return result
[32, 40, None, 1, 32] [32, 40, 1, 32] Function: only_positive_numbers EXPECTED: [4, 3] ACTUAL: [4, 3] That's correct! (1 checks right so far) EXPECTED: [] ACTUAL: [] That's correct! (2 checks right so far)
A String is: A sequence of characters A List is: A sequence of any item A Dictionary is: A collection of keys mapped to values
def count_words_by_length(words):
counts = {}
for word in words:
length = len(word)
if length not in counts:
counts[length] = 1
else:
counts[length] = counts[length] + 1
return counts
{'t': 2, 'h': 3, 'e': 4, ' ': 7, 'q': 1, 'u': 3, 'i': 1, 'c': 2, 'k': 1, 'b': 2, 'r': 3, 's': 1, 'j': 1, 'm': 1, 'p': 1, 'd': 1, 'o': 1, 'v': 1, 'l': 1, 'a': 2, 'z': 1, 'y': 1}
Function: count_words_by_length
EXPECTED: {3: 2, 1: 1, 4: 1}
ACTUAL: {3: 2, 1: 1, 4: 1}
That's correct! (1 checks right so far)
EXPECTED: {4: 3, 3: 1}
ACTUAL: {4: 3, 3: 1}
That's correct! (2 checks right so far)