Complete the code to define a simple tool function that returns the square of a number.
def square_number(num): return num [1] num
The function should multiply the number by itself to get the square. Using * performs multiplication.
Complete the code to create a tool that checks if a string contains the word 'AI'.
def contains_ai(text): return [1] in text
The function checks if the exact string 'AI' is in the text. The correct case is important.
Fix the error in the tool that returns the first element of a list.
def first_element(items): return items[1]0
To access the first element of a list, use square brackets [] with index 0.
Fill both blanks to create a tool that filters even numbers from a list.
def filter_even(numbers): return [num for num in numbers if num [1] 2 [2] 0]
// instead of modulus.The tool uses the modulus operator % to check if the number divided by 2 has a remainder of 0, meaning it is even.
Fill all three blanks to create a tool that builds a dictionary of word lengths for words longer than 3 characters.
def word_lengths(words): return { [1]: [2] for word in words if len(word) [3] 3 }
The dictionary comprehension uses the word as the key and its length as the value, including only words longer than 3 characters.